I have Postgresql database that internally uses UTF-8 encoding. Some clients that connect to the database use LATIN 2 (ISO 8859-2) client encoding when they connect to database. I can't change the encoding on clients. The clients are selecting values from a table that contains text. The problem is that some (not all but a small percentage) rows in this table contain characters that can not be converted to LATIN 2 so there is an error when clients try to select this rows.
I can simulate this with psql:
database=> set client_encoding = 'LATIN2';
database=> select text_field from some_table;
ERROR: character with byte sequence 0xc4 0x9f in encoding "UTF8" has no equivalent in encoding "LATIN2"
Since only a small percentage of the rows contain 'non convertible' characters I can select rows that have 'convertible' if i put the limit 10 because first 10 rows have supported characters.
database=> set client_encoding = 'LATIN2';
database=> select text_field from some_table limit 10;
..... works fine, I get 10 rows with data since all of them can be converted.
Can I somehow select the problematic rows that are causing errors with UTF-8 to LATIN2 conversion? I would like to isolate and modify (update or delete, whatever...) this rows but I can't find a way to select only this rows.
I tried the following that is not working:
With '^[[:ascii:]]*$' - not OK because this select non ascii characters (hex value > 127) that have equivalents in LATIN2 so the conversion for them works:
select * from some_table where text_field !~ '^[[:ascii:]]*$';
Comparing char_length and octet_length - not OK since it also selects rows with characters that have equivalents in LATIN2.
select * from some_table where WHERE char_length(text_field)!=octet_length(text_field);