Skip to content

Commit 7130be8

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 5060275 commit 7130be8

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
@@ -582,14 +582,40 @@ StreamLogicalLog(void)
582582
res = PQgetResult(conn);
583583
if (PQresultStatus(res) == PGRES_COPY_OUT)
584584
{
585+
PQclear(res);
586+
585587
/*
586588
* We're doing a client-initiated clean exit and have sent CopyDone to
587-
* the server. We've already sent replay confirmation and fsync'd so
588-
* we can just clean up the connection now.
589+
* the server. Drain any messages, so we don't miss a last-minute
590+
* ErrorResponse. The walsender stops generating XLogData records once
591+
* it sees CopyDone, so expect this to finish quickly. After CopyDone,
592+
* it's too late for sendFeedback(), even if this were to take a long
593+
* time. Hence, use synchronous-mode PQgetCopyData().
589594
*/
590-
goto error;
595+
while (1)
596+
{
597+
int r;
598+
599+
if (copybuf != NULL)
600+
{
601+
PQfreemem(copybuf);
602+
copybuf = NULL;
603+
}
604+
r = PQgetCopyData(conn, &copybuf, 0);
605+
if (r == -1)
606+
break;
607+
if (r == -2)
608+
{
609+
pg_log_error("could not read COPY data: %s",
610+
PQerrorMessage(conn));
611+
time_to_abort = false; /* unclean exit */
612+
goto error;
613+
}
614+
}
615+
616+
res = PQgetResult(conn);
591617
}
592-
else if (PQresultStatus(res) != PGRES_COMMAND_OK)
618+
if (PQresultStatus(res) != PGRES_COMMAND_OK)
593619
{
594620
pg_log_error("unexpected termination of replication stream: %s",
595621
PQresultErrorMessage(res));

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

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

74+
# Insert some rows after $endpos, which we won't read.
75+
$node_master->safe_psql('postgres',
76+
qq[INSERT INTO decoding_test(x,y) SELECT s, s::text FROM generate_series(5,50) s;]
77+
);
78+
7479
my $stdout_recv = $node_master->pg_recvlogical_upto(
7580
'postgres', 'test_slot', $endpos, 180,
7681
'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)