| From: | Staroverov Ilja <i(dot)staroverov(at)ftdata(dot)ru> |
|---|---|
| To: | "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Optional UTF-8 encoding for server logs |
| Date: | 2026-11-27 06:58:31 |
| Message-ID: | 2BEE1699-2738-40BB-ADC8-A192EBE7ACC8@localhost.localdomain |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
I'd like to revisit the server-log part of Itagaki Takahiro's 2009
proposal [1]. The April patch covered several server-side encoding
issues, and the later Windows console thread discussed a separate
log-encoding patch [2]. This series builds on the log_encoding part of
that work and adds an opt-in UTF-8 output mode for stderr, csvlog,
jsonlog, and syslog.
The same problem has come up in several forms. BUG #5661 mixed UTF-8
and EUC_JP [3]. The 2012 Linux and Windows reports
and the 2014 pgAdmin report describe the effect on log readers [6, 7].
BUG #15772 reported Windows code-page output alongside UTF-8, and
Tom raised the choice of output encoding, conversion failures and
early startup [8]. In BUG #19581, Tom explained the current
prevailing-encoding behavior and pointed to the postmaster's locale
for output from the checkpointer [11]. Even a cluster whose databases
all use UTF-8 can therefore produce a mixed log. The series adds an
explicit setting for installations that want UTF-8 logs.
Here is how the patches handle the main concerns from those threads.
* Choice of encoding. Mikio pointed out that Japanese installations
may expect the platform's native encoding, including CP932 on
Windows [3]. Tom raised the cost of imposing a common encoding
and suggested separating files by encoding [4, 8]. Craig discussed
that option and noted the users who need a single stream, especially
with syslog [5]. The default here is off. Administrators can opt in
to UTF-8 where their readers expect it. The setting applies to
server log output. SQL clients and Windows Event Log continue to
receive messages through their existing output paths.
* Conversion failure and error reporting. Tom pointed out that a
conversion error could hide the original diagnostic, and that
memory may be scarce while reporting an out-of-memory error [4].
The log path uses the built-in no-error conversions and writes
undecodable bytes as hexadecimal escapes. It falls back to a bounded
stack buffer if the extra allocation fails and marks any truncation.
In a Linux single-user test, GDB forced the conversion allocation
to fail during reporting of a deliberately raised error with
SQLSTATE 53200. The log retained the SQLSTATE and the beginning of
the diagnostic, followed by the marker. The next SQL statement ran.
* Source encoding and startup. The first patch makes the existing
forward maps available without catalog lookup or dynamic loading.
The second chooses the database encoding once it has been set and
the message encoding before then. Formatting converts the prefix
template and the database text separately. PostgreSQL can therefore
log a UTF-8 configuration prefix alongside WIN1251 diagnostics.
The setting takes effect once its configuration value is applied.
The logging collector can also capture raw stderr from external
programs. That stream can still contain bytes in other encodings.
* Input before authentication. Jacob Champion and Andres Freund
discussed the absence of a reliable encoding for client data at
this stage and the limits of escaping after formatting [9, 10].
Tom supported using pg_clean_ascii() for the immediate problem
and suggested a common treatment if non-ASCII handling were
revisited. This series uses pg_clean_ascii() for startup names
of unknown encoding in text prefixes. Structured fields retain
valid UTF-8 and escape undecodable bytes before CSV or JSON
serialization. Conversion at the output boundary cannot recover
the encodings of fragments already combined in a diagnostic.
The first attachment moves the existing local-to-UTF8 maps and keeps
the existing SQL conversion entry points. The second adds the setting
and the tests. In an earlier WSL build of the series, the backend
executable was about 0.84 MiB larger than the baseline. The existing
conversion modules use the forward tables from the backend.
The new TAP test passed on Linux and on a native MinGW Windows build.
It covers the default behavior, mixed database encodings, an
EUC_JIS_2004 combined character, a UTF-8 prefix, invalid bytes, long
quoted fields, a failed login, and logging in an aborted transaction.
The Windows TAP test used the logging collector. A separate
redirected-stderr check confirmed the UTF-8 startup prefix on Windows
and timed out before testing messages from a WIN1251 session. On
Linux, the core regression suite and test_misc also passed in the
tested configuration. Manual Linux checks exercised localized startup
messages and syslog chunk boundaries.
An earlier WSL microbenchmark used a loop emitting WIN1251 messages
with a 200-character payload. Comparing median backend CPU times over
seven runs against log_encoding=off gave about 1.7 us per message for
stderr, 0.7 us for CSV, and 2.2 us for JSON in that workload.
I'd appreciate feedback on the opt-in scope and the placement of the
forward maps, especially the trade-off between backend size and
having conversion available before database initialization.
[1] https://postgr.es/m/20090415135043.C439.52131E4D@oss.ntt.co.jp
[2] https://postgr.es/m/20091013093313.A6F3.52131E4D@oss.ntt.co.jp
[3] https://postgr.es/m/4C92D839.8020800@postnewspapers.com.au
https://postgr.es/m/20100922212552.93B2.A495B709@yahoo.co.jp
[4] https://postgr.es/m/23765.1285162902@sss.pgh.pa.us
https://postgr.es/m/4C9D6D6F.4050806@postnewspapers.com.au
[5] https://postgr.es/m/4E8421E5.7090407@ringerc.id.au
[6] https://www.postgresql.org/message-id/flat/CAHU7rYZbqojCDiSy-4bPSmeREgAuZZR8XWxXQ7NMf5XG0nhE7A%40mail.gmail.com
https://postgr.es/m/CAD8_UcbfncTLTPeVcihXGJtGoB5BjfqBH48R3Zor0Y91Ph0hUw@mail.gmail.com
[7] https://postgr.es/m/53EFB081.4080800@tortenboxer.de
https://postgr.es/m/29206.1408218049@sss.pgh.pa.us
[8] https://postgr.es/m/15772-b32554465e0ff1f3@postgresql.org
https://postgr.es/m/12140.1555604456@sss.pgh.pa.us
[9] https://postgr.es/m/2878aee8-a4a6-8078-9740-efe90fcf8328@timescale.com
[10] https://postgr.es/m/20220715211914.c5oasfknx2njndov@awork3.anarazel.de
https://postgr.es/m/3501997.1657920220@sss.pgh.pa.us
[11] https://postgr.es/m/19581-e1b37ea2556ab74d@postgresql.org
https://postgr.es/m/3803795.1785155554@sss.pgh.pa.us
--
Ilya Staroverov
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Make-built-in-local-to-UTF8-conversions-available.patch | application/octet-stream | 34.9 KB |
| v1-0002-Add-an-opt-in-UTF-8-encoding-for-server-logs.patch | application/octet-stream | 44.0 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Osama Abdul Qader | 2026-11-27 07:10:36 | Re: Preserve tablespace of exclusion constraint indexes in ALTER TYPE |
| Previous Message | shveta malik | 2026-11-27 06:51:38 | Re: Preserve conflict log table during upgrade |