From 6bca5fb05fffabe6394f0276f773d3b321bfac6d Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Thu, 15 Oct 2026 22:51:23 -0400 Subject: [PATCH v9] psql: Fall back to English for unsafe hidden query labels. Since 41d69e6dcca the labels are translated, and the server rejects the query when the translation is not valid in its encoding. Translate the label only when the client and server encodings match and the translated bytes pass the same check the server does. --- src/bin/psql/common.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 788555b9782..3618383a2b1 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -28,6 +28,7 @@ #include "fe_utils/mbprint.h" #include "fe_utils/string_utils.h" #include "mainloop.h" +#include "mb/pg_wchar.h" #include "portability/instr_time.h" #include "settings.h" @@ -2630,9 +2631,27 @@ command_no_begin(const char *query) void addQueryComment(PQExpBuffer buf, const char *label) { -#if defined(ENABLE_NLS) - /* This will become conditional once we've settled on the tests to use */ - label = _(label); +#if defined(ENABLE_NLS) && !defined(WIN32) + const char *server_enc = PQparameterStatus(pset.db, "server_encoding"); + int client_enc = PQclientEncoding(pset.db); + + /* + * Use the translation only if the server will accept its bytes. With + * equal client and server encodings, the server just verifies the string + * in that encoding, so we can run the same check here. We don't try to + * guess which encoding gettext used, so the result may look like junk if + * that doesn't match, but it can't cause an error. Don't try on Windows, + * where the encoding gettext produces is harder to predict. + */ + if (server_enc != NULL && + pg_char_to_encoding(server_enc) == client_enc) + { + const char *translated = _(label); + int len = strlen(translated); + + if (pg_encoding_verifymbstr(client_enc, translated, len) == len) + label = translated; + } #endif printfPQExpBuffer(buf, "/* %s */\n", label);