From a32c3ec893cafbd3a4b42c34270a80198f28f123 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Sun, 28 Jun 2015 22:25:55 +0300
Subject: [PATCH] Promote the assertion that XLogBeginInsert() is not called
 twice into ERROR.

Seems like cheap insurance for WAL bugs. A spurious call to
XLogBeginInsert() in itself would be fairly harmless, but if there is any
data registered and the insertion is not completed/cancelled properly, there
is a risk that the data ends up in a wrong WAL record.

Per Jeff Janes's suggestion.
---
 src/backend/access/transam/xloginsert.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index b96c39ac657..0b89c0a7a2c 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -122,12 +122,14 @@ XLogBeginInsert(void)
 	Assert(max_registered_block_id == 0);
 	Assert(mainrdata_last == (XLogRecData *) &mainrdata_head);
 	Assert(mainrdata_len == 0);
-	Assert(!begininsert_called);
 
 	/* cross-check on whether we should be here or not */
 	if (!XLogInsertAllowed())
 		elog(ERROR, "cannot make new WAL entries during recovery");
 
+	if (begininsert_called)
+		elog(ERROR, "XLogBeginInsert was already called");
+
 	begininsert_called = true;
 }
 
-- 
GitLab