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-2023, 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  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
426  elog(ERROR, "return type must be a row type");
427 
428  if (!TransactionIdIsNormal(xid))
429  {
430  memset(nulls, true, sizeof(nulls));
431  }
432  else
433  {
434  values[0] = TransactionIdGetDatum(xid);
435  nulls[0] = false;
436 
437  values[1] = TimestampTzGetDatum(ts);
438  nulls[1] = false;
439 
440  values[2] = ObjectIdGetDatum((Oid) nodeid);
441  nulls[2] = false;
442  }
443 
444  htup = heap_form_tuple(tupdesc, values, nulls);
445 
447 }
448 
449 /*
450  * pg_xact_commit_timestamp_origin
451  *
452  * SQL-callable wrapper to obtain commit timestamp and replication origin
453  * of a given transaction.
454  */
455 Datum
457 {
459  RepOriginId nodeid;
460  TimestampTz ts;
461  Datum values[2];
462  bool nulls[2];
463  TupleDesc tupdesc;
464  HeapTuple htup;
465  bool found;
466 
467  found = TransactionIdGetCommitTsData(xid, &ts, &nodeid);
468 
469  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
470  elog(ERROR, "return type must be a row type");
471 
472  if (!found)
473  {
474  memset(nulls, true, sizeof(nulls));
475  }
476  else
477  {
478  values[0] = TimestampTzGetDatum(ts);
479  nulls[0] = false;
480 
481  values[1] = ObjectIdGetDatum((Oid) nodeid);
482  nulls[1] = false;
483  }
484 
485  htup = heap_form_tuple(tupdesc, values, nulls);
486 
488 }
489 
490 /*
491  * Number of shared CommitTS buffers.
492  *
493  * We use a very similar logic as for the number of CLOG buffers (except we
494  * scale up twice as fast with shared buffers, and the maximum is twice as
495  * high); see comments in CLOGShmemBuffers.
496  */
497 Size
499 {
500  return Min(256, Max(4, NBuffers / 256));
501 }
502 
503 /*
504  * Shared memory sizing for CommitTs
505  */
506 Size
508 {
510  sizeof(CommitTimestampShared);
511 }
512 
513 /*
514  * Initialize CommitTs at system startup (postmaster start or standalone
515  * backend)
516  */
517 void
519 {
520  bool found;
521 
522  CommitTsCtl->PagePrecedes = CommitTsPagePrecedes;
524  CommitTsSLRULock, "pg_commit_ts",
528 
529  commitTsShared = ShmemInitStruct("CommitTs shared",
530  sizeof(CommitTimestampShared),
531  &found);
532 
533  if (!IsUnderPostmaster)
534  {
535  Assert(!found);
536 
541  }
542  else
543  Assert(found);
544 }
545 
546 /*
547  * This function must be called ONCE on system install.
548  *
549  * (The CommitTs directory is assumed to have been created by initdb, and
550  * CommitTsShmemInit must have been called already.)
551  */
552 void
554 {
555  /*
556  * Nothing to do here at present, unlike most other SLRU modules; segments
557  * are created when the server is started with this module enabled. See
558  * ActivateCommitTs.
559  */
560 }
561 
562 /*
563  * Initialize (or reinitialize) a page of CommitTs to zeroes.
564  * If writeXlog is true, also emit an XLOG record saying we did this.
565  *
566  * The page is not actually written, just set up in shared memory.
567  * The slot number of the new page is returned.
568  *
569  * Control lock must be held at entry, and will be held at exit.
570  */
571 static int
572 ZeroCommitTsPage(int pageno, bool writeXlog)
573 {
574  int slotno;
575 
576  slotno = SimpleLruZeroPage(CommitTsCtl, pageno);
577 
578  if (writeXlog)
579  WriteZeroPageXlogRec(pageno);
580 
581  return slotno;
582 }
583 
584 /*
585  * This must be called ONCE during postmaster or standalone-backend startup,
586  * after StartupXLOG has initialized ShmemVariableCache->nextXid.
587  */
588 void
590 {
592 }
593 
594 /*
595  * This must be called ONCE during postmaster or standalone-backend startup,
596  * after recovery has finished.
597  */
598 void
600 {
601  /*
602  * If the feature is not enabled, turn it off for good. This also removes
603  * any leftover data.
604  *
605  * Conversely, we activate the module if the feature is enabled. This is
606  * necessary for primary and standby as the activation depends on the
607  * control file contents at the beginning of recovery or when a
608  * XLOG_PARAMETER_CHANGE is replayed.
609  */
612  else
614 }
615 
616 /*
617  * Activate or deactivate CommitTs' upon reception of a XLOG_PARAMETER_CHANGE
618  * XLog record during recovery.
619  */
620 void
621 CommitTsParameterChange(bool newvalue, bool oldvalue)
622 {
623  /*
624  * If the commit_ts module is disabled in this server and we get word from
625  * the primary server that it is enabled there, activate it so that we can
626  * replay future WAL records involving it; also mark it as active on
627  * pg_control. If the old value was already set, we already did this, so
628  * don't do anything.
629  *
630  * If the module is disabled in the primary, disable it here too, unless
631  * the module is enabled locally.
632  *
633  * Note this only runs in the recovery process, so an unlocked read is
634  * fine.
635  */
636  if (newvalue)
637  {
640  }
641  else if (commitTsShared->commitTsActive)
643 }
644 
645 /*
646  * Activate this module whenever necessary.
647  * This must happen during postmaster or standalone-backend startup,
648  * or during WAL replay anytime the track_commit_timestamp setting is
649  * changed in the primary.
650  *
651  * The reason why this SLRU needs separate activation/deactivation functions is
652  * that it can be enabled/disabled during start and the activation/deactivation
653  * on the primary is propagated to the standby via replay. Other SLRUs don't
654  * have this property and they can be just initialized during normal startup.
655  *
656  * This is in charge of creating the currently active segment, if it's not
657  * already there. The reason for this is that the server might have been
658  * running with this module disabled for a while and thus might have skipped
659  * the normal creation point.
660  */
661 static void
663 {
664  TransactionId xid;
665  int pageno;
666 
667  /* If we've done this already, there's nothing to do */
668  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
670  {
671  LWLockRelease(CommitTsLock);
672  return;
673  }
674  LWLockRelease(CommitTsLock);
675 
677  pageno = TransactionIdToCTsPage(xid);
678 
679  /*
680  * Re-Initialize our idea of the latest page number.
681  */
682  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
683  CommitTsCtl->shared->latest_page_number = pageno;
684  LWLockRelease(CommitTsSLRULock);
685 
686  /*
687  * If CommitTs is enabled, but it wasn't in the previous server run, we
688  * need to set the oldest and newest values to the next Xid; that way, we
689  * will not try to read data that might not have been set.
690  *
691  * XXX does this have a problem if a server is started with commitTs
692  * enabled, then started with commitTs disabled, then restarted with it
693  * enabled again? It doesn't look like it does, because there should be a
694  * checkpoint that sets the value to InvalidTransactionId at end of
695  * recovery; and so any chance of injecting new transactions without
696  * CommitTs values would occur after the oldestCommitTsXid has been set to
697  * Invalid temporarily.
698  */
699  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
701  {
704  }
705  LWLockRelease(CommitTsLock);
706 
707  /* Create the current segment file, if necessary */
709  {
710  int slotno;
711 
712  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
713  slotno = ZeroCommitTsPage(pageno, false);
715  Assert(!CommitTsCtl->shared->page_dirty[slotno]);
716  LWLockRelease(CommitTsSLRULock);
717  }
718 
719  /* Change the activation status in shared memory. */
720  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
722  LWLockRelease(CommitTsLock);
723 }
724 
725 /*
726  * Deactivate this module.
727  *
728  * This must be called when the track_commit_timestamp parameter is turned off.
729  * This happens during postmaster or standalone-backend startup, or during WAL
730  * replay.
731  *
732  * Resets CommitTs into invalid state to make sure we don't hand back
733  * possibly-invalid data; also removes segments of old data.
734  */
735 static void
737 {
738  /*
739  * Cleanup the status in the shared memory.
740  *
741  * We reset everything in the commitTsShared record to prevent user from
742  * getting confusing data about last committed transaction on the standby
743  * when the module was activated repeatedly on the primary.
744  */
745  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
746 
751 
754 
755  LWLockRelease(CommitTsLock);
756 
757  /*
758  * Remove *all* files. This is necessary so that there are no leftover
759  * files; in the case where this feature is later enabled after running
760  * with it disabled for some time there may be a gap in the file sequence.
761  * (We can probably tolerate out-of-sequence files, as they are going to
762  * be overwritten anyway when we wrap around, but it seems better to be
763  * tidy.)
764  */
765  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
767  LWLockRelease(CommitTsSLRULock);
768 }
769 
770 /*
771  * Perform a checkpoint --- either during shutdown, or on-the-fly
772  */
773 void
775 {
776  /*
777  * Write dirty CommitTs pages to disk. This may result in sync requests
778  * queued for later handling by ProcessSyncRequests(), as part of the
779  * checkpoint.
780  */
782 }
783 
784 /*
785  * Make sure that CommitTs has room for a newly-allocated XID.
786  *
787  * NB: this is called while holding XidGenLock. We want it to be very fast
788  * most of the time; even when it's not so fast, no actual I/O need happen
789  * unless we're forced to write out a dirty CommitTs or xlog page to make room
790  * in shared memory.
791  *
792  * NB: the current implementation relies on track_commit_timestamp being
793  * PGC_POSTMASTER.
794  */
795 void
797 {
798  int pageno;
799 
800  /*
801  * Nothing to do if module not enabled. Note we do an unlocked read of
802  * the flag here, which is okay because this routine is only called from
803  * GetNewTransactionId, which is never called in a standby.
804  */
805  Assert(!InRecovery);
807  return;
808 
809  /*
810  * No work except at first XID of a page. But beware: just after
811  * wraparound, the first XID of page zero is FirstNormalTransactionId.
812  */
813  if (TransactionIdToCTsEntry(newestXact) != 0 &&
815  return;
816 
817  pageno = TransactionIdToCTsPage(newestXact);
818 
819  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
820 
821  /* Zero the page and make an XLOG entry about it */
822  ZeroCommitTsPage(pageno, !InRecovery);
823 
824  LWLockRelease(CommitTsSLRULock);
825 }
826 
827 /*
828  * Remove all CommitTs segments before the one holding the passed
829  * transaction ID.
830  *
831  * Note that we don't need to flush XLOG here.
832  */
833 void
835 {
836  int cutoffPage;
837 
838  /*
839  * The cutoff point is the start of the segment containing oldestXact. We
840  * pass the *page* containing oldestXact to SimpleLruTruncate.
841  */
842  cutoffPage = TransactionIdToCTsPage(oldestXact);
843 
844  /* Check to see if there's any files that could be removed */
846  &cutoffPage))
847  return; /* nothing to remove */
848 
849  /* Write XLOG record */
850  WriteTruncateXlogRec(cutoffPage, oldestXact);
851 
852  /* Now we can remove the old CommitTs segment(s) */
853  SimpleLruTruncate(CommitTsCtl, cutoffPage);
854 }
855 
856 /*
857  * Set the limit values between which commit TS can be consulted.
858  */
859 void
861 {
862  /*
863  * Be careful not to overwrite values that are either further into the
864  * "future" or signal a disabled committs.
865  */
866  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
868  {
873  }
874  else
875  {
879  }
880  LWLockRelease(CommitTsLock);
881 }
882 
883 /*
884  * Move forwards the oldest commitTS value that can be consulted
885  */
886 void
888 {
889  LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
893  LWLockRelease(CommitTsLock);
894 }
895 
896 
897 /*
898  * Decide whether a commitTS page number is "older" for truncation purposes.
899  * Analogous to CLOGPagePrecedes().
900  *
901  * At default BLCKSZ, (1 << 31) % COMMIT_TS_XACTS_PER_PAGE == 128. This
902  * introduces differences compared to CLOG and the other SLRUs having (1 <<
903  * 31) % per_page == 0. This function never tests exactly
904  * TransactionIdPrecedes(x-2^31, x). When the system reaches xidStopLimit,
905  * there are two possible counts of page boundaries between oldestXact and the
906  * latest XID assigned, depending on whether oldestXact is within the first
907  * 128 entries of its page. Since this function doesn't know the location of
908  * oldestXact within page2, it returns false for one page that actually is
909  * expendable. This is a wider (yet still negligible) version of the
910  * truncation opportunity that CLOGPagePrecedes() cannot recognize.
911  *
912  * For the sake of a worked example, number entries with decimal values such
913  * that page1==1 entries range from 1.0 to 1.999. Let N+0.15 be the number of
914  * pages that 2^31 entries will span (N is an integer). If oldestXact=N+2.1,
915  * then the final safe XID assignment leaves newestXact=1.95. We keep page 2,
916  * because entry=2.85 is the border that toggles whether entries precede the
917  * last entry of the oldestXact page. While page 2 is expendable at
918  * oldestXact=N+2.1, it would be precious at oldestXact=N+2.9.
919  */
920 static bool
921 CommitTsPagePrecedes(int page1, int page2)
922 {
923  TransactionId xid1;
924  TransactionId xid2;
925 
926  xid1 = ((TransactionId) page1) * COMMIT_TS_XACTS_PER_PAGE;
927  xid1 += FirstNormalTransactionId + 1;
928  xid2 = ((TransactionId) page2) * COMMIT_TS_XACTS_PER_PAGE;
929  xid2 += FirstNormalTransactionId + 1;
930 
931  return (TransactionIdPrecedes(xid1, xid2) &&
933 }
934 
935 
936 /*
937  * Write a ZEROPAGE xlog record
938  */
939 static void
941 {
942  XLogBeginInsert();
943  XLogRegisterData((char *) (&pageno), sizeof(int));
944  (void) XLogInsert(RM_COMMIT_TS_ID, COMMIT_TS_ZEROPAGE);
945 }
946 
947 /*
948  * Write a TRUNCATE xlog record
949  */
950 static void
951 WriteTruncateXlogRec(int pageno, TransactionId oldestXid)
952 {
953  xl_commit_ts_truncate xlrec;
954 
955  xlrec.pageno = pageno;
956  xlrec.oldestXid = oldestXid;
957 
958  XLogBeginInsert();
959  XLogRegisterData((char *) (&xlrec), SizeOfCommitTsTruncate);
960  (void) XLogInsert(RM_COMMIT_TS_ID, COMMIT_TS_TRUNCATE);
961 }
962 
963 /*
964  * CommitTS resource manager's routines
965  */
966 void
968 {
969  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
970 
971  /* Backup blocks are not used in commit_ts records */
972  Assert(!XLogRecHasAnyBlockRefs(record));
973 
974  if (info == COMMIT_TS_ZEROPAGE)
975  {
976  int pageno;
977  int slotno;
978 
979  memcpy(&pageno, XLogRecGetData(record), sizeof(int));
980 
981  LWLockAcquire(CommitTsSLRULock, LW_EXCLUSIVE);
982 
983  slotno = ZeroCommitTsPage(pageno, false);
985  Assert(!CommitTsCtl->shared->page_dirty[slotno]);
986 
987  LWLockRelease(CommitTsSLRULock);
988  }
989  else if (info == COMMIT_TS_TRUNCATE)
990  {
992 
994 
995  /*
996  * During XLOG replay, latest_page_number isn't set up yet; insert a
997  * suitable value to bypass the sanity test in SimpleLruTruncate.
998  */
999  CommitTsCtl->shared->latest_page_number = trunc->pageno;
1000 
1002  }
1003  else
1004  elog(PANIC, "commit_ts_redo: unknown op code %u", info);
1005 }
1006 
1007 /*
1008  * Entrypoint for sync.c to sync commit_ts files.
1009  */
1010 int
1011 committssyncfiletag(const FileTag *ftag, char *path)
1012 {
1013  return SlruSyncFileTag(CommitTsCtl, ftag, path);
1014 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define Min(x, y)
Definition: c.h:988
#define Max(x, y)
Definition: c.h:982
unsigned char uint8
Definition: c.h:488
uint32 TransactionId
Definition: c.h:636
size_t Size
Definition: c.h:589
void StartupCommitTs(void)
Definition: commit_ts.c:589
static SlruCtlData CommitTsCtlData
Definition: commit_ts.c:76
Datum pg_xact_commit_timestamp_origin(PG_FUNCTION_ARGS)
Definition: commit_ts.c:456
struct CommitTimestampEntry CommitTimestampEntry
struct CommitTimestampShared CommitTimestampShared
static void WriteZeroPageXlogRec(int pageno)
Definition: commit_ts.c:940
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:621
Size CommitTsShmemBuffers(void)
Definition: commit_ts.c:498
#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:736
Size CommitTsShmemSize(void)
Definition: commit_ts.c:507
bool track_commit_timestamp
Definition: commit_ts.c:102
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:887
static CommitTimestampShared * commitTsShared
Definition: commit_ts.c:98
int committssyncfiletag(const FileTag *ftag, char *path)
Definition: commit_ts.c:1011
void CompleteCommitTsInitialization(void)
Definition: commit_ts.c:599
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:572
static void ActivateCommitTs(void)
Definition: commit_ts.c:662
void TruncateCommitTs(TransactionId oldestXact)
Definition: commit_ts.c:834
void commit_ts_redo(XLogReaderState *record)
Definition: commit_ts.c:967
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:553
static bool CommitTsPagePrecedes(int page1, int page2)
Definition: commit_ts.c:921
void CommitTsShmemInit(void)
Definition: commit_ts.c:518
void SetCommitTsLimit(TransactionId oldestXact, TransactionId newestXact)
Definition: commit_ts.c:860
#define CommitTsCtl
Definition: commit_ts.c:78
void ExtendCommitTs(TransactionId newestXact)
Definition: commit_ts.c:796
static void WriteTruncateXlogRec(int pageno, TransactionId oldestXid)
Definition: commit_ts.c:951
#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:774
#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:158
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define PANIC
Definition: elog.h:42
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#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
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
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:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LWTRANCHE_COMMITTS_BUFFER
Definition: lwlock.h:182
@ LW_SHARED
Definition: lwlock.h:117
@ LW_EXCLUSIVE
Definition: lwlock.h:116
#define InvalidRepOriginId
Definition: origin.h:33
int64 timestamp
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:272
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
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:280
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
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:5921
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:461
void XLogBeginInsert(void)
Definition: xloginsert.c:150
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetData(decoder)
Definition: xlogreader.h:415
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:417
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
bool InRecovery
Definition: xlogutils.c:53