From fa4684aab65634fae24eb4b6a593d5f01b6f8834 Mon Sep 17 00:00:00 2001 From: Rucha Kulkarni Date: Thu, 26 Nov 2026 19:34:42 +0000 Subject: [PATCH v1] Avoid infinite retry loop when a GUC_REPORT value cannot be reported search_path became GUC_REPORT in 28a1121fd91, so a schema name outside the client's encoding now has to be reported via ParameterStatus. If the conversion fails, ReportGUCOption() throws before the caller clears GUC_NEEDS_REPORT and removes the entry from guc_report_list, so the same report is retried forever at every ReadyForQuery, and the backend never gets back to reading from the client. Clear the pending state before calling ReportGUCOption(), both in ReportChangedGUCOptions() and in BeginReportingGUCOptions(), so a failed report is not retried. Also have ReportGUCOption() catch ERRCODE_UNTRANSLATABLE_CHARACTER and report it as a WARNING instead of an ERROR, since the SET itself already succeeded; any other error is re-thrown. The client is left with a stale value for the affected setting, as there is no way to deliver one its encoding cannot represent. --- src/backend/utils/misc/guc.c | 74 +++++++++++++++++-- src/test/modules/test_misc/meson.build | 1 + .../test_misc/t/017_guc_report_encoding.pl | 69 +++++++++++++++++ 3 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 src/test/modules/test_misc/t/017_guc_report_encoding.pl diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 21c9ccec1e2..ac39fa28447 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -2482,7 +2482,20 @@ BeginReportingGUCOptions(void) struct config_generic *conf = hentry->gucvar; if (conf->flags & GUC_REPORT) + { + /* + * The value of this variable is sent to the client just below, so + * remove it from the report queue. Otherwise, if sending fails + * here, ReportChangedGUCOptions() would try again and warn a + * second time. + */ + if (conf->status & GUC_NEEDS_REPORT) + { + conf->status &= ~GUC_NEEDS_REPORT; + slist_delete(&guc_report_list, &conf->report_link); + } ReportGUCOption(conf); + } } } @@ -2525,9 +2538,15 @@ ReportChangedGUCOptions(void) report_link, iter.cur); Assert((conf->flags & GUC_REPORT) && (conf->status & GUC_NEEDS_REPORT)); - ReportGUCOption(conf); + + /* + * Clear the report status before transmitting, so that if + * ReportGUCOption() throws an error we don't retry the same variable + * at the next ReadyForQuery. + */ conf->status &= ~GUC_NEEDS_REPORT; slist_delete_current(&iter); + ReportGUCOption(conf); } } @@ -2546,19 +2565,58 @@ ReportGUCOption(struct config_generic *record) strcmp(val, record->last_reported) != 0) { StringInfoData msgbuf; + MemoryContext oldcontext = CurrentMemoryContext; + bool sent = true; - pq_beginmessage(&msgbuf, PqMsg_ParameterStatus); - pq_sendstring(&msgbuf, record->name); - pq_sendstring(&msgbuf, val); - pq_endmessage(&msgbuf); + /* + * Converting the value to the client encoding can fail, if the value + * contains a character that doesn't exist in that encoding. Since we + * are not executing any command here, report that as a WARNING rather + * than an ERROR, which would wrongly fail the preceding command and + * abort any open transaction. This is safe because nothing is + * written to the output buffer until pq_endmessage(). Any other + * error is re-thrown. + */ + PG_TRY(); + { + pq_beginmessage(&msgbuf, PqMsg_ParameterStatus); + pq_sendstring(&msgbuf, record->name); + pq_sendstring(&msgbuf, val); + pq_endmessage(&msgbuf); + } + PG_CATCH(); + { + ErrorData *edata; + + MemoryContextSwitchTo(oldcontext); + edata = CopyErrorData(); + FlushErrorState(); + + if (edata->sqlerrcode != ERRCODE_UNTRANSLATABLE_CHARACTER) + ReThrowError(edata); + + ereport(WARNING, + (errcode(edata->sqlerrcode), + errmsg("could not report parameter \"%s\" to client", + record->name), + errdetail_internal("%s", edata->message))); + FreeErrorData(edata); + + sent = false; + } + PG_END_TRY(); /* * We need a long-lifespan copy. If guc_strdup() fails due to OOM, * we'll set last_reported to NULL and thereby possibly make a - * duplicate report later. + * duplicate report later. Don't update it if the report failed, so + * that a later change back to a reportable value is still reported. */ - guc_free(record->last_reported); - record->last_reported = guc_strdup(LOG, val); + if (sent) + { + guc_free(record->last_reported); + record->last_reported = guc_strdup(LOG, val); + } } pfree(val); diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build index 5225aa280ca..00eb3dbddcd 100644 --- a/src/test/modules/test_misc/meson.build +++ b/src/test/modules/test_misc/meson.build @@ -25,6 +25,7 @@ tests += { 't/014_log_statement_max_length.pl', 't/015_temp_schema_exit_deferrable.pl', 't/016_heap_scan_stream.pl', + 't/017_guc_report_encoding.pl', ], # The injection points are cluster-wide, so disable installcheck 'runningcheck': false, diff --git a/src/test/modules/test_misc/t/017_guc_report_encoding.pl b/src/test/modules/test_misc/t/017_guc_report_encoding.pl new file mode 100644 index 00000000000..32413bc3a94 --- /dev/null +++ b/src/test/modules/test_misc/t/017_guc_report_encoding.pl @@ -0,0 +1,69 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Test that a GUC_REPORT variable whose value cannot be converted to the +# client encoding is reported as a WARNING, does not fail the command or +# abort an open transaction, and does not leave the session stuck +# retrying the report. + +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $node = PostgreSQL::Test::Cluster->new('main'); +# The euro sign (U+20AC) exists in UTF8 but not in LATIN1. +$node->init(extra => [ '--encoding=UTF8', '--locale=C' ]); +$node->start; + +my $euro_schema = "U&\"\\20AC\""; +my $conv_error = qr/has no equivalent in encoding "LATIN1"/; + +# Use a persistent session so that a backend stuck retrying the report shows +# up as a query timeout. +my $psql = $node->background_psql('postgres', on_error_stop => 0); + +$psql->query_safe("SET client_encoding = 'LATIN1'"); + +$psql->query("SET search_path = $euro_schema"); +like( + $psql->{stderr}, + qr/WARNING: could not report parameter "search_path" to client/, + 'unreportable search_path produces a WARNING'); +like($psql->{stderr}, $conv_error, 'WARNING detail names the conversion failure'); +unlike($psql->{stderr}, qr/ERROR/, 'unreportable search_path is not an ERROR'); + +$psql->{stderr} = ''; +my $alive = $psql->query_safe('SELECT 42'); +like($alive, qr/\b42\b/, 'session is still responsive after the failed report'); + +# The SET did take effect on the server. +$psql->{stderr} = ''; +$psql->query_safe("SET client_encoding = 'UTF8'"); +my $sp = $psql->query_safe('SHOW search_path'); +like($sp, qr/\x{e2}\x{82}\x{ac}|\x{20ac}/, 'search_path was set despite the failed report'); +$psql->query_safe("SET client_encoding = 'LATIN1'"); + +# A later, representable change is still reported. +$psql->{stderr} = ''; +$psql->query('SET search_path = public'); +is($psql->{stderr}, '', 'a representable search_path change reports cleanly'); + +# An unreportable value set inside a transaction block must not abort it. +$psql->{stderr} = ''; +$psql->query("BEGIN; SET search_path = $euro_schema; SELECT 'still open'"); +like($psql->{stderr}, qr/WARNING/, 'in-transaction unreportable value warns'); +unlike($psql->{stderr}, qr/ERROR|aborted/, 'in-transaction unreportable value does not abort'); +$psql->{stderr} = ''; +my $commit = $psql->query('COMMIT'); +is($psql->{stderr}, '', 'transaction commits after the failed report'); + +$psql->quit; + +$node->safe_psql('postgres', 'SELECT 1'); +ok(1, 'server survived the unreportable search_path'); + +$node->stop; + +done_testing(); -- 2.50.1