PostgreSQL Source Code  git master
commit_ts.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * commit_ts.c
4  * PostgreSQL commit timestamp manager
5  *
6  * This module is a pg_xact-like system that stores the commit timestamp
7  * for each transaction.
8  *
9  * XLOG interactions: this module generates an XLOG record whenever a new
10  * CommitTs page is initialized to zeroes. Other writes of CommitTS come
11  * from recording of transaction commit in xact.c, which generates its own
12  * XLOG records for these events and will re-perform the status update on
13  * redo; so we need make no additional XLOG entry here.
14  *
15  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
16  * Portions Copyright (c) 1994, Regents of the University of California
17  *
18  * src/backend/access/transam/commit_ts.c
19  *
20  *-------------------------------------------------------------------------
21  */
22 #include "postgres.h"
23 
24 #include "access/commit_ts.h"
25 #include "access/htup_details.h"
26 #include "access/slru.h"
27 #include "access/transam.h"
28 #include "access/xloginsert.h"
29 #include "access/xlogutils.h"
30 #include "catalog/pg_type.h"
31 #include "funcapi.h"
32 #include "miscadmin.h"
33 #include "pg_trace.h"
34 #include "storage/shmem.h"
35 #include "utils/builtins.h"
36 #include "utils/snapmgr.h"
37 #include "utils/timestamp.h"
38 
39 /*
40  * Defines for CommitTs page sizes. A page is the same BLCKSZ as is used
41  * everywhere else in Postgres.
42  *
43  * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
44  * CommitTs page numbering also wraps around at
45  * 0xFFFFFFFF/COMMIT_TS_XACTS_PER_PAGE, and CommitTs segment numbering at
46  * 0xFFFFFFFF/COMMIT_TS_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need take no
47  * explicit notice of that fact in this module, except when comparing segment
48  * and page numbers in TruncateCommitTs (see CommitTsPagePrecedes).
49  */
50 
51 /*
52  * We need 8+2 bytes per xact. Note that enlarging this struct might mean
53  * the largest possible file name is more than 5 chars long; see
54  * SlruScanDirectory.
55  */
56 typedef struct CommitTimestampEntry
57 {
61 
62 #define SizeOfCommitTimestampEntry (offsetof(CommitTimestampEntry, nodeid) + \
63  sizeof(RepOriginId))
64 
65 #define COMMIT_TS_XACTS_PER_PAGE \
66  (BLCKSZ / SizeOfCommitTimestampEntry)
67 
68 #define TransactionIdToCTsPage(xid) \
69  ((xid) / (TransactionId) COMMIT_TS_XACTS_PER_PAGE)
70 #define TransactionIdToCTsEntry(xid) \
71  ((xid) % (TransactionId) COMMIT_TS_XACTS_PER_PAGE)
72 
73 /*
74  * Link to shared-memory data structures for CommitTs control
75  */
77 
78 #define CommitTsCtl (&CommitTsCtlData)
79 
80 /*
81  * We keep a cache of the last value set in shared memory.
82  *
83  * This is also good place to keep the activation status. We keep this
84  * separate from the GUC so that the standby can activate the module if the
85  * primary has it active independently of the value of the GUC.
86  *
87  * This is protected by CommitTsLock. In some places, we use commitTsActive
88  * without acquiring the lock; where this happens, a comment explains the
89  * rationale for it.
90  */
91 typedef struct CommitTimestampShared
92 {
97 
99 
100 
101 /* GUC variable */
103 
104 static void SetXidCommitTsInPage(TransactionId xid, int nsubxids,
105  TransactionId *subxids, TimestampTz ts,
106  RepOriginId nodeid, int pageno);
108  RepOriginId nodeid, int slotno);
109 static void error_commit_ts_disabled(void);
110 static int ZeroCommitTsPage(int pageno, bool writeXlog);
111 static bool CommitTsPagePrecedes(int page1, int page2);
112 static void ActivateCommitTs(void);
113 static void DeactivateCommitTs(void);
114 static void WriteZeroPageXlogRec(int pageno);
115 static void WriteTruncateXlogRec(int pageno, TransactionId oldestXid);
116 
117 /*
118  * TransactionTreeSetCommitTsData
119  *
120  * Record the final commit timestamp of transaction entries in the commit log
121  * for a transaction and its subtransaction tree, as efficiently as possible.
122  *
123  * xid is the top level transaction id.
124  *
125  * subxids is an array of xids of length nsubxids, representing subtransactions
126  * in the tree of xid. In various cases nsubxids may be zero.
127  * The reason why tracking just the parent xid commit timestamp is not enough
128  * is that the subtrans SLRU does not stay valid across crashes (it's not
129  * permanent) so we need to keep the information about them here. If the
130  * subtrans implementation changes in the future, we might want to revisit the
131  * decision of storing timestamp info for each subxid.
132  */
133 void
136  RepOriginId nodeid)
137 {
138  int i;
139  TransactionId headxid;
140  TransactionId newestXact;
141 
142  /*
143  * No-op if the module is not active.
144  *
145  * An unlocked read here is fine, because in a standby (the only place
146  * where the flag can change in flight) this routine is only called by the
147  * recovery process, which is also the only process which can change the
148  * flag.
149  */
151  return;
152 
153  /*
154  * Figure out the latest Xid in this batch: either the last subxid if
155  * there's any, otherwise the parent xid.
156  */
157  if (nsubxids > 0)
158  newestXact = subxids[nsubxids - 1];
159  else
160  newestXact = xid;
161 
162  /*
163  * We split the xids to set the timestamp to in groups belonging to the
164  * same SLRU page; the first element in each such set is its head. The
165  * first group has the main XID as the head; subsequent sets use the first
166  * subxid not on the previous page as head. This way, we only have to
167  * lock/modify each SLRU page once.
168  */
169  headxid = xid;
170  i = 0;
171  for (;;)
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 >= 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 + 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 }
209 
210 /*
211  * Record the commit timestamp of transaction entries in the commit log for all
212  * entries on a single page. Atomic only on this page.
213  */
214 static void
216  TransactionId *subxids, TimestampTz ts,
217  RepOriginId nodeid, int pageno)
218 {
219  int slotno;
220  int i;
221 
222  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
223 
224  slotno = SimpleLruReadPage(CommitTsCtl, pageno, true, xid);
225 
226  TransactionIdSetCommitTs(xid, ts, nodeid, slotno);
227  for (i = 0; i < nsubxids; i++)
228  TransactionIdSetCommitTs(subxids[i], ts, nodeid, slotno);
229 
230  CommitTsCtl->shared->page_dirty[slotno] = true;
231 
232  LWLockRelease(CommitTsSLRULock);
233 }
234 
235 /*
236  * Sets the commit timestamp of a single transaction.
237  *
238  * Must be called with CommitTsSLRULock held
239  */
240 static void
242  RepOriginId nodeid, int slotno)
243 {
244  int entryno = TransactionIdToCTsEntry(xid);
245  CommitTimestampEntry entry;
246 
248 
249  entry.time = ts;
250  entry.nodeid = nodeid;
251 
252  memcpy(CommitTsCtl->shared->page_buffer[slotno] +
253  SizeOfCommitTimestampEntry * entryno,
255 }
256 
257 /*
258  * Interrogate the commit timestamp of a transaction.
259  *
260  * The return value indicates whether a commit timestamp record was found for
261  * the given xid. The timestamp value is returned in *ts (which may not be
262  * null), and the origin node for the Xid is returned in *nodeid, if it's not
263  * null.
264  */
265 bool
267  RepOriginId *nodeid)
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 }
342 
343 /*
344  * Return the Xid of the latest committed transaction. (As far as this module
345  * is concerned, anyway; it's up to the caller to ensure the value is useful
346  * for its purposes.)
347  *
348  * ts and nodeid are filled with the corresponding data; they can be passed
349  * as NULL if not wanted.
350  */
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 }
371 
372 static void
374 {
375  ereport(ERROR,
376  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
377  errmsg("could not get commit timestamp data"),
379  errhint("Make sure the configuration parameter \"%s\" is set on the primary server.",
380  "track_commit_timestamp") :
381  errhint("Make sure the configuration parameter \"%s\" is set.",
382  "track_commit_timestamp")));
383 }
384 
385 /*
386  * SQL-callable wrapper to obtain commit time of a transaction
387  */
388 Datum
390 {
392  TimestampTz ts;
393  bool found;
394 
395  found = TransactionIdGetCommitTsData(xid, &ts, NULL);
396 
397  if (!found)
398  PG_RETURN_NULL();
399 
401 }
402 
403 
404 /*
405  * pg_last_committed_xact
406  *
407  * SQL-callable wrapper to obtain some information about the latest
408  * committed transaction: transaction ID, timestamp and replication
409  * origin.
410  */
411 Datum
413 {
414  TransactionId xid;
415  RepOriginId nodeid;
416  TimestampTz ts;
417  Datum values[3];
418  bool nulls[3];
419  TupleDesc tupdesc;
420  HeapTuple htup;
421 
422  /* and construct a tuple with our data */
423  xid = GetLatestCommitTsData(&ts, &nodeid);
424 
425  /*
426  * Construct a tuple descriptor for the result row. This must match this
427  * function's pg_proc entry!
428  */
429  tupdesc = CreateTemplateTupleDesc(3);
430  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "xid",
431  XIDOID, -1, 0);
432  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "timestamp",
433  TIMESTAMPTZOID, -1, 0);
434  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "roident",
435  OIDOID, -1, 0);
436  tupdesc = BlessTupleDesc(tupdesc);
437 
438  if (!TransactionIdIsNormal(xid))
439  {
440  memset(nulls, true, sizeof(nulls));
441  }
442  else
443  {
444  values[0] = TransactionIdGetDatum(xid);
445  nulls[0] = false;
446 
447  values[1] = TimestampTzGetDatum(ts);
448  nulls[1] = false;
449 
450  values[2] = ObjectIdGetDatum((Oid) nodeid);
451  nulls[2] = false;
452  }
453 
454  htup = heap_form_tuple(tupdesc, values, nulls);
455 
457 }
458 
459 /*
460  * pg_xact_commit_timestamp_origin
461  *
462  * SQL-callable wrapper to obtain commit timestamp and replication origin
463  * of a given transaction.
464  */
465 Datum
467 {
469  RepOriginId nodeid;
470  TimestampTz ts;
471  Datum values[2];
472  bool nulls[2];
473  TupleDesc tupdesc;
474  HeapTuple htup;
475  bool found;
476 
477  found = TransactionIdGetCommitTsData(xid, &ts, &nodeid);
478 
479  /*
480  * Construct a tuple descriptor for the result row. This must match this
481  * function's pg_proc entry!
482  */
483  tupdesc = CreateTemplateTupleDesc(2);
484  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "timestamp",
485  TIMESTAMPTZOID, -1, 0);
486  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "roident",
487  OIDOID, -1, 0);
488  tupdesc = BlessTupleDesc(tupdesc);
489 
490  if (!found)
491  {
492  memset(nulls, true, sizeof(nulls));
493  }
494  else
495  {
496  values[0] = TimestampTzGetDatum(ts);
497  nulls[0] = false;
498 
499  values[1] = ObjectIdGetDatum((Oid) nodeid);
500  nulls[1] = false;
501  }
502 
503  htup = heap_form_tuple(tupdesc, values, nulls);
504 
506 }
507 
508 /*
509  * Number of shared CommitTS buffers.
510  *
511  * We use a very similar logic as for the number of CLOG buffers (except we
512  * scale up twice as fast with shared buffers, and the maximum is twice as
513  * high); see comments in CLOGShmemBuffers.
514  */
515 Size
517 {
518  return Min(256, Max(4, NBuffers / 256));
519 }
520 
521 /*
522  * Shared memory sizing for CommitTs
523  */
524 Size
526 {
528  sizeof(CommitTimestampShared);
529 }
530 
531 /*
532  * Initialize CommitTs at system startup (postmaster start or standalone
533  * backend)
534  */
535 void
537 {
538  bool found;
539 
540  CommitTsCtl->PagePrecedes = CommitTsPagePrecedes;
542  CommitTsSLRULock, "pg_commit_ts",
546 
547  commitTsShared = ShmemInitStruct("CommitTs shared",
548  sizeof(CommitTimestampShared),
549  &found);
550 
551  if (!IsUnderPostmaster)
552  {
553  Assert(!found);
554 
559  }
560  else
561  Assert(found);
562 }
563 
564 /*
565  * This function must be called ONCE on system install.
566  *
567  * (The CommitTs directory is assumed to have been created by initdb, and
568  * CommitTsShmemInit must have been called already.)
569  */
570 void
572 {
573  /*
574  * Nothing to do here at present, unlike most other SLRU modules; segments
575  * are created when the server is started with this module enabled. See
576  * ActivateCommitTs.
577  */
578 }
579 
580 /*
581  * Initialize (or reinitialize) a page of CommitTs to zeroes.
582  * If writeXlog is true, also emit an XLOG record saying we did this.
583  *
584  * The page is not actually written, just set up in shared memory.
585  * The slot number of the new page is returned.
586  *
587  * Control lock must be held at entry, and will be held at exit.
588  */
589 static int
590 ZeroCommitTsPage(int pageno, bool writeXlog)
591 {
592  int slotno;
593 
594  slotno = SimpleLruZeroPage(CommitTsCtl, pageno);
595 
596  if (writeXlog)
597  WriteZeroPageXlogRec(pageno);
598 
599  return slotno;
600 }
601 
602 /*
603  * This must be called ONCE during postmaster or standalone-backend startup,
604  * after StartupXLOG has initialized ShmemVariableCache->nextXid.
605  */
606 void
608 {
610 }
611 
612 /*
613  * This must be called ONCE during postmaster or standalone-backend startup,
614  * after recovery has finished.
615  */
616 void
618 {
619  /*
620  * If the feature is not enabled, turn it off for good. This also removes
621  * any leftover data.
622  *
623  * Conversely, we activate the module if the feature is enabled. This is
624  * necessary for primary and standby as the activation depends on the
625  * control file contents at the beginning of recovery or when a
626  * XLOG_PARAMETER_CHANGE is replayed.
627  */
630  else
632 }
633 
634 /*
635  * Activate or deactivate CommitTs' upon reception of a XLOG_PARAMETER_CHANGE
636  * XLog record during recovery.
637  */
638 void
639 CommitTsParameterChange(bool newvalue, bool oldvalue)
640 {
641  /*
642  * If the commit_ts module is disabled in this server and we get word from
643  * the primary server that it is enabled there, activate it so that we can
644  * replay future WAL records involving it; also mark it as active on
645  * pg_control. If the old value was already set, we already did this, so
646  * don't do anything.
647  *
648  * If the module is disabled in the primary, disable it here too, unless
649  * the module is enabled locally.
650  *
651  * Note this only runs in the recovery process, so an unlocked read is
652  * fine.
653  */
654  if (newvalue)
655  {
658  }
659  else if (commitTsShared->commitTsActive)
661 }
662 
663 /*
664  * Activate this module whenever necessary.
665  * This must happen during postmaster or standalone-backend startup,
666  * or during WAL replay anytime the track_commit_timestamp setting is
667  * changed in the primary.
668  *
669  * The reason why this SLRU needs separate activation/deactivation functions is
670  * that it can be enabled/disabled during start and the activation/deactivation
671  * on the primary is propagated to the standby via replay. Other SLRUs don't
672  * have this property and they can be just initialized during normal startup.
673  *
674  * This is in charge of creating the currently active segment, if it's not
675  * already there. The reason for this is that the server might have been
676  * running with this module disabled for a while and thus might have skipped
677  * the normal creation point.
678  */
679 static void
681 {
682  TransactionId xid;
683  int pageno;
684 
685  /* If we've done this already, there's nothing to do */
686  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
688  {
689  LWLockRelease(CommitTsLock);
690  return;
691  }
692  LWLockRelease(CommitTsLock);
693 
695  pageno = TransactionIdToCTsPage(xid);
696 
697  /*
698  * Re-Initialize our idea of the latest page number.
699  */
700  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
701  CommitTsCtl->shared->latest_page_number = pageno;
702  LWLockRelease(CommitTsSLRULock);
703 
704  /*
705  * If CommitTs is enabled, but it wasn't in the previous server run, we
706  * need to set the oldest and newest values to the next Xid; that way, we
707  * will not try to read data that might not have been set.
708  *
709  * XXX does this have a problem if a server is started with commitTs
710  * enabled, then started with commitTs disabled, then restarted with it
711  * enabled again? It doesn't look like it does, because there should be a
712  * checkpoint that sets the value to InvalidTransactionId at end of
713  * recovery; and so any chance of injecting new transactions without
714  * CommitTs values would occur after the oldestCommitTsXid has been set to
715  * Invalid temporarily.
716  */
717  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
719  {
722  }
723  LWLockRelease(CommitTsLock);
724 
725  /* Create the current segment file, if necessary */
727  {
728  int slotno;
729 
730  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
731  slotno = ZeroCommitTsPage(pageno, false);
733  Assert(!CommitTsCtl->shared->page_dirty[slotno]);
734  LWLockRelease(CommitTsSLRULock);
735  }
736 
737  /* Change the activation status in shared memory. */
738  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
740  LWLockRelease(CommitTsLock);
741 }
742 
743 /*
744  * Deactivate this module.
745  *
746  * This must be called when the track_commit_timestamp parameter is turned off.
747  * This happens during postmaster or standalone-backend startup, or during WAL
748  * replay.
749  *
750  * Resets CommitTs into invalid state to make sure we don't hand back
751  * possibly-invalid data; also removes segments of old data.
752  */
753 static void
755 {
756  /*
757  * Cleanup the status in the shared memory.
758  *
759  * We reset everything in the commitTsShared record to prevent user from
760  * getting confusing data about last committed transaction on the standby
761  * when the module was activated repeatedly on the primary.
762  */
763  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
764 
769 
772 
773  LWLockRelease(CommitTsLock);
774 
775  /*
776  * Remove *all* files. This is necessary so that there are no leftover
777  * files; in the case where this feature is later enabled after running
778  * with it disabled for some time there may be a gap in the file sequence.
779  * (We can probably tolerate out-of-sequence files, as they are going to
780  * be overwritten anyway when we wrap around, but it seems better to be
781  * tidy.)
782  */
783  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
785  LWLockRelease(CommitTsSLRULock);
786 }
787 
788 /*
789  * Perform a checkpoint --- either during shutdown, or on-the-fly
790  */
791 void
793 {
794  /*
795  * Write dirty CommitTs pages to disk. This may result in sync requests
796  * queued for later handling by ProcessSyncRequests(), as part of the
797  * checkpoint.
798  */
800 }
801 
802 /*
803  * Make sure that CommitTs has room for a newly-allocated XID.
804  *
805  * NB: this is called while holding XidGenLock. We want it to be very fast
806  * most of the time; even when it's not so fast, no actual I/O need happen
807  * unless we're forced to write out a dirty CommitTs or xlog page to make room
808  * in shared memory.
809  *
810  * NB: the current implementation relies on track_commit_timestamp being
811  * PGC_POSTMASTER.
812  */
813 void
815 {
816  int pageno;
817 
818  /*
819  * Nothing to do if module not enabled. Note we do an unlocked read of
820  * the flag here, which is okay because this routine is only called from
821  * GetNewTransactionId, which is never called in a standby.
822  */
823  Assert(!InRecovery);
825  return;
826 
827  /*
828  * No work except at first XID of a page. But beware: just after
829  * wraparound, the first XID of page zero is FirstNormalTransactionId.
830  */
831  if (TransactionIdToCTsEntry(newestXact) != 0 &&
833  return;
834 
835  pageno = TransactionIdToCTsPage(newestXact);
836 
837  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
838 
839  /* Zero the page and make an XLOG entry about it */
840  ZeroCommitTsPage(pageno, !InRecovery);
841 
842  LWLockRelease(CommitTsSLRULock);
843 }
844 
845 /*
846  * Remove all CommitTs segments before the one holding the passed
847  * transaction ID.
848  *
849  * Note that we don't need to flush XLOG here.
850  */
851 void
853 {
854  int cutoffPage;
855 
856  /*
857  * The cutoff point is the start of the segment containing oldestXact. We
858  * pass the *page* containing oldestXact to SimpleLruTruncate.
859  */
860  cutoffPage = TransactionIdToCTsPage(oldestXact);
861 
862  /* Check to see if there's any files that could be removed */
864  &cutoffPage))
865  return; /* nothing to remove */
866 
867  /* Write XLOG record */
868  WriteTruncateXlogRec(cutoffPage, oldestXact);
869 
870  /* Now we can remove the old CommitTs segment(s) */
871  SimpleLruTruncate(CommitTsCtl, cutoffPage);
872 }
873 
874 /*
875  * Set the limit values between which commit TS can be consulted.
876  */
877 void
879 {
880  /*
881  * Be careful not to overwrite values that are either further into the
882  * "future" or signal a disabled committs.
883  */
884  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
886  {
891  }
892  else
893  {
897  }
898  LWLockRelease(CommitTsLock);
899 }
900 
901 /*
902  * Move forwards the oldest commitTS value that can be consulted
903  */
904 void
906 {
907  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
911  LWLockRelease(CommitTsLock);
912 }
913 
914 
915 /*
916  * Decide whether a commitTS page number is "older" for truncation purposes.
917  * Analogous to CLOGPagePrecedes().
918  *
919  * At default BLCKSZ, (1 << 31) % COMMIT_TS_XACTS_PER_PAGE == 128. This
920  * introduces differences compared to CLOG and the other SLRUs having (1 <<
921  * 31) % per_page == 0. This function never tests exactly
922  * TransactionIdPrecedes(x-2^31, x). When the system reaches xidStopLimit,
923  * there are two possible counts of page boundaries between oldestXact and the
924  * latest XID assigned, depending on whether oldestXact is within the first
925  * 128 entries of its page. Since this function doesn't know the location of
926  * oldestXact within page2, it returns false for one page that actually is
927  * expendable. This is a wider (yet still negligible) version of the
928  * truncation opportunity that CLOGPagePrecedes() cannot recognize.
929  *
930  * For the sake of a worked example, number entries with decimal values such
931  * that page1==1 entries range from 1.0 to 1.999. Let N+0.15 be the number of
932  * pages that 2^31 entries will span (N is an integer). If oldestXact=N+2.1,
933  * then the final safe XID assignment leaves newestXact=1.95. We keep page 2,
934  * because entry=2.85 is the border that toggles whether entries precede the
935  * last entry of the oldestXact page. While page 2 is expendable at
936  * oldestXact=N+2.1, it would be precious at oldestXact=N+2.9.
937  */
938 static bool
939 CommitTsPagePrecedes(int page1, int page2)
940 {
941  TransactionId xid1;
942  TransactionId xid2;
943 
944  xid1 = ((TransactionId) page1) * COMMIT_TS_XACTS_PER_PAGE;
945  xid1 += FirstNormalTransactionId + 1;
946  xid2 = ((TransactionId) page2) * COMMIT_TS_XACTS_PER_PAGE;
947  xid2 += FirstNormalTransactionId + 1;
948 
949  return (TransactionIdPrecedes(xid1, xid2) &&
951 }
952 
953 
954 /*
955  * Write a ZEROPAGE xlog record
956  */
957 static void
959 {
960  XLogBeginInsert();
961  XLogRegisterData((char *) (&pageno), sizeof(int));
962  (void) XLogInsert(RM_COMMIT_TS_ID, COMMIT_TS_ZEROPAGE);
963 }
964 
965 /*
966  * Write a TRUNCATE xlog record
967  */
968 static void
969 WriteTruncateXlogRec(int pageno, TransactionId oldestXid)
970 {
971  xl_commit_ts_truncate xlrec;
972 
973  xlrec.pageno = pageno;
974  xlrec.oldestXid = oldestXid;
975 
976  XLogBeginInsert();
977  XLogRegisterData((char *) (&xlrec), SizeOfCommitTsTruncate);
978  (void) XLogInsert(RM_COMMIT_TS_ID, COMMIT_TS_TRUNCATE);
979 }
980 
981 /*
982  * CommitTS resource manager's routines
983  */
984 void
986 {
987  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
988 
989  /* Backup blocks are not used in commit_ts records */
990  Assert(!XLogRecHasAnyBlockRefs(record));
991 
992  if (info == COMMIT_TS_ZEROPAGE)
993  {
994  int pageno;
995  int slotno;
996 
997  memcpy(&pageno, XLogRecGetData(record), sizeof(int));
998 
999  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
1000 
1001  slotno = ZeroCommitTsPage(pageno, false);
1003  Assert(!CommitTsCtl->shared->page_dirty[slotno]);
1004 
1005  LWLockRelease(CommitTsSLRULock);
1006  }
1007  else if (info == COMMIT_TS_TRUNCATE)
1008  {
1010 
1012 
1013  /*
1014  * During XLOG replay, latest_page_number isn't set up yet; insert a
1015  * suitable value to bypass the sanity test in SimpleLruTruncate.
1016  */
1017  CommitTsCtl->shared->latest_page_number = trunc->pageno;
1018 
1020  }
1021  else
1022  elog(PANIC, "commit_ts_redo: unknown op code %u", info);
1023 }
1024 
1025 /*
1026  * Entrypoint for sync.c to sync commit_ts files.
1027  */
1028 int
1029 committssyncfiletag(const FileTag *ftag, char *path)
1030 {
1031  return SlruSyncFileTag(CommitTsCtl, ftag, path);
1032 }
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define Min(x, y)
Definition: c.h:937
#define Max(x, y)
Definition: c.h:931
unsigned char uint8
Definition: c.h:440
uint32 TransactionId
Definition: c.h:588
size_t Size
Definition: c.h:541
void StartupCommitTs(void)
Definition: commit_ts.c:607
static SlruCtlData CommitTsCtlData
Definition: commit_ts.c:76
Datum pg_xact_commit_timestamp_origin(PG_FUNCTION_ARGS)
Definition: commit_ts.c:466
struct CommitTimestampEntry CommitTimestampEntry
struct CommitTimestampShared CommitTimestampShared
static void WriteZeroPageXlogRec(int pageno)
Definition: commit_ts.c:958
Datum pg_last_committed_xact(PG_FUNCTION_ARGS)
Definition: commit_ts.c:412
TransactionId GetLatestCommitTsData(TimestampTz *ts, RepOriginId *nodeid)
Definition: commit_ts.c:352
void CommitTsParameterChange(bool newvalue, bool oldvalue)
Definition: commit_ts.c:639
Size CommitTsShmemBuffers(void)
Definition: commit_ts.c:516
#define COMMIT_TS_XACTS_PER_PAGE
Definition: commit_ts.c:65
#define TransactionIdToCTsEntry(xid)
Definition: commit_ts.c:70
static void DeactivateCommitTs(void)
Definition: commit_ts.c:754
Size CommitTsShmemSize(void)
Definition: commit_ts.c:525
bool track_commit_timestamp
Definition: commit_ts.c:102
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:905
static CommitTimestampShared * commitTsShared
Definition: commit_ts.c:98
int committssyncfiletag(const FileTag *ftag, char *path)
Definition: commit_ts.c:1029
void CompleteCommitTsInitialization(void)
Definition: commit_ts.c:617
static void SetXidCommitTsInPage(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz ts, RepOriginId nodeid, int pageno)
Definition: commit_ts.c:215
static int ZeroCommitTsPage(int pageno, bool writeXlog)
Definition: commit_ts.c:590
static void ActivateCommitTs(void)
Definition: commit_ts.c:680
void TruncateCommitTs(TransactionId oldestXact)
Definition: commit_ts.c:852
void commit_ts_redo(XLogReaderState *record)
Definition: commit_ts.c:985
bool TransactionIdGetCommitTsData(TransactionId xid, TimestampTz *ts, RepOriginId *nodeid)
Definition: commit_ts.c:266
static void TransactionIdSetCommitTs(TransactionId xid, TimestampTz ts, RepOriginId nodeid, int slotno)
Definition: commit_ts.c:241
Datum pg_xact_commit_timestamp(PG_FUNCTION_ARGS)
Definition: commit_ts.c:389
static void error_commit_ts_disabled(void)
Definition: commit_ts.c:373
#define SizeOfCommitTimestampEntry
Definition: commit_ts.c:62
void BootStrapCommitTs(void)
Definition: commit_ts.c:571
static bool CommitTsPagePrecedes(int page1, int page2)
Definition: commit_ts.c:939
void CommitTsShmemInit(void)
Definition: commit_ts.c:536
void SetCommitTsLimit(TransactionId oldestXact, TransactionId newestXact)
Definition: commit_ts.c:878
#define CommitTsCtl
Definition: commit_ts.c:78
void ExtendCommitTs(TransactionId newestXact)
Definition: commit_ts.c:814
static void WriteTruncateXlogRec(int pageno, TransactionId oldestXid)
Definition: commit_ts.c:969
#define TransactionIdToCTsPage(xid)
Definition: commit_ts.c:68
void TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids, TransactionId *subxids, TimestampTz timestamp, RepOriginId nodeid)
Definition: commit_ts.c:134
void CheckPointCommitTs(void)
Definition: commit_ts.c:792
#define COMMIT_TS_ZEROPAGE
Definition: commit_ts.h:47
#define SizeOfCommitTsTruncate
Definition: commit_ts.h:67
#define COMMIT_TS_TRUNCATE
Definition: commit_ts.h:48
int64 TimestampTz
Definition: timestamp.h:39
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:151
int errhint(const char *fmt,...)
Definition: elog.c:1153
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define PANIC
Definition: elog.h:38
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_TRANSACTIONID(n)
Definition: fmgr.h:279
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
int NBuffers
Definition: globals.c:136
bool IsUnderPostmaster
Definition: globals.c:113
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
int j
Definition: isn.c:74
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1194
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1802
@ LWTRANCHE_COMMITTS_BUFFER
Definition: lwlock.h:178
@ LW_SHARED
Definition: lwlock.h:113
@ LW_EXCLUSIVE
Definition: lwlock.h:112
#define InvalidRepOriginId
Definition: origin.h:33
int64 timestamp
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:620
uintptr_t Datum
Definition: postgres.h:412
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
unsigned int Oid
Definition: postgres_ext.h:31
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:615
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1157
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1501
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1531
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:188
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1227
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1554
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1594
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:281
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:496
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno)
Definition: slru.c:627
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:396
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:156
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition: slru.h:156
TimestampTz time
Definition: commit_ts.c:58
RepOriginId nodeid
Definition: commit_ts.c:59
CommitTimestampEntry dataLastCommit
Definition: commit_ts.c:94
TransactionId xidLastCommit
Definition: commit_ts.c:93
Definition: sync.h:51
FullTransactionId nextXid
Definition: transam.h:220
TransactionId newestCommitTsXid
Definition: transam.h:233
TransactionId oldestCommitTsXid
Definition: transam.h:232
TransactionId oldestXid
Definition: commit_ts.h:64
@ SYNC_HANDLER_COMMIT_TS
Definition: sync.h:39
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:273
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:315
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define XidFromFullTransactionId(x)
Definition: transam.h:48
#define FirstNormalTransactionId
Definition: transam.h:34
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool RecoveryInProgress(void)
Definition: xlog.c:5912
uint16 RepOriginId
Definition: xlogdefs.h:65
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:351
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:451
void XLogBeginInsert(void)
Definition: xloginsert.c:150
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:411
#define XLogRecGetData(decoder)
Definition: xlogreader.h:416
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:418
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
bool InRecovery
Definition: xlogutils.c:53