Sub topic 14
Encoding and Collation Effects on String Results
Assume you know SELECT, WHERE, GROUP BY, PostgreSQL data types, and NULL. This chapter focuses on two settings that can change string results: encoding, which controls how characters are stored as bytes, and collation, which controls locale-sensitive comparison and ordering.
Encoding: characters versus bytes
A PostgreSQL database has a server encoding, commonly UTF8. UTF-8 uses one to four bytes for a character. ASCII characters use one byte, while many accented characters use two or more.
SELECT current_setting('server_encoding') AS encoding,
char_length('héllo') AS characters,
octet_length('héllo') AS bytes,
encode(convert_to('héllo', 'UTF8'), 'hex') AS hexadecimal;In a UTF-8 database, char_length returns 5, while octet_length returns 6. The character é occupies two UTF-8 bytes.
| Function | Measures | Example for `héllo` in UTF-8 |
|---|---|---|
| `char_length` | Characters | 5 |
| `length(text)` | Characters for text values | 5 |
| `octet_length` | Bytes | 6 |
PostgreSQL counts characters rather than user-perceived symbols. For example, a letter followed by a separate combining accent can count as two characters even though it may look like one symbol.
The database, client, and connection must agree on encoding. If a client sends bytes in the wrong encoding, PostgreSQL can reject the input or display incorrect text. Check the connection setting with:
SHOW client_encoding;Use convert_to to encode text as bytea, PostgreSQL’s binary data type. Use convert_from to decode bytes back into text.
SELECT convert_from(convert_to('café', 'UTF8'), 'UTF8') AS restored_text;Collation: comparison and ordering rules
A collation is a set of language and locale rules for comparing strings. It can affect ORDER BY, comparison operators, LIKE, case conversion, and regular-expression behavior.
The default collation comes from the database or column definition. You can request a different collation for one expression:
SELECT word
FROM (VALUES ('a'), ('B'), ('ä'), ('b')) AS words(word)
ORDER BY word COLLATE "C";The "C" collation uses simple byte-oriented rules. It is often predictable for technical values, but it is not a natural-language sort order. Other collations may sort uppercase and lowercase letters differently and may treat accented characters according to language-specific rules.
You can inspect available collations with:
SELECT collname, collprovider, collcollate
FROM pg_collation
ORDER BY collname
LIMIT 10;Collation is also part of an index’s behavior. An index created under one collation may not provide the expected ordering for a query using another collation. Operating-system locale updates or ICU library updates can change sort rules, so production systems should monitor collation version warnings and rebuild affected indexes when PostgreSQL requires it.
Practical choices
- Use
char_lengthfor user-visible character limits. - Use
octet_lengthwhen measuring encoded payload size. - Use an explicit
COLLATEwhen a query needs stable, documented comparison rules. - Test sorting and case conversion with the production database locale.
- Keep collation choices consistent between columns, queries, and indexes.
Exercise
In a UTF-8 database, why can char_length('é') and octet_length('é') return different values?
Show answer
char_length counts characters, while octet_length counts encoded bytes. UTF-8 commonly stores é in two bytes, so the results can be 1 and 2.