Turns out that 0xed 0xb3 0xb6 is valid utf8, but describes \uDCF7 which is not a valid defined codepoint, which postgres barfs on when you try to insert it.
Python2 doesn't recognise there being anything invalid about it, however.
The workaround in the end was to use iconv_codecs to use iconv to strip invalid codepoints out of the string before handing to postgres, with something like:
Error ends up looking like:
in the event_search logic.
Turns out that 0xed 0xb3 0xb6 is valid utf8, but describes \uDCF7 which is not a valid defined codepoint, which postgres barfs on when you try to insert it.
Python2 doesn't recognise there being anything invalid about it, however.
The workaround in the end was to use
iconv_codecs
to useiconv
to strip invalid codepoints out of the string before handing to postgres, with something like:row["value"].encode("iconv:utf8", "ignore").decode("utf8")
Which seemed to work on linux, but fails on macOS.
Thanks to
@flux:matrix.org
for reporting and debugging this!The original cause of the bad data is https://github.com/matrix-org/synapse/issues/3537