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 "utils/guc.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

bool check_track_commit_timestamp (bool *newval, void **extra, GucSource source)
 
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 62 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 71 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 903 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().

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

◆ BootStrapCommitTs()

void BootStrapCommitTs ( void  )

Definition at line 569 of file commit_ts.c.

Referenced by BootStrapXLOG().

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

◆ check_track_commit_timestamp()

bool check_track_commit_timestamp ( bool newval,
void **  extra,
GucSource  source 
)

◆ CheckPointCommitTs()

void CheckPointCommitTs ( void  )

Definition at line 790 of file commit_ts.c.

References CommitTsCtl, and SimpleLruWriteAll().

Referenced by CheckPointGuts().

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

◆ 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:51
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:68
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:52
#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:51
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:52

◆ commit_ts_redo()

void commit_ts_redo ( XLogReaderState record)

Definition at line 983 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().

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

◆ CommitTsParameterChange()

void CommitTsParameterChange ( bool  newvalue,
bool  oldvalue 
)

Definition at line 637 of file commit_ts.c.

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

Referenced by xlog_redo().

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

◆ CommitTsShmemBuffers()

Size CommitTsShmemBuffers ( void  )

Definition at line 514 of file commit_ts.c.

References Max, Min, and NBuffers.

Referenced by CommitTsShmemInit(), and CommitTsShmemSize().

515 {
516  return Min(16, Max(4, NBuffers / 1024));
517 }
#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 534 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().

535 {
536  bool found;
537 
538  CommitTsCtl->PagePrecedes = CommitTsPagePrecedes;
540  CommitTsSLRULock, "pg_commit_ts",
544 
545  commitTsShared = ShmemInitStruct("CommitTs shared",
546  sizeof(CommitTimestampShared),
547  &found);
548 
549  if (!IsUnderPostmaster)
550  {
551  Assert(!found);
552 
557  }
558  else
559  Assert(found);
560 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:95
#define CommitTsCtl
Definition: commit_ts.c:79
Size CommitTsShmemBuffers(void)
Definition: commit_ts.c:514
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:186
#define COMMIT_TS_XACTS_PER_PAGE
Definition: commit_ts.c:66
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:59
static bool CommitTsPagePrecedes(int page1, int page2)
Definition: commit_ts.c:937
TransactionId xidLastCommit
Definition: commit_ts.c:94
#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:60
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:99

◆ CommitTsShmemSize()

Size CommitTsShmemSize ( void  )

Definition at line 523 of file commit_ts.c.

References CommitTsShmemBuffers(), and SimpleLruShmemSize().

Referenced by CreateSharedMemoryAndSemaphores().

524 {
526  sizeof(CommitTimestampShared);
527 }
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:155
Size CommitTsShmemBuffers(void)
Definition: commit_ts.c:514
struct CommitTimestampShared CommitTimestampShared

◆ committssyncfiletag()

int committssyncfiletag ( const FileTag ftag,
char *  path 
)

Definition at line 1027 of file commit_ts.c.

References CommitTsCtl, and SlruSyncFileTag().

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

◆ CompleteCommitTsInitialization()

void CompleteCommitTsInitialization ( void  )

Definition at line 615 of file commit_ts.c.

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

Referenced by StartupXLOG().

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

◆ ExtendCommitTs()

void ExtendCommitTs ( TransactionId  newestXact)

Definition at line 812 of file commit_ts.c.

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

Referenced by GetNewTransactionId().

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

◆ GetLatestCommitTsData()

TransactionId GetLatestCommitTsData ( TimestampTz ts,
RepOriginId nodeid 
)

Definition at line 351 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().

352 {
353  TransactionId xid;
354 
355  LWLockAcquire(CommitTsLock, LW_SHARED);
356 
357  /* Error if module not enabled */
360 
362  if (ts)
364  if (nodeid)
366  LWLockRelease(CommitTsLock);
367 
368  return xid;
369 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:95
uint32 TransactionId
Definition: c.h:587
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
TimestampTz time
Definition: commit_ts.c:59
TransactionId xidLastCommit
Definition: commit_ts.c:94
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
RepOriginId nodeid
Definition: commit_ts.c:60
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:99
static void error_commit_ts_disabled(void)
Definition: commit_ts.c:372

◆ SetCommitTsLimit()

void SetCommitTsLimit ( TransactionId  oldestXact,
TransactionId  newestXact 
)

Definition at line 876 of file commit_ts.c.

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

Referenced by BootStrapXLOG(), and StartupXLOG().

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

◆ StartupCommitTs()

void StartupCommitTs ( void  )

Definition at line 605 of file commit_ts.c.

References ActivateCommitTs().

Referenced by StartupXLOG().

606 {
608 }
static void ActivateCommitTs(void)
Definition: commit_ts.c:678

◆ TransactionIdGetCommitTsData()

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

Definition at line 265 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().

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

◆ TransactionTreeSetCommitTsData()

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

Definition at line 135 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().

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

◆ TruncateCommitTs()

void TruncateCommitTs ( TransactionId  oldestXact)

Definition at line 850 of file commit_ts.c.

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

Referenced by vac_truncate_clog().

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

Variable Documentation

◆ track_commit_timestamp

PGDLLIMPORT bool track_commit_timestamp