aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFujii Masao <fujii@postgresql.org>2014-11-19 14:11:12 +0900
committerFujii Masao <fujii@postgresql.org>2014-11-19 14:11:12 +0900
commitf66c20b317578838a39a1de8014c4363bdc98b9a (patch)
treefa72e88e71ef1b044928500bb73b32c0d806cac1
parent8d7af8fbe7349cbebb576459c9b5f54dfcc6216a (diff)
downloadpostgresql-f66c20b317578838a39a1de8014c4363bdc98b9a.tar.gz
postgresql-f66c20b317578838a39a1de8014c4363bdc98b9a.zip
Fix pg_receivexlog --slot so that it doesn't prevent the server shutdown.
When pg_receivexlog --slot is connecting to the server, at the shutdown of the server, walsender keeps waiting for the last WAL record to be replicated and flushed in pg_receivexlog. But previously pg_receivexlog issued sync command only when WAL file was switched. So there was the case where the last WAL was never flushed and walsender had to keep waiting infinitely. This caused the server shutdown to get stuck. pg_recvlogical handles this problem by calling fsync() when it receives the request of immediate reply from the server. That is, at shutdown, walsender sends the request, pg_recvlogical receives it, flushes the last WAL record, and sends the flush location back to the server. Since walsender can see that the last WAL record is successfully flushed, it can exit cleanly. This commit introduces the same logic as pg_recvlogical has, to pg_receivexlog. Back-patch to 9.4 where pg_receivexlog was changed so that it can use the replication slot. Original patch by Michael Paquier, rewritten by me. Bug report by Furuya Osamu.
-rw-r--r--src/bin/pg_basebackup/receivelog.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c
index e51cac9b87b..1b8a5ad15ce 100644
--- a/src/bin/pg_basebackup/receivelog.c
+++ b/src/bin/pg_basebackup/receivelog.c
@@ -1020,6 +1020,25 @@ ProcessKeepaliveMsg(PGconn *conn, char *copybuf, int len,
/* If the server requested an immediate reply, send one. */
if (replyRequested && still_sending)
{
+ if (reportFlushPosition && lastFlushPosition < blockpos &&
+ walfile != 1)
+ {
+ /*
+ * If a valid flush location needs to be reported,
+ * flush the current WAL file so that the latest flush
+ * location is sent back to the server. This is necessary to
+ * see whether the last WAL data has been successfully
+ * replicated or not, at the normal shutdown of the server.
+ */
+ if (fsync(walfile) != 0)
+ {
+ fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"),
+ progname, current_walfile_name, strerror(errno));
+ return false;
+ }
+ lastFlushPosition = blockpos;
+ }
+
now = feGetCurrentTimestamp();
if (!sendFeedback(conn, blockpos, now, false))
return false;