PostgreSQL Bad Encoding: Diagnosing and Repairing Text Corruption
Inspecting how text values are encoded in PostgreSQL requires the right tool. Casting text directly to bytea often fails because the cast expects the text value to be a pre-formatted bytea input representation, not a raw byte sequence. A better diagnostic is convert_to(col,'SQL_ASCII'), which returns the actual stored bytes. Here, SQL_ASCII acts as a "no conversion" directive, revealing the raw bytes regardless of the server's declared encoding.
Systematic encoding errors can frequently be fixed without a full dump and restore cycle. The most prevalent issues reported by IRC users involve two specific corruption patterns:
- Double-encoded UTF-8: Where UTF-8 bytes are mistakenly interpreted and encoded again.
- Mixed encodings: A blend of UTF-8 and legacy encodings like LATIN1 or WIN1252, often appearing in databases using
SQL_ASCIIorLATIN1.
Both problems are addressable through automated repair scripts with strong reliability. This is possible because UTF-8’s design has structural properties that allow corrupted byte sequences to be identified and restored with high confidence.



