This problem is quite old, but it leads to the inability to use XML generation functions in PostgreSQL database for some cases, or at least requires to perform subsequent parsing and regenerating XML by an external utility. It reproduces in PostgreSQL 12.4, compiled by Visual C++ build 1914, 64-bit (windows 10), but I've seen the same problem in 9.6 build from CentOS yum package.
How to reproduce:
Just execute the query (actually the xmlelement call is enough to reproduce the proble):
select xmlserialize(document xmlroot(xmlelement(name "ЭлементВКириллице", xmlattributes('ЗначениеВКириллице' as "АтрибутВКириллице"), 'ТекстВКириллице'), version '1.0', standalone yes) as text);
Expected result:
<?xml version="1.0" standalone="yes"?><ЭлементВКириллице АтрибутВКириллице="ЗначениеВКириллице">ТекстВКириллице</ЭлементВКириллице>
Actual result:
<?xml version="1.0" standalone="yes"?><ЭлементВКириллице АтрибутВКириллице="ЗначениеВКириллице">ТекстВКириллице</ЭлементВКириллице>
This example uses cyrillic letters, but it could be any non-ASCII character.
According to the
discussion, this problem arises because PostgreSQL does not provides libxml2 an information of document encoding due to the lack of xmlTextWriterStartDocument call, so libxml2 has no idea that encoding is UTF-8 and non-ASCII characters could be written without converting to &#x...;-sequences.