slot: Only send the flush position in receivelog.c when a slot is being used
authorAndres Freund <andres@anarazel.de>
Wed, 29 Jan 2014 23:30:07 +0000 (00:30 +0100)
committerAndres Freund <andres@anarazel.de>
Wed, 29 Jan 2014 23:30:07 +0000 (00:30 +0100)
src/bin/pg_basebackup/receivelog.c

index d50cf8d90e33d33a2b5dd9488afca0c03eea8f8a..6854e13b9caa2a6f8d5cc968f684095c61e27e86 100644 (file)
@@ -31,6 +31,7 @@
 /* fd and filename for currently open WAL file */
 static int     walfile = -1;
 static char current_walfile_name[MAXPGPATH] = "";
+static bool reportFlushPosition = false;
 static XLogRecPtr lastFlushPosition = InvalidXLogRecPtr;
 
 static PGresult *HandleCopyStream(PGconn *conn, XLogRecPtr startpos,
@@ -423,7 +424,10 @@ sendFeedback(PGconn *conn, XLogRecPtr blockpos, int64 now, bool replyRequested)
        len += 1;
        sendint64(blockpos, &replybuf[len]);            /* write */
        len += 8;
-       sendint64(lastFlushPosition, &replybuf[len]);           /* flush */
+       if (reportFlushPosition)
+               sendint64(lastFlushPosition, &replybuf[len]);           /* flush */
+       else
+               sendint64(InvalidXLogRecPtr, &replybuf[len]);           /* flush */
        len += 8;
        sendint64(InvalidXLogRecPtr, &replybuf[len]);           /* apply */
        len += 8;
@@ -525,9 +529,26 @@ ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
                return false;
 
        if (replication_slot != NULL)
+       {
+               /*
+                * Report the flush position, so the primary can know what WAL we'll
+                * possibly re-request, and remove older WAL safely.
+                *
+                * We only report it when a slot has explicitly been used, because
+                * reporting the flush position makes one elegible as a synchronous
+                * replica. People shouldn't include generic names in
+                * synchronous_standby_names, but we've protected them against it so
+                * far, so lets continue to do so in the situations we don't need
+                * to. Without a slot reporting back wouldn't have any effect anyway.
+                */
+               reportFlushPosition = true;
                sprintf(slotcmd, "SLOT \"%s\" ", replication_slot);
+       }
        else
+       {
+               reportFlushPosition = false;
                slotcmd[0] = 0;
+       }
 
        if (sysidentifier != NULL)
        {