PostgreSQL Source Code  git master
commit_ts.h File Reference
#include "access/xlog.h"
#include "datatype/timestamp.h"
#include "replication/origin.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 COMMIT_TS_SETTS   0x20
 
#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 write_xlog)
 
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 ShutdownCommitTs (void)
 
void CheckPointCommitTs (void)
 
void ExtendCommitTs (TransactionId newestXact)
 
void TruncateCommitTs (TransactionId oldestXact)
 
void SetCommitTsLimit (TransactionId oldestXact, TransactionId newestXact)
 
void AdvanceOldestCommitTsXid (TransactionId oldestXact)
 
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_SETTS

#define COMMIT_TS_SETTS   0x20

◆ 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:520
#define offsetof(type, field)
Definition: c.h:668

Definition at line 61 of file commit_ts.h.

Referenced by commit_ts_desc(), and commit_ts_redo().

◆ SizeOfCommitTsTruncate

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

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

937 {
938  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
942  LWLockRelease(CommitTsLock);
943 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
TransactionId oldestCommitTsXid
Definition: transam.h:190
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208

◆ BootStrapCommitTs()

void BootStrapCommitTs ( void  )

Definition at line 584 of file commit_ts.c.

Referenced by BootStrapXLOG().

585 {
586  /*
587  * Nothing to do here at present, unlike most other SLRU modules; segments
588  * are created when the server is started with this module enabled. See
589  * ActivateCommitTs.
590  */
591 }

◆ check_track_commit_timestamp()

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

◆ CheckPointCommitTs()

void CheckPointCommitTs ( void  )

Definition at line 821 of file commit_ts.c.

References CommitTsCtl, fsync_fname(), and SimpleLruFlush().

Referenced by CheckPointGuts().

822 {
823  /* Flush dirty CommitTs pages to disk */
825 
826  /*
827  * fsync pg_commit_ts to ensure that any files flushed previously are
828  * durably on disk.
829  */
830  fsync_fname("pg_commit_ts", true);
831 }
#define CommitTsCtl
Definition: commit_ts.c:79
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:632
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1128

◆ commit_ts_desc()

void commit_ts_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 22 of file committsdesc.c.

References appendStringInfo(), COMMIT_TS_SETTS, COMMIT_TS_TRUNCATE, COMMIT_TS_ZEROPAGE, i, xl_commit_ts_set::mainxid, xl_commit_ts_set::nodeid, xl_commit_ts_truncate::oldestXid, xl_commit_ts_truncate::pageno, palloc(), pfree(), SizeOfCommitTsSet, xl_commit_ts_set::timestamp, timestamptz_to_str(), XLogRecGetData, XLogRecGetDataLen, 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  else if (info == COMMIT_TS_SETTS)
42  {
43  xl_commit_ts_set *xlrec = (xl_commit_ts_set *) rec;
44  int nsubxids;
45 
46  appendStringInfo(buf, "set %s/%d for: %u",
48  xlrec->nodeid,
49  xlrec->mainxid);
50  nsubxids = ((XLogRecGetDataLen(record) - SizeOfCommitTsSet) /
51  sizeof(TransactionId));
52  if (nsubxids > 0)
53  {
54  int i;
55  TransactionId *subxids;
56 
57  subxids = palloc(sizeof(TransactionId) * nsubxids);
58  memcpy(subxids,
60  sizeof(TransactionId) * nsubxids);
61  for (i = 0; i < nsubxids; i++)
62  appendStringInfo(buf, ", %u", subxids[i]);
63  pfree(subxids);
64  }
65  }
66 }
#define COMMIT_TS_ZEROPAGE
Definition: commit_ts.h:49
#define COMMIT_TS_SETTS
Definition: commit_ts.h:51
uint32 TransactionId
Definition: c.h:520
unsigned char uint8
Definition: c.h:372
TimestampTz timestamp
Definition: commit_ts.h:55
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define XLogRecGetDataLen(decoder)
Definition: xlogreader.h:311
#define SizeOfCommitTsSet
Definition: commit_ts.h:61
TransactionId oldestXid
Definition: commit_ts.h:67
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:50
TransactionId mainxid
Definition: commit_ts.h:57
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
void * palloc(Size size)
Definition: mcxt.c:949
RepOriginId nodeid
Definition: commit_ts.h:56
int i
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1736

◆ commit_ts_identify()

const char* commit_ts_identify ( uint8  info)

Definition at line 69 of file committsdesc.c.

References COMMIT_TS_SETTS, COMMIT_TS_TRUNCATE, and COMMIT_TS_ZEROPAGE.

70 {
71  switch (info)
72  {
73  case COMMIT_TS_ZEROPAGE:
74  return "ZEROPAGE";
75  case COMMIT_TS_TRUNCATE:
76  return "TRUNCATE";
77  case COMMIT_TS_SETTS:
78  return "SETTS";
79  default:
80  return NULL;
81  }
82 }
#define COMMIT_TS_ZEROPAGE
Definition: commit_ts.h:49
#define COMMIT_TS_SETTS
Definition: commit_ts.h:51
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:50

◆ commit_ts_redo()

void commit_ts_redo ( XLogReaderState record)

Definition at line 1024 of file commit_ts.c.

References AdvanceOldestCommitTsXid(), Assert, COMMIT_TS_SETTS, COMMIT_TS_TRUNCATE, COMMIT_TS_ZEROPAGE, CommitTsCtl, elog, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), xl_commit_ts_set::mainxid, xl_commit_ts_set::nodeid, xl_commit_ts_truncate::oldestXid, xl_commit_ts_truncate::pageno, palloc(), PANIC, pfree(), SimpleLruTruncate(), SimpleLruWritePage(), SizeOfCommitTsSet, xl_commit_ts_set::timestamp, TransactionTreeSetCommitTsData(), XLogRecGetData, XLogRecGetDataLen, XLogRecGetInfo, XLogRecHasAnyBlockRefs, XLR_INFO_MASK, and ZeroCommitTsPage().

1025 {
1026  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1027 
1028  /* Backup blocks are not used in commit_ts records */
1029  Assert(!XLogRecHasAnyBlockRefs(record));
1030 
1031  if (info == COMMIT_TS_ZEROPAGE)
1032  {
1033  int pageno;
1034  int slotno;
1035 
1036  memcpy(&pageno, XLogRecGetData(record), sizeof(int));
1037 
1038  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
1039 
1040  slotno = ZeroCommitTsPage(pageno, false);
1042  Assert(!CommitTsCtl->shared->page_dirty[slotno]);
1043 
1044  LWLockRelease(CommitTsSLRULock);
1045  }
1046  else if (info == COMMIT_TS_TRUNCATE)
1047  {
1049 
1051 
1052  /*
1053  * During XLOG replay, latest_page_number isn't set up yet; insert a
1054  * suitable value to bypass the sanity test in SimpleLruTruncate.
1055  */
1056  CommitTsCtl->shared->latest_page_number = trunc->pageno;
1057 
1059  }
1060  else if (info == COMMIT_TS_SETTS)
1061  {
1062  xl_commit_ts_set *setts = (xl_commit_ts_set *) XLogRecGetData(record);
1063  int nsubxids;
1064  TransactionId *subxids;
1065 
1066  nsubxids = ((XLogRecGetDataLen(record) - SizeOfCommitTsSet) /
1067  sizeof(TransactionId));
1068  if (nsubxids > 0)
1069  {
1070  subxids = palloc(sizeof(TransactionId) * nsubxids);
1071  memcpy(subxids,
1073  sizeof(TransactionId) * nsubxids);
1074  }
1075  else
1076  subxids = NULL;
1077 
1078  TransactionTreeSetCommitTsData(setts->mainxid, nsubxids, subxids,
1079  setts->timestamp, setts->nodeid, true);
1080  if (subxids)
1081  pfree(subxids);
1082  }
1083  else
1084  elog(PANIC, "commit_ts_redo: unknown op code %u", info);
1085 }
#define COMMIT_TS_ZEROPAGE
Definition: commit_ts.h:49
#define COMMIT_TS_SETTS
Definition: commit_ts.h:51
uint32 TransactionId
Definition: c.h:520
#define CommitTsCtl
Definition: commit_ts.c:79
static int ZeroCommitTsPage(int pageno, bool writeXlog)
Definition: commit_ts.c:603
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1196
unsigned char uint8
Definition: c.h:372
#define PANIC
Definition: elog.h:53
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:936
TimestampTz timestamp
Definition: commit_ts.h:55
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
#define XLogRecGetDataLen(decoder)
Definition: xlogreader.h:311
#define SizeOfCommitTsSet
Definition: commit_ts.h:61
TransactionId oldestXid
Definition: commit_ts.h:67
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:597
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:50
TransactionId mainxid
Definition: commit_ts.h:57
#define Assert(condition)
Definition: c.h:745
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
void * palloc(Size size)
Definition: mcxt.c:949
RepOriginId nodeid
Definition: commit_ts.h:56
#define elog(elevel,...)
Definition: elog.h:214
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:312
void TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz timestamp, RepOriginId nodeid, bool write_xlog)
Definition: commit_ts.c:145

◆ CommitTsParameterChange()

void CommitTsParameterChange ( bool  newvalue,
bool  oldvalue 
)

Definition at line 652 of file commit_ts.c.

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

Referenced by xlog_redo().

653 {
654  /*
655  * If the commit_ts module is disabled in this server and we get word from
656  * the primary server that it is enabled there, activate it so that we can
657  * replay future WAL records involving it; also mark it as active on
658  * pg_control. If the old value was already set, we already did this, so
659  * don't do anything.
660  *
661  * If the module is disabled in the primary, disable it here too, unless
662  * the module is enabled locally.
663  *
664  * Note this only runs in the recovery process, so an unlocked read is
665  * fine.
666  */
667  if (newvalue)
668  {
671  }
672  else if (commitTsShared->commitTsActive)
674 }
static void ActivateCommitTs(void)
Definition: commit_ts.c:693
static void DeactivateCommitTs(void)
Definition: commit_ts.c:767
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:99

◆ CommitTsShmemBuffers()

Size CommitTsShmemBuffers ( void  )

Definition at line 531 of file commit_ts.c.

References Max, Min, and NBuffers.

Referenced by CommitTsShmemInit(), and CommitTsShmemSize().

532 {
533  return Min(16, Max(4, NBuffers / 1024));
534 }
#define Min(x, y)
Definition: c.h:927
#define Max(x, y)
Definition: c.h:921
int NBuffers
Definition: globals.c:132

◆ CommitTsShmemInit()

void CommitTsShmemInit ( void  )

Definition at line 551 of file commit_ts.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

552 {
553  bool found;
554 
555  CommitTsCtl->PagePrecedes = CommitTsPagePrecedes;
557  CommitTsSLRULock, "pg_commit_ts",
559 
560  commitTsShared = ShmemInitStruct("CommitTs shared",
561  sizeof(CommitTimestampShared),
562  &found);
563 
564  if (!IsUnderPostmaster)
565  {
566  Assert(!found);
567 
572  }
573  else
574  Assert(found);
575 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:95
#define CommitTsCtl
Definition: commit_ts.c:79
Size CommitTsShmemBuffers(void)
Definition: commit_ts.c:531
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
bool IsUnderPostmaster
Definition: globals.c:109
#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:957
TransactionId xidLastCommit
Definition: commit_ts.c:94
#define Assert(condition)
Definition: c.h:745
#define InvalidRepOriginId
Definition: origin.h:33
RepOriginId nodeid
Definition: commit_ts.c:60
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:99
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id)
Definition: slru.c:175

◆ CommitTsShmemSize()

Size CommitTsShmemSize ( void  )

Definition at line 540 of file commit_ts.c.

References CommitTsShmemBuffers(), and SimpleLruShmemSize().

Referenced by CreateSharedMemoryAndSemaphores().

541 {
543  sizeof(CommitTimestampShared);
544 }
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:144
Size CommitTsShmemBuffers(void)
Definition: commit_ts.c:531
struct CommitTimestampShared CommitTimestampShared

◆ CompleteCommitTsInitialization()

void CompleteCommitTsInitialization ( void  )

Definition at line 630 of file commit_ts.c.

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

Referenced by StartupXLOG().

631 {
632  /*
633  * If the feature is not enabled, turn it off for good. This also removes
634  * any leftover data.
635  *
636  * Conversely, we activate the module if the feature is enabled. This is
637  * necessary for primary and standby as the activation depends on the
638  * control file contents at the beginning of recovery or when a
639  * XLOG_PARAMETER_CHANGE is replayed.
640  */
643  else
645 }
bool track_commit_timestamp
Definition: commit_ts.c:103
static void ActivateCommitTs(void)
Definition: commit_ts.c:693
static void DeactivateCommitTs(void)
Definition: commit_ts.c:767

◆ ExtendCommitTs()

void ExtendCommitTs ( TransactionId  newestXact)

Definition at line 845 of file commit_ts.c.

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

Referenced by GetNewTransactionId().

846 {
847  int pageno;
848 
849  /*
850  * Nothing to do if module not enabled. Note we do an unlocked read of
851  * the flag here, which is okay because this routine is only called from
852  * GetNewTransactionId, which is never called in a standby.
853  */
854  Assert(!InRecovery);
856  return;
857 
858  /*
859  * No work except at first XID of a page. But beware: just after
860  * wraparound, the first XID of page zero is FirstNormalTransactionId.
861  */
862  if (TransactionIdToCTsEntry(newestXact) != 0 &&
864  return;
865 
866  pageno = TransactionIdToCTsPage(newestXact);
867 
868  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
869 
870  /* Zero the page and make an XLOG entry about it */
871  ZeroCommitTsPage(pageno, !InRecovery);
872 
873  LWLockRelease(CommitTsSLRULock);
874 }
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
static int ZeroCommitTsPage(int pageno, bool writeXlog)
Definition: commit_ts.c:603
bool InRecovery
Definition: xlog.c:204
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define FirstNormalTransactionId
Definition: transam.h:34
#define TransactionIdToCTsPage(xid)
Definition: commit_ts.c:69
#define Assert(condition)
Definition: c.h:745
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#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 368 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().

369 {
370  TransactionId xid;
371 
372  LWLockAcquire(CommitTsLock, LW_SHARED);
373 
374  /* Error if module not enabled */
377 
379  if (ts)
381  if (nodeid)
383  LWLockRelease(CommitTsLock);
384 
385  return xid;
386 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:95
uint32 TransactionId
Definition: c.h:520
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
TimestampTz time
Definition: commit_ts.c:59
TransactionId xidLastCommit
Definition: commit_ts.c:94
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
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:389

◆ SetCommitTsLimit()

void SetCommitTsLimit ( TransactionId  oldestXact,
TransactionId  newestXact 
)

Definition at line 909 of file commit_ts.c.

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

Referenced by BootStrapXLOG(), and StartupXLOG().

910 {
911  /*
912  * Be careful not to overwrite values that are either further into the
913  * "future" or signal a disabled committs.
914  */
915  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
917  {
922  }
923  else
924  {
928  }
929  LWLockRelease(CommitTsLock);
930 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
TransactionId oldestCommitTsXid
Definition: transam.h:190
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define Assert(condition)
Definition: c.h:745
TransactionId newestCommitTsXid
Definition: transam.h:191
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208

◆ ShutdownCommitTs()

void ShutdownCommitTs ( void  )

Definition at line 805 of file commit_ts.c.

References CommitTsCtl, fsync_fname(), and SimpleLruFlush().

Referenced by ShutdownXLOG().

806 {
807  /* Flush dirty CommitTs pages to disk */
808  SimpleLruFlush(CommitTsCtl, false);
809 
810  /*
811  * fsync pg_commit_ts to ensure that any files flushed previously are
812  * durably on disk.
813  */
814  fsync_fname("pg_commit_ts", true);
815 }
#define CommitTsCtl
Definition: commit_ts.c:79
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:632
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1128

◆ StartupCommitTs()

void StartupCommitTs ( void  )

Definition at line 620 of file commit_ts.c.

References ActivateCommitTs().

Referenced by StartupXLOG().

621 {
623 }
static void ActivateCommitTs(void)
Definition: commit_ts.c:693

◆ TransactionIdGetCommitTsData()

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

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

284 {
285  int pageno = TransactionIdToCTsPage(xid);
286  int entryno = TransactionIdToCTsEntry(xid);
287  int slotno;
288  CommitTimestampEntry entry;
289  TransactionId oldestCommitTsXid;
290  TransactionId newestCommitTsXid;
291 
292  if (!TransactionIdIsValid(xid))
293  ereport(ERROR,
294  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
295  errmsg("cannot retrieve commit timestamp for transaction %u", xid)));
296  else if (!TransactionIdIsNormal(xid))
297  {
298  /* frozen and bootstrap xids are always committed far in the past */
299  *ts = 0;
300  if (nodeid)
301  *nodeid = 0;
302  return false;
303  }
304 
305  LWLockAcquire(CommitTsLock, LW_SHARED);
306 
307  /* Error if module not enabled */
310 
311  /*
312  * If we're asked for the cached value, return that. Otherwise, fall
313  * through to read from SLRU.
314  */
315  if (commitTsShared->xidLastCommit == xid)
316  {
318  if (nodeid)
320 
321  LWLockRelease(CommitTsLock);
322  return *ts != 0;
323  }
324 
325  oldestCommitTsXid = ShmemVariableCache->oldestCommitTsXid;
326  newestCommitTsXid = ShmemVariableCache->newestCommitTsXid;
327  /* neither is invalid, or both are */
328  Assert(TransactionIdIsValid(oldestCommitTsXid) == TransactionIdIsValid(newestCommitTsXid));
329  LWLockRelease(CommitTsLock);
330 
331  /*
332  * Return empty if the requested value is outside our valid range.
333  */
334  if (!TransactionIdIsValid(oldestCommitTsXid) ||
335  TransactionIdPrecedes(xid, oldestCommitTsXid) ||
336  TransactionIdPrecedes(newestCommitTsXid, xid))
337  {
338  *ts = 0;
339  if (nodeid)
340  *nodeid = InvalidRepOriginId;
341  return false;
342  }
343 
344  /* lock is acquired by SimpleLruReadPage_ReadOnly */
345  slotno = SimpleLruReadPage_ReadOnly(CommitTsCtl, pageno, xid);
346  memcpy(&entry,
347  CommitTsCtl->shared->page_buffer[slotno] +
348  SizeOfCommitTimestampEntry * entryno,
350 
351  *ts = entry.time;
352  if (nodeid)
353  *nodeid = entry.nodeid;
354 
355  LWLockRelease(CommitTsSLRULock);
356  return *ts != 0;
357 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:95
uint32 TransactionId
Definition: c.h:520
#define SizeOfCommitTimestampEntry
Definition: commit_ts.c:63
#define CommitTsCtl
Definition: commit_ts.c:79
int errcode(int sqlerrcode)
Definition: elog.c:610
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define ERROR
Definition: elog.h:43
VariableCache ShmemVariableCache
Definition: varsup.c:34
TransactionId oldestCommitTsXid
Definition: transam.h:190
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:482
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
TransactionId newestCommitTsXid
Definition: transam.h:191
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define TransactionIdToCTsEntry(xid)
Definition: commit_ts.c:71
#define InvalidRepOriginId
Definition: origin.h:33
int errmsg(const char *fmt,...)
Definition: elog.c:824
#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:389

◆ TransactionTreeSetCommitTsData()

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

Definition at line 145 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, WriteSetTimestampXlogRec(), and CommitTimestampShared::xidLastCommit.

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

148 {
149  int i;
150  TransactionId headxid;
151  TransactionId newestXact;
152 
153  /*
154  * No-op if the module is not active.
155  *
156  * An unlocked read here is fine, because in a standby (the only place
157  * where the flag can change in flight) this routine is only called by the
158  * recovery process, which is also the only process which can change the
159  * flag.
160  */
162  return;
163 
164  /*
165  * Comply with the WAL-before-data rule: if caller specified it wants this
166  * value to be recorded in WAL, do so before touching the data.
167  */
168  if (write_xlog)
169  WriteSetTimestampXlogRec(xid, nsubxids, subxids, timestamp, nodeid);
170 
171  /*
172  * Figure out the latest Xid in this batch: either the last subxid if
173  * there's any, otherwise the parent xid.
174  */
175  if (nsubxids > 0)
176  newestXact = subxids[nsubxids - 1];
177  else
178  newestXact = xid;
179 
180  /*
181  * We split the xids to set the timestamp to in groups belonging to the
182  * same SLRU page; the first element in each such set is its head. The
183  * first group has the main XID as the head; subsequent sets use the first
184  * subxid not on the previous page as head. This way, we only have to
185  * lock/modify each SLRU page once.
186  */
187  for (i = 0, headxid = xid;;)
188  {
189  int pageno = TransactionIdToCTsPage(headxid);
190  int j;
191 
192  for (j = i; j < nsubxids; j++)
193  {
194  if (TransactionIdToCTsPage(subxids[j]) != pageno)
195  break;
196  }
197  /* subxids[i..j] are on the same page as the head */
198 
199  SetXidCommitTsInPage(headxid, j - i, subxids + i, timestamp, nodeid,
200  pageno);
201 
202  /* if we wrote out all subxids, we're done. */
203  if (j + 1 >= nsubxids)
204  break;
205 
206  /*
207  * Set the new head and skip over it, as well as over the subxids we
208  * just wrote.
209  */
210  headxid = subxids[j];
211  i += j - i + 1;
212  }
213 
214  /* update the cached value in shared memory */
215  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
219 
220  /* and move forwards our endpoint, if needed */
223  LWLockRelease(CommitTsLock);
224 }
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:95
uint32 TransactionId
Definition: c.h:520
static void WriteSetTimestampXlogRec(TransactionId mainxid, int nsubxids, TransactionId *subxids, TimestampTz timestamp, RepOriginId nodeid)
Definition: commit_ts.c:1002
int64 timestamp
static void SetXidCommitTsInPage(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz ts, RepOriginId nodeid, int pageno)
Definition: commit_ts.c:231
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
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:191
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
int i
RepOriginId nodeid
Definition: commit_ts.c:60
CommitTimestampShared * commitTsShared
Definition: commit_ts.c:99

◆ TruncateCommitTs()

void TruncateCommitTs ( TransactionId  oldestXact)

Definition at line 883 of file commit_ts.c.

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

Referenced by vac_truncate_clog().

884 {
885  int cutoffPage;
886 
887  /*
888  * The cutoff point is the start of the segment containing oldestXact. We
889  * pass the *page* containing oldestXact to SimpleLruTruncate.
890  */
891  cutoffPage = TransactionIdToCTsPage(oldestXact);
892 
893  /* Check to see if there's any files that could be removed */
895  &cutoffPage))
896  return; /* nothing to remove */
897 
898  /* Write XLOG record */
899  WriteTruncateXlogRec(cutoffPage, oldestXact);
900 
901  /* Now we can remove the old CommitTs segment(s) */
902  SimpleLruTruncate(CommitTsCtl, cutoffPage);
903 }
#define CommitTsCtl
Definition: commit_ts.c:79
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1196
static void WriteTruncateXlogRec(int pageno, TransactionId oldestXid)
Definition: commit_ts.c:986
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1352
#define TransactionIdToCTsPage(xid)
Definition: commit_ts.c:69
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1407

Variable Documentation

◆ track_commit_timestamp

PGDLLIMPORT bool track_commit_timestamp