Skip to content
Book a call

MySQL TEXT holds 65,535 bytes, and a Romanian letter costs two

· 4-minute read · Engineering · by Direct Softworks

A TEXT column sounds like it holds 65,535 characters. It holds 65,535 bytes, and whether a longer value fails loudly depends on a keyword you chose for another reason.

The posts on this blog are markdown files in our repository. They reach the production database as a generated SQL file, one insert per post, and the article body lands in a column of type TEXT. The database is MariaDB, which behaves like MySQL in everything below.

TEXT holds 65,535. Most developers finish that sentence with "characters". The correct word is bytes, and on a site that writes Romanian the difference is not academic.

Nothing has gone wrong with it yet. That is the reason to write it down: this is a failure that would not announce itself, and the way it stays quiet is more interesting than the limit.

Bytes, not characters

A TEXT value is stored with a two-byte length prefix, so the ceiling is 2 to the 16th minus one: 65,535 bytes. The column is utf8mb4, which is plain UTF-8, and UTF-8 spends a different number of bytes on different characters:

Characters Bytes each
ASCII letters, digits, punctuation 1
ă â î ș ț, and the older cedilla spellings of the last two 2
The euro sign, typographic quotes, the long dash 3
Emoji 4

So the limit in characters depends on what the characters are. Say a Romanian article is 40,000 characters and one in ten carries a diacritic. That is 44,000 bytes. Paste in a table copied from a word processor, full of typographic quotes and dashes, and it climbs again without a single visible character being added.

The number on the column is a byte count, and nothing you see on screen tells you the byte count.

JavaScript makes this worse, not better. string.length counts UTF-16 code units, which is neither characters nor bytes: ș has a length of 1 and costs 2 bytes, an emoji has a length of 2 and costs 4. A length check written with .length undercounts every non-ASCII character. In Node the right call is Buffer.byteLength(s, "utf8"); in a browser, new TextEncoder().encode(s).length.

One more trap sits in the same table. A VARCHAR(255) column counts characters, not bytes. Two columns in one row, two different units. Our check measures titles in bytes too, which is stricter than the column needs and costs nothing.

How a too-long value disappears quietly

Out of the box, MariaDB runs in strict SQL mode, and in strict mode a value longer than its column is an error: Data too long for column. That is the behaviour you want. The insert fails, somebody looks.

Two things take it away. A server configured without strict mode cuts the value to fit and records a warning. And the IGNORE keyword: the MySQL manual is explicit that when IGNORE and strict mode are both in effect, IGNORE takes precedence, and the error becomes a warning. The row goes in, shortened.

Our seed uses INSERT IGNORE, deliberately. Once a post is live, the panel owns it, and a re-run of the seed must never revert an edit somebody made there. The keyword we chose so that a re-run would be harmless is the same keyword that turns an over-long post into a shorter one.

The warning does not help either. The command-line client prints warnings when you pass --show-warnings; a file piped into it without that flag loads and says nothing about them. The load succeeds, the article is live, and it ends mid-sentence. The first person to find out is a reader who gets to the bottom.

The same keyword hides a second failure. A duplicate slug collides with the unique key and, under INSERT IGNORE, the row simply does not load. No error, one post missing.

The escaping problem next to it

Getting markdown into SQL has its own silent failure. Whether a backslash inside a quoted string is an escape character depends on the server's sql_mode, and markdown is full of backslashes, apostrophes and diacritics. A mis-escaped byte would surface as a damaged article, not as an error.

So the generator does not quote anything. Every string goes in as hex:

function hex(s) {
  return `CONVERT(UNHEX('${Buffer.from(String(s), "utf8").toString("hex")}') USING utf8mb4)`;
}

Hex has no quoting rules to get wrong.

The check measures bytes

The content check runs as part of npm test. For every post it computes the body's size with Buffer.byteLength and fails if it reaches the TEXT limit. It also fails on a duplicate slug, on a title or image path over 255 bytes, and on a cover image that is not on disk, all of which share the property that the database would accept them without complaint.

The longest post we have today is about 7 KB, roughly a ninth of the limit. The check is still worth its one line, because a limit you are nowhere near is exactly the one nobody remembers when a post finally reaches it.

If your content can genuinely be long, choose the column for it at design time. MEDIUMTEXT holds 16,777,215 bytes and costs one extra byte of length prefix per value, not per declared maximum. A rich-text editor that inlines images as base64 can pass 64 KB with a single screenshot, and that is a much better thing to discover in a schema review than in a support ticket.

The general shape

Two things combined here, and each is harmless alone. A limit stated in one unit and measured in another. And an error path that a reasonable choice, made for a different reason, quietly downgraded to a warning nobody is shown.

It is the same family as Romanian letters that exist twice in Unicode: text that looks identical to a person and is not identical to the machine. Whenever text crosses into storage, measure it the way the storage measures it, in bytes, in the encoding the column actually uses, and make the check fail before the database gets the chance to be polite about it.

More notes