Skip to content

Commit

Permalink
Add XLogRecGetFullTransactionId() function.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
macdice committed Jun 26, 2019
1 parent 0185343 commit 1203c2f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/backend/access/transam/xlogreader.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "replication/origin.h"

#ifndef FRONTEND
#include "miscadmin.h"
#include "utils/memutils.h"
#endif

Expand Down Expand Up @@ -1460,3 +1461,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
8 changes: 8 additions & 0 deletions src/include/access/xlogreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#ifndef XLOGREADER_H
#define XLOGREADER_H

#ifndef FRONTEND
#include "access/transam.h"
#endif

#include "access/xlogrecord.h"
#include "storage/smgr.h"

Expand Down Expand Up @@ -242,6 +246,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,
Expand Down

0 comments on commit 1203c2f

Please sign in to comment.