PostgreSQL Source Code  git master
commit_ts.h File Reference
#include "access/xlog.h"
#include "datatype/timestamp.h"
#include "replication/origin.h"
#include "storage/sync.h"
Include dependency graph for commit_ts.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  xl_commit_ts_set
 
struct  xl_commit_ts_truncate
 

Macros

#define COMMIT_TS_ZEROPAGE   0x00
 
#define COMMIT_TS_TRUNCATE   0x10
 
#define SizeOfCommitTsSet
 
#define SizeOfCommitTsTruncate
 

Typedefs

typedef struct xl_commit_ts_set xl_commit_ts_set
 
typedef struct xl_commit_ts_truncate xl_commit_ts_truncate
 

Functions

void TransactionTreeSetCommitTsData (TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz timestamp, RepOriginId nodeid)
 
bool TransactionIdGetCommitTsData (TransactionId xid, TimestampTz *ts, RepOriginId *nodeid)
 
TransactionId GetLatestCommitTsData (TimestampTz *ts, RepOriginId *nodeid)
 
Size CommitTsShmemBuffers (void)
 
Size CommitTsShmemSize (void)
 
void CommitTsShmemInit (void)
 
void BootStrapCommitTs (void)
 
void StartupCommitTs (void)
 
void CommitTsParameterChange (bool newvalue, bool oldvalue)
 
void CompleteCommitTsInitialization (void)
 
void CheckPointCommitTs (void)
 
void ExtendCommitTs (TransactionId newestXact)
 
void TruncateCommitTs (TransactionId oldestXact)
 
void SetCommitTsLimit (TransactionId oldestXact, TransactionId newestXact)
 
void AdvanceOldestCommitTsXid (TransactionId oldestXact)
 
int committssyncfiletag (const FileTag *ftag, char *path)
 
void commit_ts_redo (XLogReaderState *record)
 
void commit_ts_desc (StringInfo buf, XLogReaderState *record)
 
const char * commit_ts_identify (uint8 info)
 

Variables

PGDLLIMPORT bool track_commit_timestamp
 

Macro Definition Documentation

◆ COMMIT_TS_TRUNCATE

#define COMMIT_TS_TRUNCATE   0x10

◆ COMMIT_TS_ZEROPAGE

#define COMMIT_TS_ZEROPAGE   0x00

◆ SizeOfCommitTsSet

#define SizeOfCommitTsSet
Value:
(offsetof(xl_commit_ts_set, mainxid) + \
sizeof(TransactionId))
uint32 TransactionId
Definition: c.h:587
#define offsetof(type, field)
Definition: c.h:727

Definition at line 58 of file commit_ts.h.

◆ SizeOfCommitTsTruncate

#define SizeOfCommitTsTruncate
Value:
sizeof(TransactionId))
uint32 TransactionId
Definition: c.h:587
#define offsetof(type, field)
Definition: c.h:727

Definition at line 67 of file commit_ts.h.

Referenced by WriteTruncateXlogRec().

Typedef Documentation

◆ xl_commit_ts_set

◆ xl_commit_ts_truncate

Function Documentation

◆ AdvanceOldestCommitTsXid()

void AdvanceOldestCommitTsXid ( TransactionId  oldestXact)

Definition at line 904 of file commit_ts.c.

References InvalidTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), VariableCacheData::oldestCommitTsXid, ShmemVariableCache, and TransactionIdPrecedes().

Referenced by commit_ts_redo(), and vac_truncate_clog().

905 {
906  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
910  LWLockRelease(CommitTsLock);
911 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
TransactionId oldestCommitTsXid
Definition: transam.h:232
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ BootStrapCommitTs()

void BootStrapCommitTs ( void  )

Definition at line 570 of file commit_ts.c.

Referenced by BootStrapXLOG().

571 {
572  /*
573  * Nothing to do here at present, unlike most other SLRU modules; segments
574  * are created when the server is started with this module enabled. See
575  * ActivateCommitTs.
576  */
577 }

◆ CheckPointCommitTs()

void CheckPointCommitTs ( void  )

Definition at line 791 of file commit_ts.c.

References CommitTsCtl, and SimpleLruWriteAll().

Referenced by CheckPointGuts().

792 {
793  /*
794  * Write dirty CommitTs pages to disk. This may result in sync requests
795  * queued for later handling by ProcessSyncRequests(), as part of the
796  * checkpoint.
797  */
799 }
#define CommitTsCtl
Definition: commit_ts.c:80
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1156

◆ commit_ts_desc()

void commit_ts_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 22 of file committsdesc.c.

References appendStringInfo(), COMMIT_TS_TRUNCATE, COMMIT_TS_ZEROPAGE, xl_commit_ts_truncate::oldestXid, xl_commit_ts_truncate::pageno, XLogRecGetData, XLogRecGetInfo, and XLR_INFO_MASK.

23 {
24  char *rec = XLogRecGetData(record);
25  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
26 
27  if (info == COMMIT_TS_ZEROPAGE)
28  {
29  int pageno;
30 
31  memcpy(&pageno, rec, sizeof(int));
32  appendStringInfo(buf, "%d", pageno);
33  }
34  else if (info == COMMIT_TS_TRUNCATE)
35  {
37 
38  appendStringInfo(buf, "pageno %d, oldestXid %u",
39  trunc->pageno, trunc->oldestXid);
40  }
41 }
#define COMMIT_TS_ZEROPAGE
Definition: commit_ts.h:47
unsigned char uint8
Definition: c.h:439
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
TransactionId oldestXid
Definition: commit_ts.h:64
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:48
#define XLR_INFO_MASK
Definition: xlogrecord.h:62

◆ commit_ts_identify()

const char* commit_ts_identify ( uint8  info)

Definition at line 44 of file committsdesc.c.

References COMMIT_TS_TRUNCATE, and COMMIT_TS_ZEROPAGE.

45 {
46  switch (info)
47  {
48  case COMMIT_TS_ZEROPAGE:
49  return "ZEROPAGE";
50  case COMMIT_TS_TRUNCATE:
51  return "TRUNCATE";
52  default:
53  return NULL;
54  }
55 }
#define COMMIT_TS_ZEROPAGE
Definition: commit_ts.h:47
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:48

◆ commit_ts_redo()

void commit_ts_redo ( XLogReaderState record)

Definition at line 984 of file commit_ts.c.

References AdvanceOldestCommitTsXid(), Assert, COMMIT_TS_TRUNCATE, COMMIT_TS_ZEROPAGE, CommitTsCtl, elog, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), xl_commit_ts_truncate::oldestXid, xl_commit_ts_truncate::pageno, PANIC, SimpleLruTruncate(), SimpleLruWritePage(), XLogRecGetData, XLogRecGetInfo, XLogRecHasAnyBlockRefs, XLR_INFO_MASK, and ZeroCommitTsPage().

985 {
986  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
987 
988  /* Backup blocks are not used in commit_ts records */
989  Assert(!XLogRecHasAnyBlockRefs(record));
990 
991  if (info == COMMIT_TS_ZEROPAGE)
992  {
993  int pageno;
994  int slotno;
995 
996  memcpy(&pageno, XLogRecGetData(record), sizeof(int));
997 
998  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
999 
1000  slotno = ZeroCommitTsPage(pageno, false);
1002  Assert(!CommitTsCtl->shared->page_dirty[slotno]);
1003 
1004  LWLockRelease(CommitTsSLRULock);
1005  }
1006  else if (info == COMMIT_TS_TRUNCATE)
1007  {
1009 
1011 
1012  /*
1013  * During XLOG replay, latest_page_number isn't set up yet; insert a
1014  * suitable value to bypass the sanity test in SimpleLruTruncate.
1015  */
1016  CommitTsCtl->shared->latest_page_number = trunc->pageno;
1017 
1019  }
1020  else
1021  elog(PANIC, "commit_ts_redo: unknown op code %u", info);
1022 }
#define COMMIT_TS_ZEROPAGE
Definition: commit_ts.h:47
#define CommitTsCtl
Definition: commit_ts.c:80
static int ZeroCommitTsPage(int pageno, bool writeXlog)
Definition: commit_ts.c:589
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1226
unsigned char uint8
Definition: c.h:439
#define PANIC
Definition: elog.h:50
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:904
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
TransactionId oldestXid
Definition: commit_ts.h:64
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:614
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:48
#define Assert(condition)
Definition: c.h:804
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
#define elog(elevel,...)
Definition: elog.h:232
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:312

◆ CommitTsParameterChange()

void CommitTsParameterChange ( bool  newvalue,
bool  oldvalue 
)

Definition at line 638 of file commit_ts.c.

References ActivateCommitTs(), CommitTimestampShared::commitTsActive, and DeactivateCommitTs().

Referenced by xlog_redo().

639 {
640  /*
641  * If the commit_ts module is disabled in this server and we get word from
642  * the primary server that it is enabled there, activate it so that we can
643  * replay future WAL records involving it; also mark it as active on
644  * pg_control. If the old value was already set, we already did this, so
645  * don't do anything.
646  *
647  * If the module is disabled in the primary, disable it here too, unless
648  * the module is enabled locally.
649  *
650  * Note this only runs in the recovery process, so an unlocked read is
651  * fine.
652  */
653  if (newvalue)
654  {
657  }
658  else if (commitTsShared->commitTsActive)
660 }
static void ActivateCommitTs(void)
Definition: commit_ts.c:679
static void DeactivateCommitTs(void)
Definition: commit_ts.c:753
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:100

◆ CommitTsShmemBuffers()

Size CommitTsShmemBuffers ( void  )

Definition at line 515 of file commit_ts.c.

References Max, Min, and NBuffers.

Referenced by CommitTsShmemInit(), and CommitTsShmemSize().

516 {
517  return Min(16, Max(4, NBuffers / 1024));
518 }
#define Min(x, y)
Definition: c.h:986
#define Max(x, y)
Definition: c.h:980
int NBuffers
Definition: globals.c:135

◆ CommitTsShmemInit()

void CommitTsShmemInit ( void  )

Definition at line 535 of file commit_ts.c.

References Assert, COMMIT_TS_XACTS_PER_PAGE, CommitTimestampShared::commitTsActive, CommitTsCtl, CommitTsPagePrecedes(), CommitTsShmemBuffers(), CommitTimestampShared::dataLastCommit, InvalidRepOriginId, InvalidTransactionId, IsUnderPostmaster, LWTRANCHE_COMMITTS_BUFFER, CommitTimestampEntry::nodeid, ShmemInitStruct(), SimpleLruInit(), SlruPagePrecedesUnitTests, SYNC_HANDLER_COMMIT_TS, CommitTimestampEntry::time, TIMESTAMP_NOBEGIN, and CommitTimestampShared::xidLastCommit.

Referenced by CreateSharedMemoryAndSemaphores().

536 {
537  bool found;
538 
539  CommitTsCtl->PagePrecedes = CommitTsPagePrecedes;
541  CommitTsSLRULock, "pg_commit_ts",
545 
546  commitTsShared = ShmemInitStruct("CommitTs shared",
547  sizeof(CommitTimestampShared),
548  &found);
549 
550  if (!IsUnderPostmaster)
551  {
552  Assert(!found);
553 
558  }
559  else
560  Assert(found);
561 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:96
#define CommitTsCtl
Definition: commit_ts.c:80
Size CommitTsShmemBuffers(void)
Definition: commit_ts.c:515
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id, SyncRequestHandler sync_handler)
Definition: slru.c:187
#define COMMIT_TS_XACTS_PER_PAGE
Definition: commit_ts.c:67
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
bool IsUnderPostmaster
Definition: globals.c:112
#define InvalidTransactionId
Definition: transam.h:31
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
TimestampTz time
Definition: commit_ts.c:60
static bool CommitTsPagePrecedes(int page1, int page2)
Definition: commit_ts.c:938
TransactionId xidLastCommit
Definition: commit_ts.c:95
#define Assert(condition)
Definition: c.h:804
#define InvalidRepOriginId
Definition: origin.h:33
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition: slru.h:156
RepOriginId nodeid
Definition: commit_ts.c:61
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:100

◆ CommitTsShmemSize()

Size CommitTsShmemSize ( void  )

Definition at line 524 of file commit_ts.c.

References CommitTsShmemBuffers(), and SimpleLruShmemSize().

Referenced by CalculateShmemSize().

525 {
527  sizeof(CommitTimestampShared);
528 }
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:156
Size CommitTsShmemBuffers(void)
Definition: commit_ts.c:515
struct CommitTimestampShared CommitTimestampShared

◆ committssyncfiletag()

int committssyncfiletag ( const FileTag ftag,
char *  path 
)

Definition at line 1028 of file commit_ts.c.

References CommitTsCtl, and SlruSyncFileTag().

1029 {
1030  return SlruSyncFileTag(CommitTsCtl, ftag, path);
1031 }
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1593
#define CommitTsCtl
Definition: commit_ts.c:80

◆ CompleteCommitTsInitialization()

void CompleteCommitTsInitialization ( void  )

Definition at line 616 of file commit_ts.c.

References ActivateCommitTs(), DeactivateCommitTs(), and track_commit_timestamp.

Referenced by StartupXLOG().

617 {
618  /*
619  * If the feature is not enabled, turn it off for good. This also removes
620  * any leftover data.
621  *
622  * Conversely, we activate the module if the feature is enabled. This is
623  * necessary for primary and standby as the activation depends on the
624  * control file contents at the beginning of recovery or when a
625  * XLOG_PARAMETER_CHANGE is replayed.
626  */
629  else
631 }
bool track_commit_timestamp
Definition: commit_ts.c:104
static void ActivateCommitTs(void)
Definition: commit_ts.c:679
static void DeactivateCommitTs(void)
Definition: commit_ts.c:753

◆ ExtendCommitTs()

void ExtendCommitTs ( TransactionId  newestXact)

Definition at line 813 of file commit_ts.c.

References Assert, CommitTimestampShared::commitTsActive, FirstNormalTransactionId, InRecovery, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), TransactionIdEquals, TransactionIdToCTsEntry, TransactionIdToCTsPage, and ZeroCommitTsPage().

Referenced by GetNewTransactionId().

814 {
815  int pageno;
816 
817  /*
818  * Nothing to do if module not enabled. Note we do an unlocked read of
819  * the flag here, which is okay because this routine is only called from
820  * GetNewTransactionId, which is never called in a standby.
821  */
822  Assert(!InRecovery);
824  return;
825 
826  /*
827  * No work except at first XID of a page. But beware: just after
828  * wraparound, the first XID of page zero is FirstNormalTransactionId.
829  */
830  if (TransactionIdToCTsEntry(newestXact) != 0 &&
832  return;
833 
834  pageno = TransactionIdToCTsPage(newestXact);
835 
836  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
837 
838  /* Zero the page and make an XLOG entry about it */
839  ZeroCommitTsPage(pageno, !InRecovery);
840 
841  LWLockRelease(CommitTsSLRULock);
842 }
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
static int ZeroCommitTsPage(int pageno, bool writeXlog)
Definition: commit_ts.c:589
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define FirstNormalTransactionId
Definition: transam.h:34
#define TransactionIdToCTsPage(xid)
Definition: commit_ts.c:70
bool InRecovery
Definition: xlogutils.c:52
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
#define TransactionIdToCTsEntry(xid)
Definition: commit_ts.c:72
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:100

◆ GetLatestCommitTsData()

TransactionId GetLatestCommitTsData ( TimestampTz ts,
RepOriginId nodeid 
)

Definition at line 352 of file commit_ts.c.

References CommitTimestampShared::commitTsActive, CommitTimestampShared::dataLastCommit, error_commit_ts_disabled(), LW_SHARED, LWLockAcquire(), LWLockRelease(), CommitTimestampEntry::nodeid, CommitTimestampEntry::time, and CommitTimestampShared::xidLastCommit.

Referenced by pg_last_committed_xact().

353 {
354  TransactionId xid;
355 
356  LWLockAcquire(CommitTsLock, LW_SHARED);
357 
358  /* Error if module not enabled */
361 
363  if (ts)
365  if (nodeid)
367  LWLockRelease(CommitTsLock);
368 
369  return xid;
370 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:96
uint32 TransactionId
Definition: c.h:587
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
TimestampTz time
Definition: commit_ts.c:60
TransactionId xidLastCommit
Definition: commit_ts.c:95
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
RepOriginId nodeid
Definition: commit_ts.c:61
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:100
static void error_commit_ts_disabled(void)
Definition: commit_ts.c:373

◆ SetCommitTsLimit()

void SetCommitTsLimit ( TransactionId  oldestXact,
TransactionId  newestXact 
)

Definition at line 877 of file commit_ts.c.

References Assert, InvalidTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), VariableCacheData::newestCommitTsXid, VariableCacheData::oldestCommitTsXid, ShmemVariableCache, and TransactionIdPrecedes().

Referenced by BootStrapXLOG(), and StartupXLOG().

878 {
879  /*
880  * Be careful not to overwrite values that are either further into the
881  * "future" or signal a disabled committs.
882  */
883  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
885  {
890  }
891  else
892  {
896  }
897  LWLockRelease(CommitTsLock);
898 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
TransactionId oldestCommitTsXid
Definition: transam.h:232
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define Assert(condition)
Definition: c.h:804
TransactionId newestCommitTsXid
Definition: transam.h:233
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199

◆ StartupCommitTs()

void StartupCommitTs ( void  )

Definition at line 606 of file commit_ts.c.

References ActivateCommitTs().

Referenced by StartupXLOG().

607 {
609 }
static void ActivateCommitTs(void)
Definition: commit_ts.c:679

◆ TransactionIdGetCommitTsData()

bool TransactionIdGetCommitTsData ( TransactionId  xid,
TimestampTz ts,
RepOriginId nodeid 
)

Definition at line 266 of file commit_ts.c.

References Assert, CommitTimestampShared::commitTsActive, CommitTsCtl, CommitTimestampShared::dataLastCommit, ereport, errcode(), errmsg(), ERROR, error_commit_ts_disabled(), InvalidRepOriginId, LW_SHARED, LWLockAcquire(), LWLockRelease(), VariableCacheData::newestCommitTsXid, CommitTimestampEntry::nodeid, VariableCacheData::oldestCommitTsXid, ShmemVariableCache, SimpleLruReadPage_ReadOnly(), SizeOfCommitTimestampEntry, CommitTimestampEntry::time, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), TransactionIdToCTsEntry, TransactionIdToCTsPage, and CommitTimestampShared::xidLastCommit.

Referenced by pg_xact_commit_timestamp(), and pg_xact_commit_timestamp_origin().

268 {
269  int pageno = TransactionIdToCTsPage(xid);
270  int entryno = TransactionIdToCTsEntry(xid);
271  int slotno;
272  CommitTimestampEntry entry;
273  TransactionId oldestCommitTsXid;
274  TransactionId newestCommitTsXid;
275 
276  if (!TransactionIdIsValid(xid))
277  ereport(ERROR,
278  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
279  errmsg("cannot retrieve commit timestamp for transaction %u", xid)));
280  else if (!TransactionIdIsNormal(xid))
281  {
282  /* frozen and bootstrap xids are always committed far in the past */
283  *ts = 0;
284  if (nodeid)
285  *nodeid = 0;
286  return false;
287  }
288 
289  LWLockAcquire(CommitTsLock, LW_SHARED);
290 
291  /* Error if module not enabled */
294 
295  /*
296  * If we're asked for the cached value, return that. Otherwise, fall
297  * through to read from SLRU.
298  */
299  if (commitTsShared->xidLastCommit == xid)
300  {
302  if (nodeid)
304 
305  LWLockRelease(CommitTsLock);
306  return *ts != 0;
307  }
308 
309  oldestCommitTsXid = ShmemVariableCache->oldestCommitTsXid;
310  newestCommitTsXid = ShmemVariableCache->newestCommitTsXid;
311  /* neither is invalid, or both are */
312  Assert(TransactionIdIsValid(oldestCommitTsXid) == TransactionIdIsValid(newestCommitTsXid));
313  LWLockRelease(CommitTsLock);
314 
315  /*
316  * Return empty if the requested value is outside our valid range.
317  */
318  if (!TransactionIdIsValid(oldestCommitTsXid) ||
319  TransactionIdPrecedes(xid, oldestCommitTsXid) ||
320  TransactionIdPrecedes(newestCommitTsXid, xid))
321  {
322  *ts = 0;
323  if (nodeid)
324  *nodeid = InvalidRepOriginId;
325  return false;
326  }
327 
328  /* lock is acquired by SimpleLruReadPage_ReadOnly */
329  slotno = SimpleLruReadPage_ReadOnly(CommitTsCtl, pageno, xid);
330  memcpy(&entry,
331  CommitTsCtl->shared->page_buffer[slotno] +
332  SizeOfCommitTimestampEntry * entryno,
334 
335  *ts = entry.time;
336  if (nodeid)
337  *nodeid = entry.nodeid;
338 
339  LWLockRelease(CommitTsSLRULock);
340  return *ts != 0;
341 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:96
uint32 TransactionId
Definition: c.h:587
#define SizeOfCommitTimestampEntry
Definition: commit_ts.c:64
#define CommitTsCtl
Definition: commit_ts.c:80
int errcode(int sqlerrcode)
Definition: elog.c:698
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define ERROR
Definition: elog.h:46
VariableCache ShmemVariableCache
Definition: varsup.c:34
TransactionId oldestCommitTsXid
Definition: transam.h:232
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
TimestampTz time
Definition: commit_ts.c:60
#define TransactionIdToCTsPage(xid)
Definition: commit_ts.c:70
TransactionId xidLastCommit
Definition: commit_ts.c:95
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:495
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
TransactionId newestCommitTsXid
Definition: transam.h:233
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
#define TransactionIdToCTsEntry(xid)
Definition: commit_ts.c:72
#define InvalidRepOriginId
Definition: origin.h:33
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
RepOriginId nodeid
Definition: commit_ts.c:61
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:100
static void error_commit_ts_disabled(void)
Definition: commit_ts.c:373

◆ TransactionTreeSetCommitTsData()

void TransactionTreeSetCommitTsData ( TransactionId  xid,
int  nsubxids,
TransactionId subxids,
TimestampTz  timestamp,
RepOriginId  nodeid 
)

Definition at line 136 of file commit_ts.c.

References CommitTimestampShared::commitTsActive, CommitTimestampShared::dataLastCommit, i, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), VariableCacheData::newestCommitTsXid, CommitTimestampEntry::nodeid, SetXidCommitTsInPage(), ShmemVariableCache, CommitTimestampEntry::time, TransactionIdPrecedes(), TransactionIdToCTsPage, and CommitTimestampShared::xidLastCommit.

Referenced by RecordTransactionCommit(), RecordTransactionCommitPrepared(), and xact_redo_commit().

139 {
140  int i;
141  TransactionId headxid;
142  TransactionId newestXact;
143 
144  /*
145  * No-op if the module is not active.
146  *
147  * An unlocked read here is fine, because in a standby (the only place
148  * where the flag can change in flight) this routine is only called by the
149  * recovery process, which is also the only process which can change the
150  * flag.
151  */
153  return;
154 
155  /*
156  * Figure out the latest Xid in this batch: either the last subxid if
157  * there's any, otherwise the parent xid.
158  */
159  if (nsubxids > 0)
160  newestXact = subxids[nsubxids - 1];
161  else
162  newestXact = xid;
163 
164  /*
165  * We split the xids to set the timestamp to in groups belonging to the
166  * same SLRU page; the first element in each such set is its head. The
167  * first group has the main XID as the head; subsequent sets use the first
168  * subxid not on the previous page as head. This way, we only have to
169  * lock/modify each SLRU page once.
170  */
171  for (i = 0, headxid = xid;;)
172  {
173  int pageno = TransactionIdToCTsPage(headxid);
174  int j;
175 
176  for (j = i; j < nsubxids; j++)
177  {
178  if (TransactionIdToCTsPage(subxids[j]) != pageno)
179  break;
180  }
181  /* subxids[i..j] are on the same page as the head */
182 
183  SetXidCommitTsInPage(headxid, j - i, subxids + i, timestamp, nodeid,
184  pageno);
185 
186  /* if we wrote out all subxids, we're done. */
187  if (j + 1 >= nsubxids)
188  break;
189 
190  /*
191  * Set the new head and skip over it, as well as over the subxids we
192  * just wrote.
193  */
194  headxid = subxids[j];
195  i += j - i + 1;
196  }
197 
198  /* update the cached value in shared memory */
199  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
203 
204  /* and move forwards our endpoint, if needed */
207  LWLockRelease(CommitTsLock);
208 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:96
uint32 TransactionId
Definition: c.h:587
int64 timestamp
static void SetXidCommitTsInPage(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz ts, RepOriginId nodeid, int pageno)
Definition: commit_ts.c:215
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
TimestampTz time
Definition: commit_ts.c:60
#define TransactionIdToCTsPage(xid)
Definition: commit_ts.c:70
TransactionId xidLastCommit
Definition: commit_ts.c:95
TransactionId newestCommitTsXid
Definition: transam.h:233
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
int i
RepOriginId nodeid
Definition: commit_ts.c:61
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:100

◆ TruncateCommitTs()

void TruncateCommitTs ( TransactionId  oldestXact)

Definition at line 851 of file commit_ts.c.

References CommitTsCtl, SimpleLruTruncate(), SlruScanDirCbReportPresence(), SlruScanDirectory(), TransactionIdToCTsPage, and WriteTruncateXlogRec().

Referenced by vac_truncate_clog().

852 {
853  int cutoffPage;
854 
855  /*
856  * The cutoff point is the start of the segment containing oldestXact. We
857  * pass the *page* containing oldestXact to SimpleLruTruncate.
858  */
859  cutoffPage = TransactionIdToCTsPage(oldestXact);
860 
861  /* Check to see if there's any files that could be removed */
863  &cutoffPage))
864  return; /* nothing to remove */
865 
866  /* Write XLOG record */
867  WriteTruncateXlogRec(cutoffPage, oldestXact);
868 
869  /* Now we can remove the old CommitTs segment(s) */
870  SimpleLruTruncate(CommitTsCtl, cutoffPage);
871 }
#define CommitTsCtl
Definition: commit_ts.c:80
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1226
static void WriteTruncateXlogRec(int pageno, TransactionId oldestXid)
Definition: commit_ts.c:968
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1500
#define TransactionIdToCTsPage(xid)
Definition: commit_ts.c:70
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1553

Variable Documentation

◆ track_commit_timestamp

PGDLLIMPORT bool track_commit_timestamp