Hi all,

While playing with pg_logical_emit_message() and WAL replay, I have
noticed that LogLogicalMessage() inserts a record but forgets to make
sure that the record has been flushed.  So, for example, if the system
crashes the message inserted can get lost.

I was writing some TAP tests for it for the sake of a bug, and I have
found this the current behavior annoying because one cannot really
rely on it when emulating crashes.

This has been introduced in 3fe3511 (from 2016), and there is no
mention of that on the original thread that led to this commit:
https://www.postgresql.org/message-id/flat/5685F999.6010202%402ndquadrant.com

This could be an issue for anybody using LogLogicalMessage() out of
core, as well, because it would mean some records lost.  So, perhaps
this should be treated as a bug, sufficient for a backpatch?

Thoughts?
--
Michael
diff --git a/src/backend/replication/logical/message.c b/src/backend/replication/logical/message.c
index c5de14afc6..cebe6c5d05 100644
--- a/src/backend/replication/logical/message.c
+++ b/src/backend/replication/logical/message.c
@@ -47,6 +47,7 @@ LogLogicalMessage(const char *prefix, const char *message, size_t size,
 				  bool transactional)
 {
 	xl_logical_message xlrec;
+	XLogRecPtr	lsn;
 
 	/*
 	 * Force xid to be allocated if we're emitting a transactional message.
@@ -71,7 +72,11 @@ LogLogicalMessage(const char *prefix, const char *message, size_t size,
 	/* allow origin filtering */
 	XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
 
-	return XLogInsert(RM_LOGICALMSG_ID, XLOG_LOGICAL_MESSAGE);
+	lsn = XLogInsert(RM_LOGICALMSG_ID, XLOG_LOGICAL_MESSAGE);
+
+	/* Make sure that the message hits disk before leaving */
+	XLogFlush(lsn);
+	return lsn;
 }
 
 /*

Attachment: signature.asc
Description: PGP signature

Reply via email to