PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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_truncate
 

Macros

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

Typedefs

typedef struct xl_commit_ts_truncate xl_commit_ts_truncate
 

Functions

void TransactionTreeSetCommitTsData (TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz timestamp, ReplOriginId nodeid)
 
bool TransactionIdGetCommitTsData (TransactionId xid, TimestampTz *ts, ReplOriginId *nodeid)
 
TransactionId GetLatestCommitTsData (TimestampTz *ts, ReplOriginId *nodeid)
 
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 charcommit_ts_identify (uint8 info)
 

Variables

PGDLLIMPORT bool track_commit_timestamp
 

Macro Definition Documentation

◆ COMMIT_TS_TRUNCATE

#define COMMIT_TS_TRUNCATE   0x10

Definition at line 45 of file commit_ts.h.

◆ COMMIT_TS_ZEROPAGE

#define COMMIT_TS_ZEROPAGE   0x00

Definition at line 44 of file commit_ts.h.

◆ SizeOfCommitTsTruncate

#define SizeOfCommitTsTruncate
Value:
uint32 TransactionId
Definition c.h:736
static int fb(int x)

Definition at line 53 of file commit_ts.h.

Typedef Documentation

◆ xl_commit_ts_truncate

Function Documentation

◆ AdvanceOldestCommitTsXid()

void AdvanceOldestCommitTsXid ( TransactionId  oldestXact)
extern

Definition at line 919 of file commit_ts.c.

920{
926}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_EXCLUSIVE
Definition lwlock.h:104
TransactionId oldestCommitTsXid
Definition transam.h:232
#define InvalidTransactionId
Definition transam.h:31
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
TransamVariablesData * TransamVariables
Definition varsup.c:37

References fb(), InvalidTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), TransamVariablesData::oldestCommitTsXid, TransactionIdPrecedes(), and TransamVariables.

Referenced by commit_ts_redo(), and vac_truncate_clog().

◆ BootStrapCommitTs()

void BootStrapCommitTs ( void  )
extern

Definition at line 599 of file commit_ts.c.

600{
601 /*
602 * Nothing to do here at present, unlike most other SLRU modules; segments
603 * are created when the server is started with this module enabled. See
604 * ActivateCommitTs.
605 */
606}

Referenced by BootStrapXLOG().

◆ CheckPointCommitTs()

void CheckPointCommitTs ( void  )
extern

Definition at line 799 of file commit_ts.c.

800{
801 /*
802 * Write dirty CommitTs pages to disk. This may result in sync requests
803 * queued for later handling by ProcessSyncRequests(), as part of the
804 * checkpoint.
805 */
807}
#define CommitTsCtl
Definition commit_ts.c:96
void SimpleLruWriteAll(SlruDesc *ctl, bool allow_redirtied)
Definition slru.c:1372

References CommitTsCtl, and SimpleLruWriteAll().

Referenced by CheckPointGuts().

◆ commit_ts_desc()

void commit_ts_desc ( StringInfo  buf,
XLogReaderState record 
)
extern

Definition at line 21 of file committsdesc.c.

22{
23 char *rec = XLogRecGetData(record);
24 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
25
26 if (info == COMMIT_TS_ZEROPAGE)
27 {
28 int64 pageno;
29
30 memcpy(&pageno, rec, sizeof(pageno));
31 appendStringInfo(buf, "%" PRId64, pageno);
32 }
33 else if (info == COMMIT_TS_TRUNCATE)
34 {
36
37 appendStringInfo(buf, "pageno %" PRId64 ", oldestXid %u",
38 trunc->pageno, trunc->oldestXid);
39 }
40}
uint8_t uint8
Definition c.h:622
int64_t int64
Definition c.h:621
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define COMMIT_TS_ZEROPAGE
Definition commit_ts.h:44
#define COMMIT_TS_TRUNCATE
Definition commit_ts.h:45
static char buf[DEFAULT_XLOG_SEG_SIZE]
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
#define XLogRecGetInfo(decoder)
Definition xlogreader.h:410
#define XLogRecGetData(decoder)
Definition xlogreader.h:415

References appendStringInfo(), buf, COMMIT_TS_TRUNCATE, COMMIT_TS_ZEROPAGE, fb(), memcpy(), XLogRecGetData, and XLogRecGetInfo.

◆ commit_ts_identify()

const char * commit_ts_identify ( uint8  info)
extern

Definition at line 43 of file committsdesc.c.

44{
45 switch (info)
46 {
48 return "ZEROPAGE";
50 return "TRUNCATE";
51 default:
52 return NULL;
53 }
54}

References COMMIT_TS_TRUNCATE, COMMIT_TS_ZEROPAGE, and fb().

◆ commit_ts_redo()

void commit_ts_redo ( XLogReaderState record)
extern

Definition at line 995 of file commit_ts.c.

996{
997 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
998
999 /* Backup blocks are not used in commit_ts records */
1001
1002 if (info == COMMIT_TS_ZEROPAGE)
1003 {
1004 int64 pageno;
1005
1006 memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1008 }
1009 else if (info == COMMIT_TS_TRUNCATE)
1010 {
1012
1013 AdvanceOldestCommitTsXid(trunc->oldestXid);
1014
1015 /*
1016 * During XLOG replay, latest_page_number isn't set up yet; insert a
1017 * suitable value to bypass the sanity test in SimpleLruTruncate.
1018 */
1019 pg_atomic_write_u64(&CommitTsCtl->shared->latest_page_number,
1020 trunc->pageno);
1021
1023 }
1024 else
1025 elog(PANIC, "commit_ts_redo: unknown op code %u", info);
1026}
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:485
#define Assert(condition)
Definition c.h:943
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition commit_ts.c:919
#define PANIC
Definition elog.h:44
#define elog(elevel,...)
Definition elog.h:228
void SimpleLruTruncate(SlruDesc *ctl, int64 cutoffPage)
Definition slru.c:1458
void SimpleLruZeroAndWritePage(SlruDesc *ctl, int64 pageno)
Definition slru.c:466
#define XLogRecHasAnyBlockRefs(decoder)
Definition xlogreader.h:417

References AdvanceOldestCommitTsXid(), Assert, COMMIT_TS_TRUNCATE, COMMIT_TS_ZEROPAGE, CommitTsCtl, elog, fb(), memcpy(), PANIC, pg_atomic_write_u64(), SimpleLruTruncate(), SimpleLruZeroAndWritePage(), XLogRecGetData, XLogRecGetInfo, and XLogRecHasAnyBlockRefs.

◆ CommitTsParameterChange()

void CommitTsParameterChange ( bool  newvalue,
bool  oldvalue 
)
extern

Definition at line 645 of file commit_ts.c.

646{
647 /*
648 * If the commit_ts module is disabled in this server and we get word from
649 * the primary server that it is enabled there, activate it so that we can
650 * replay future WAL records involving it; also mark it as active on
651 * pg_control. If the old value was already set, we already did this, so
652 * don't do anything.
653 *
654 * If the module is disabled in the primary, disable it here too, unless
655 * the module is enabled locally.
656 *
657 * Note this only runs in the recovery process, so an unlocked read is
658 * fine.
659 */
660 if (newvalue)
661 {
664 }
667}
static void DeactivateCommitTs(void)
Definition commit_ts.c:757
static CommitTimestampShared * commitTsShared
Definition commit_ts.c:116
static void ActivateCommitTs(void)
Definition commit_ts.c:686

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

Referenced by xlog_redo().

◆ committssyncfiletag()

int committssyncfiletag ( const FileTag ftag,
char path 
)
extern

Definition at line 1032 of file commit_ts.c.

1033{
1034 return SlruSyncFileTag(CommitTsCtl, ftag, path);
1035}
int SlruSyncFileTag(SlruDesc *ctl, const FileTag *ftag, char *path)
Definition slru.c:1884

References CommitTsCtl, and SlruSyncFileTag().

◆ CompleteCommitTsInitialization()

void CompleteCommitTsInitialization ( void  )
extern

Definition at line 623 of file commit_ts.c.

624{
625 /*
626 * If the feature is not enabled, turn it off for good. This also removes
627 * any leftover data.
628 *
629 * Conversely, we activate the module if the feature is enabled. This is
630 * necessary for primary and standby as the activation depends on the
631 * control file contents at the beginning of recovery or when a
632 * XLOG_PARAMETER_CHANGE is replayed.
633 */
636 else
638}
bool track_commit_timestamp
Definition commit_ts.c:121

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

Referenced by StartupXLOG().

◆ ExtendCommitTs()

void ExtendCommitTs ( TransactionId  newestXact)
extern

Definition at line 821 of file commit_ts.c.

822{
823 int64 pageno;
824 LWLock *lock;
825
826 /*
827 * Nothing to do if module not enabled. Note we do an unlocked read of
828 * the flag here, which is okay because this routine is only called from
829 * GetNewTransactionId, which is never called in a standby.
830 */
833 return;
834
835 /*
836 * No work except at first XID of a page. But beware: just after
837 * wraparound, the first XID of page zero is FirstNormalTransactionId.
838 */
841 return;
842
844
845 lock = SimpleLruGetBankLock(CommitTsCtl, pageno);
846
848
849 /* Zero the page ... */
851
852 /* and make a WAL entry about that, unless we're in REDO */
853 if (!InRecovery)
855
856 LWLockRelease(lock);
857}
#define TransactionIdToCTsEntry(xid)
Definition commit_ts.c:78
static int64 TransactionIdToCTsPage(TransactionId xid)
Definition commit_ts.c:73
int SimpleLruZeroPage(SlruDesc *ctl, int64 pageno)
Definition slru.c:397
static LWLock * SimpleLruGetBankLock(SlruDesc *ctl, int64 pageno)
Definition slru.h:207
#define TransactionIdEquals(id1, id2)
Definition transam.h:43
#define FirstNormalTransactionId
Definition transam.h:34
XLogRecPtr XLogSimpleInsertInt64(RmgrId rmid, uint8 info, int64 value)
Definition xloginsert.c:547
bool InRecovery
Definition xlogutils.c:50

References Assert, COMMIT_TS_ZEROPAGE, CommitTimestampShared::commitTsActive, CommitTsCtl, commitTsShared, fb(), FirstNormalTransactionId, InRecovery, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SimpleLruGetBankLock(), SimpleLruZeroPage(), TransactionIdEquals, TransactionIdToCTsEntry, TransactionIdToCTsPage(), and XLogSimpleInsertInt64().

Referenced by GetNewTransactionId().

◆ GetLatestCommitTsData()

TransactionId GetLatestCommitTsData ( TimestampTz ts,
ReplOriginId nodeid 
)
extern

Definition at line 369 of file commit_ts.c.

370{
371 TransactionId xid;
372
374
375 /* Error if module not enabled */
378
380 if (ts)
382 if (nodeid)
385
386 return xid;
387}
static void error_commit_ts_disabled(void)
Definition commit_ts.c:390
@ LW_SHARED
Definition lwlock.h:105
TimestampTz time
Definition commit_ts.c:57
ReplOriginId nodeid
Definition commit_ts.c:58
CommitTimestampEntry dataLastCommit
Definition commit_ts.c:112
TransactionId xidLastCommit
Definition commit_ts.c:111

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

Referenced by pg_last_committed_xact().

◆ SetCommitTsLimit()

void SetCommitTsLimit ( TransactionId  oldestXact,
TransactionId  newestXact 
)
extern

◆ StartupCommitTs()

void StartupCommitTs ( void  )
extern

Definition at line 613 of file commit_ts.c.

614{
616}

References ActivateCommitTs().

Referenced by StartupXLOG().

◆ TransactionIdGetCommitTsData()

bool TransactionIdGetCommitTsData ( TransactionId  xid,
TimestampTz ts,
ReplOriginId nodeid 
)
extern

Definition at line 283 of file commit_ts.c.

285{
286 int64 pageno = TransactionIdToCTsPage(xid);
288 int slotno;
290 TransactionId oldestCommitTsXid;
291 TransactionId newestCommitTsXid;
292
293 if (!TransactionIdIsValid(xid))
296 errmsg("cannot retrieve commit timestamp for transaction %u", xid)));
297 else if (!TransactionIdIsNormal(xid))
298 {
299 /* frozen and bootstrap xids are always committed far in the past */
300 *ts = 0;
301 if (nodeid)
302 *nodeid = 0;
303 return false;
304 }
305
307
308 /* Error if module not enabled */
311
312 /*
313 * If we're asked for the cached value, return that. Otherwise, fall
314 * through to read from SLRU.
315 */
316 if (commitTsShared->xidLastCommit == xid)
317 {
319 if (nodeid)
321
323 return *ts != 0;
324 }
325
326 oldestCommitTsXid = TransamVariables->oldestCommitTsXid;
327 newestCommitTsXid = TransamVariables->newestCommitTsXid;
328 /* neither is invalid, or both are */
329 Assert(TransactionIdIsValid(oldestCommitTsXid) == TransactionIdIsValid(newestCommitTsXid));
331
332 /*
333 * Return empty if the requested value is outside our valid range.
334 */
335 if (!TransactionIdIsValid(oldestCommitTsXid) ||
336 TransactionIdPrecedes(xid, oldestCommitTsXid) ||
337 TransactionIdPrecedes(newestCommitTsXid, xid))
338 {
339 *ts = 0;
340 if (nodeid)
341 *nodeid = InvalidReplOriginId;
342 return false;
343 }
344
345 /* lock is acquired by SimpleLruReadPage_ReadOnly */
347 memcpy(&entry,
348 CommitTsCtl->shared->page_buffer[slotno] +
351
352 *ts = entry.time;
353 if (nodeid)
354 *nodeid = entry.nodeid;
355
357 return *ts != 0;
358}
#define SizeOfCommitTimestampEntry
Definition commit_ts.c:61
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
#define InvalidReplOriginId
Definition origin.h:33
int SimpleLruReadPage_ReadOnly(SlruDesc *ctl, int64 pageno, const void *opaque_data)
Definition slru.c:654
#define TransactionIdIsValid(xid)
Definition transam.h:41
#define TransactionIdIsNormal(xid)
Definition transam.h:42

References Assert, CommitTimestampShared::commitTsActive, CommitTsCtl, commitTsShared, CommitTimestampShared::dataLastCommit, ereport, errcode(), errmsg, ERROR, error_commit_ts_disabled(), fb(), InvalidReplOriginId, LW_SHARED, LWLockAcquire(), LWLockRelease(), memcpy(), TransamVariablesData::newestCommitTsXid, CommitTimestampEntry::nodeid, TransamVariablesData::oldestCommitTsXid, SimpleLruGetBankLock(), SimpleLruReadPage_ReadOnly(), SizeOfCommitTimestampEntry, CommitTimestampEntry::time, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), TransactionIdToCTsEntry, TransactionIdToCTsPage(), TransamVariables, and CommitTimestampShared::xidLastCommit.

Referenced by GetTupleTransactionInfo(), pg_xact_commit_timestamp(), pg_xact_commit_timestamp_origin(), and update_most_recent_deletion_info().

◆ TransactionTreeSetCommitTsData()

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

Definition at line 150 of file commit_ts.c.

153{
154 int i;
157
158 /*
159 * No-op if the module is not active.
160 *
161 * An unlocked read here is fine, because in a standby (the only place
162 * where the flag can change in flight) this routine is only called by the
163 * recovery process, which is also the only process which can change the
164 * flag.
165 */
167 return;
168
169 /*
170 * Figure out the latest Xid in this batch: either the last subxid if
171 * there's any, otherwise the parent xid.
172 */
173 if (nsubxids > 0)
174 newestXact = subxids[nsubxids - 1];
175 else
176 newestXact = xid;
177
178 /*
179 * We split the xids to set the timestamp to in groups belonging to the
180 * same SLRU page; the first element in each such set is its head. The
181 * first group has the main XID as the head; subsequent sets use the first
182 * subxid not on the previous page as head. This way, we only have to
183 * lock/modify each SLRU page once.
184 */
185 headxid = xid;
186 i = 0;
187 for (;;)
188 {
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 >= 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 + 1;
212 }
213
214 /* update the cached value in shared memory */
219
220 /* and move forwards our endpoint, if needed */
224}
static void SetXidCommitTsInPage(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz ts, ReplOriginId nodeid, int64 pageno)
Definition commit_ts.c:231
int j
Definition isn.c:78
int i
Definition isn.c:77
int64 timestamp

References CommitTimestampShared::commitTsActive, commitTsShared, CommitTimestampShared::dataLastCommit, fb(), i, j, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), TransamVariablesData::newestCommitTsXid, CommitTimestampEntry::nodeid, SetXidCommitTsInPage(), CommitTimestampEntry::time, TransactionIdPrecedes(), TransactionIdToCTsPage(), TransamVariables, and CommitTimestampShared::xidLastCommit.

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

◆ TruncateCommitTs()

void TruncateCommitTs ( TransactionId  oldestXact)
extern

Definition at line 866 of file commit_ts.c.

867{
869
870 /*
871 * The cutoff point is the start of the segment containing oldestXact. We
872 * pass the *page* containing oldestXact to SimpleLruTruncate.
873 */
875
876 /* Check to see if there's any files that could be removed */
878 &cutoffPage))
879 return; /* nothing to remove */
880
881 /* Write XLOG record */
882 WriteTruncateXlogRec(cutoffPage, oldestXact);
883
884 /* Now we can remove the old CommitTs segment(s) */
886}
static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXid)
Definition commit_ts.c:979
bool SlruScanDirectory(SlruDesc *ctl, SlruScanCallback callback, void *data)
Definition slru.c:1844
bool SlruScanDirCbReportPresence(SlruDesc *ctl, char *filename, int64 segpage, void *data)
Definition slru.c:1765

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

Referenced by vac_truncate_clog().

Variable Documentation

◆ track_commit_timestamp