Skip to content

Commit a28db20

Browse files
committed
In successful pg_recvlogical, end PGRES_COPY_OUT cleanly.
pg_recvlogical merely called PQfinish(), so the backend sent messages after the disconnect. When that caused EPIPE in internal_flush(), before a LogicalConfirmReceivedLocation(), the next pg_recvlogical would repeat already-acknowledged records. Whether or not the defect causes EPIPE, post-disconnect messages could contain an ErrorResponse that the user should see. One properly ends PGRES_COPY_OUT by repeating PQgetCopyData() until it returns a negative value. Augment one of the tests to cover the case of WAL past --endpos. Back-patch to v10, where commit 7c03078 first appeared. Before that commit, pg_recvlogical never reached PGRES_COPY_OUT. Reported by Thomas Munro. Discussion: https://postgr.es/m/CAEepm=1MzM2Z_xNe4foGwZ1a+MO_2S9oYDq3M5D11=JDU_+0Nw@mail.gmail.com
1 parent a411361 commit a28db20

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/bin/pg_basebackup/pg_recvlogical.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,40 @@ StreamLogicalLog(void)
604604
res = PQgetResult(conn);
605605
if (PQresultStatus(res) == PGRES_COPY_OUT)
606606
{
607+
PQclear(res);
608+
607609
/*
608610
* We're doing a client-initiated clean exit and have sent CopyDone to
609-
* the server. We've already sent replay confirmation and fsync'd so
610-
* we can just clean up the connection now.
611+
* the server. Drain any messages, so we don't miss a last-minute
612+
* ErrorResponse. The walsender stops generating XLogData records once
613+
* it sees CopyDone, so expect this to finish quickly. After CopyDone,
614+
* it's too late for sendFeedback(), even if this were to take a long
615+
* time. Hence, use synchronous-mode PQgetCopyData().
611616
*/
612-
goto error;
617+
while (1)
618+
{
619+
int r;
620+
621+
if (copybuf != NULL)
622+
{
623+
PQfreemem(copybuf);
624+
copybuf = NULL;
625+
}
626+
r = PQgetCopyData(conn, &copybuf, 0);
627+
if (r == -1)
628+
break;
629+
if (r == -2)
630+
{
631+
fprintf(stderr, _("%s: could not read COPY data: %s"),
632+
progname, PQerrorMessage(conn));
633+
time_to_abort = false; /* unclean exit */
634+
goto error;
635+
}
636+
}
637+
638+
res = PQgetResult(conn);
613639
}
614-
else if (PQresultStatus(res) != PGRES_COMMAND_OK)
640+
if (PQresultStatus(res) != PGRES_COMMAND_OK)
615641
{
616642
fprintf(stderr,
617643
_("%s: unexpected termination of replication stream: %s"),

src/test/recovery/t/006_logical_decoding.pl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
);
7171
print "waiting to replay $endpos\n";
7272

73+
# Insert some rows after $endpos, which we won't read.
74+
$node_master->safe_psql('postgres',
75+
qq[INSERT INTO decoding_test(x,y) SELECT s, s::text FROM generate_series(5,50) s;]
76+
);
77+
7378
my $stdout_recv = $node_master->pg_recvlogical_upto(
7479
'postgres', 'test_slot', $endpos, 180,
7580
'include-xids' => '0',
@@ -89,7 +94,7 @@
8994
'skip-empty-xacts' => '1');
9095
chomp($stdout_recv);
9196
is($stdout_recv, '',
92-
'pg_recvlogical acknowledged changes, nothing pending on slot');
97+
'pg_recvlogical acknowledged changes');
9398

9499
$node_master->safe_psql('postgres', 'CREATE DATABASE otherdb');
95100

0 commit comments

Comments
 (0)