Sub topic 4
Measuring Strings: Characters Versus Bytes
Assume you know basic SELECT syntax, PostgreSQL data types, NULL, and how to run queries in psql or another SQL client. This chapter focuses on measuring text accurately, especially when characters occupy different numbers of bytes.
Two meanings of length
A string has at least two useful sizes:
- Character length counts characters as users usually perceive them.
- Byte length counts the bytes used to store the encoded string.
PostgreSQL provides char_length and length for character length. It provides octet_length for byte length.
SELECT char_length('héllo') AS characters,
length('héllo') AS same_character_count,
octet_length('héllo') AS bytes;In a UTF-8 database, the result is:
| characters | same_character_count | bytes |
|---|---|---|
| 5 | 5 | 6 |
The letter é is one character but uses two UTF-8 bytes. The other four characters use one byte each.
Character length
Use char_length(value) when a rule concerns the number of characters. For example, a username rule might require between 3 and 30 characters.
SELECT username
FROM users
WHERE char_length(username) BETWEEN 3 AND 30;length(value) is commonly used as a shorter spelling for character length when the value is text, varchar, or another character string.
NULL has no length. Any of these expressions returns NULL when its argument is NULL:
SELECT char_length(NULL::text) AS characters,
octet_length(NULL::text) AS bytes;If you need a numeric value for missing text, decide what missing means before replacing it:
SELECT char_length(coalesce(display_name, '')) AS characters
FROM users;Byte length
Use octet_length(value) when the limit is imposed by a protocol, file format, storage system, or external service.
SELECT message
FROM outbound_messages
WHERE octet_length(message) <= 160;A 160-character message can exceed 160 bytes when it contains accented characters, emoji, or text from many non-Latin writing systems. A character limit and a byte limit are different validation rules.
Trailing spaces and fixed-width types
For text and varchar, spaces are ordinary characters:
SELECT char_length('a '::text) AS characters,
octet_length('a '::text) AS bytes;Both results include the two trailing spaces. The char(n) type is different. PostgreSQL pads values to the declared width and generally ignores trailing spaces when returning a char(n) value. Fixed-width types can therefore produce surprising measurements and comparisons. Prefer text or varchar unless fixed-width behavior is required.
Applying measurements safely
Measure the original value before trimming if spaces are meaningful. Measure the normalized value if validation should ignore accidental surrounding spaces.
SELECT char_length(trim(raw_name)) AS name_characters,
octet_length(trim(raw_name)) AS name_bytes
FROM imports;The database encoding also matters. Check it when diagnosing unexpected byte counts:
SHOW server_encoding;In a UTF-8 database, byte counts follow UTF-8 encoding. Character counting still does not mean “number of displayed symbols” in every case. Some visible symbols consist of multiple Unicode code points, such as a letter followed by a combining accent.
Exercise
In a UTF-8 database, which function should validate a 40-character display-name rule, and which should validate a 255-byte message-column rule?
Show answer
Use charlength for the display-name rule and octetlength for the message-column rule.