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/[email protected]
[2] https://postgr.es/m/[email protected]
[3] https://postgr.es/m/[email protected]
    https://postgr.es/m/[email protected]
[4] https://postgr.es/m/[email protected]
    https://postgr.es/m/[email protected]
[5] https://postgr.es/m/[email protected]
[6] 
https://www.postgresql.org/message-id/flat/CAHU7rYZbqojCDiSy-4bPSmeREgAuZZR8XWxXQ7NMf5XG0nhE7A%40mail.gmail.com
    
https://postgr.es/m/cad8_ucbfnctltpevcihxgjtgob5bjfqbh48r3zor0y91ph0...@mail.gmail.com
[7] https://postgr.es/m/[email protected]
    https://postgr.es/m/[email protected]
[8] https://postgr.es/m/[email protected]
    https://postgr.es/m/[email protected]
[9] https://postgr.es/m/[email protected]
[10] https://postgr.es/m/[email protected]
     https://postgr.es/m/[email protected]
[11] https://postgr.es/m/[email protected]
     https://postgr.es/m/[email protected]

--
Ilya Staroverov

Attachment: v1-0001-Make-built-in-local-to-UTF8-conversions-available.patch
Description: v1-0001-Make-built-in-local-to-UTF8-conversions-available.patch

Attachment: v1-0002-Add-an-opt-in-UTF-8-encoding-for-server-logs.patch
Description: v1-0002-Add-an-opt-in-UTF-8-encoding-for-server-logs.patch

Reply via email to