Skip to content

Commit 24ee495

Browse files
author
Commitfest Bot
committed
[CF 6239] v4 - Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect on publisher's walsender
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/6239 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAHGQGwGxSVPia+ZtJxyqWTdP_-YG_YPb=CMrZpC7fTjknARX=A@mail.gmail.com Author(s): Fujii Masao
2 parents 795e94c + 0d2149e commit 24ee495

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,6 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
180180
/* Tell the publisher to translate to our encoding */
181181
keys[++i] = "client_encoding";
182182
vals[i] = GetDatabaseEncodingName();
183-
184-
/*
185-
* Force assorted GUC parameters to settings that ensure that the
186-
* publisher will output data values in a form that is unambiguous
187-
* to the subscriber. (We don't want to modify the subscriber's
188-
* GUC settings, since that might surprise user-defined code
189-
* running in the subscriber, such as triggers.) This should
190-
* match what pg_dump does.
191-
*/
192-
keys[++i] = "options";
193-
vals[i] = "-c datestyle=ISO -c intervalstyle=postgres -c extra_float_digits=3";
194183
}
195184
else
196185
{
@@ -256,6 +245,38 @@ libpqrcv_connect(const char *conninfo, bool replication, bool logical,
256245
PQclear(res);
257246
}
258247

248+
/*
249+
* Force assorted GUC parameters to settings that ensure that the
250+
* publisher will output data values in a form that is unambiguous to the
251+
* subscriber. (We don't want to modify the subscriber's GUC settings,
252+
* since that might surprise user-defined code running in the subscriber,
253+
* such as triggers.) This should match what pg_dump does.
254+
*/
255+
if (replication && logical)
256+
{
257+
const char *params[] =
258+
{"datestyle", "intervalstyle", "extra_float_digits"};
259+
const char *values[] = {"ISO", "postgres", "3"};
260+
261+
for (int j = 0; j < lengthof(params); j++)
262+
{
263+
char sql[100];
264+
PGresult *res;
265+
266+
sprintf(sql, "SET %s = %s", params[j], values[j]);
267+
res = libpqsrv_exec(conn->streamConn, sql,
268+
WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE);
269+
if (PQresultStatus(res) != PGRES_COMMAND_OK)
270+
{
271+
PQclear(res);
272+
*err = psprintf(_("could not set %s: %s"),
273+
params[j], pchomp(PQerrorMessage(conn->streamConn)));
274+
goto bad_connection;
275+
}
276+
PQclear(res);
277+
}
278+
}
279+
259280
conn->logical = logical;
260281

261282
return conn;

src/test/subscription/t/001_rep_changes.pl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,17 +438,34 @@
438438
22.22|bar|2),
439439
'update works with dropped subscriber column');
440440

441+
# Verify that GUC settings supplied in the CONNECTION string take effect on
442+
# the publisher's walsender. We do this by enabling log_disconnections in
443+
# the CONNECTION string later and checking that the publisher's log contains a
444+
# disconnection message.
445+
#
446+
# First, confirm that no such disconnection message appears before enabling
447+
# log_disconnections.
448+
$logfile = slurp_file($node_publisher->logfile, $log_location);
449+
unlike(
450+
$logfile,
451+
qr/disconnection: session time:/,
452+
'log_disconnections has not been enabled yet');
453+
441454
# check that change of connection string and/or publication list causes
442455
# restart of subscription workers. We check the state along with
443456
# application_name to ensure that the walsender is (re)started.
444457
#
445458
# Not all of these are registered as tests as we need to poll for a change
446459
# but the test suite will fail nonetheless when something goes wrong.
460+
#
461+
# Enable log_disconnections as the change of connection string,
462+
# which is also for the above mentioned test of GUC settings passed through
463+
# CONNECTION.
447464
my $oldpid = $node_publisher->safe_psql('postgres',
448465
"SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
449466
);
450467
$node_subscriber->safe_psql('postgres',
451-
"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr sslmode=disable'"
468+
"ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_disconnections=on'''"
452469
);
453470
$node_publisher->poll_query_until('postgres',
454471
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
@@ -552,6 +569,15 @@
552569
or die
553570
"Timed out while waiting for apply to restart after renaming SUBSCRIPTION";
554571

572+
# Check that the expected disconnection message appears,
573+
# which shows that log_disconnections=on from the CONNECTION string
574+
# was correctly passed through to and honored by the walsender.
575+
$logfile = slurp_file($node_publisher->logfile, $log_location);
576+
like(
577+
$logfile,
578+
qr/disconnection: session time:/,
579+
'log_disconnections in CONNECTION string had effect on publisher\'s walsender');
580+
555581
# check all the cleanup
556582
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_renamed");
557583

0 commit comments

Comments
 (0)