XLogRecGetFullXid()

Started by Thomas Munroover 6 years ago2 messages
#1Thomas Munro
thomas.munro@gmail.com
1 attachment(s)

Hello hackers,

Here is a small patch extracted from the undo log patch set that I'd
like to discuss separately and commit soon. I'm pretty sure that
zheap, zedstore and anyone else developing new AMs based on 64 bit
xids needs this, but there are no plans to extend WAL records to
actually carry 64 bit xids yet, and I want to discourage people from
making generic xid expanding functions that don't have any
interlocking, as I mentioned recently[1]/messages/by-id/CA+hUKGJPuKR7i7UvmXRXhjhdW=3v1-nSO3aFn4XDLdkBJru15g@mail.gmail.com.

It's defined in xlogreader.c, because that's where such things
naturally live, but it can only work during replay for now, so I
wrapped it in a FRONTEND invisibility cloak. That means that
front-end tools (pg_waldump.c) can't use it and will have to continue
show 32 bit xids for now.

Better ideas?

[1]: /messages/by-id/CA+hUKGJPuKR7i7UvmXRXhjhdW=3v1-nSO3aFn4XDLdkBJru15g@mail.gmail.com

--
Thomas Munro
https://enterprisedb.com

Attachments:

0001-Add-XLogRecGetFullXid-function.patchapplication/octet-stream; name=0001-Add-XLogRecGetFullXid-function.patchDownload
From 337395874903af64fc3e5f5f90498d40393c976a Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 18 Jun 2019 15:09:56 +1200
Subject: [PATCH] Add XLogRecGetFullXid() function.

In order to be able to work with 64 bit transaction IDs without increasing
the size of the WAL, infer the epoch.  In general we can't do that safely,
but during replay we can because we know that the next XID can't advance
concurrently (it's only advanced by replaying WAL records).
---
 src/backend/access/transam/xlogreader.c | 35 +++++++++++++++++++++++++
 src/include/access/xlogreader.h         |  8 ++++++
 2 files changed, 43 insertions(+)

diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c
index 41dae916b46..33ccfc15531 100644
--- a/src/backend/access/transam/xlogreader.c
+++ b/src/backend/access/transam/xlogreader.c
@@ -26,6 +26,7 @@
 #include "replication/origin.h"
 
 #ifndef FRONTEND
+#include "miscadmin.h"
 #include "utils/memutils.h"
 #endif
 
@@ -1443,3 +1444,37 @@ RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
 
 	return true;
 }
+
+#ifndef FRONTEND
+
+/*
+ * Extract the FullTransactionId from a WAL record.
+ */
+FullTransactionId
+XLogRecGetFullXid(XLogReaderState *record)
+{
+	TransactionId	xid,
+					next_xid;
+	uint32			epoch;
+
+	/*
+	 * This function is only safe during replay, because it depends on the
+	 * replay state.  See AdvanceNextFullTransactionIdPastXid() for more.
+	 */
+	Assert(AmStartupProcess() || !IsUnderPostmaster);
+
+	xid = XLogRecGetXid(record);
+	next_xid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
+	epoch = EpochFromFullTransactionId(ShmemVariableCache->nextFullXid);
+
+	/*
+	 * If xid is numerically greater than next_xid, it has to be from the
+	 * last epoch.
+	 */
+	if (unlikely(xid > next_xid))
+		--epoch;
+
+	return FullTransactionIdFromEpochAndXid(epoch, xid);
+};
+
+#endif
diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h
index 04228e2a871..a12c94cba67 100644
--- a/src/include/access/xlogreader.h
+++ b/src/include/access/xlogreader.h
@@ -25,6 +25,10 @@
 #ifndef XLOGREADER_H
 #define XLOGREADER_H
 
+#ifndef FRONTEND
+#include "access/transam.h"
+#endif
+
 #include "access/xlogrecord.h"
 
 typedef struct XLogReaderState XLogReaderState;
@@ -240,6 +244,10 @@ extern bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record,
 #define XLogRecBlockImageApply(decoder, block_id) \
 	((decoder)->blocks[block_id].apply_image)
 
+#ifndef FRONTEND
+extern FullTransactionId XLogRecGetFullXid(XLogReaderState *record);
+#endif
+
 extern bool RestoreBlockImage(XLogReaderState *recoder, uint8 block_id, char *dst);
 extern char *XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len);
 extern bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id,
-- 
2.21.0

#2Thomas Munro
thomas.munro@gmail.com
In reply to: Thomas Munro (#1)
Re: XLogRecGetFullXid()

On Fri, Jul 12, 2019 at 1:25 PM Thomas Munro <thomas.munro@gmail.com> wrote:

Here is a small patch extracted from the undo log patch set that I'd
like to discuss separately and commit soon. [...]

Pushed.

--
Thomas Munro
https://enterprisedb.com