From 03f8f13ca905ac32f9870177fb05926d9bb8d3b5 Mon Sep 17 00:00:00 2001
From: Craig Ringer <craig@2ndquadrant.com>
Date: Wed, 1 Jun 2016 14:05:58 +0800
Subject: [PATCH 12/21] Don't attempt to export a snapshot from
 CREATE_REPLICATION_SLOT when in recovery

Exporting a snapshot requires us to start an xact, and we can't do that from a
server in recovery. So skip snapshot export. The client must handle syncing of
initial state via some external means like a slot on the master or manually
stopping replay from a physical copy at the same LSN.
---
 src/backend/replication/walsender.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index ac8c2c3..957ae36 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -842,7 +842,18 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
 		 * Export a plain (not of the snapbuild.c type) snapshot to the user
 		 * that can be imported into another session.
 		 */
-		snapshot_name = SnapBuildExportSnapshot(ctx->snapshot_builder);
+		if (!RecoveryInProgress())
+		{
+			snapshot_name = SnapBuildExportSnapshot(ctx->snapshot_builder);
+		}
+		else
+		{
+			/*
+			 * Can't assign an xid during recovery so we can't export a
+			 * snapshot.
+			 */
+			snapshot_name = "";
+		}
 
 		/* don't need the decoding context anymore */
 		FreeDecodingContext(ctx);
-- 
2.5.5

