PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
clog.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * clog.c
4  * PostgreSQL transaction-commit-log manager
5  *
6  * This module replaces the old "pg_log" access code, which treated pg_log
7  * essentially like a relation, in that it went through the regular buffer
8  * manager. The problem with that was that there wasn't any good way to
9  * recycle storage space for transactions so old that they'll never be
10  * looked up again. Now we use specialized access code so that the commit
11  * log can be broken into relatively small, independent segments.
12  *
13  * XLOG interactions: this module generates an XLOG record whenever a new
14  * CLOG page is initialized to zeroes. Other writes of CLOG come from
15  * recording of transaction commit or abort in xact.c, which generates its
16  * own XLOG records for these events and will re-perform the status update
17  * on redo; so we need make no additional XLOG entry here. For synchronous
18  * transaction commits, the XLOG is guaranteed flushed through the XLOG commit
19  * record before we are called to log a commit, so the WAL rule "write xlog
20  * before data" is satisfied automatically. However, for async commits we
21  * must track the latest LSN affecting each CLOG page, so that we can flush
22  * XLOG that far and satisfy the WAL rule. We don't have to worry about this
23  * for aborts (whether sync or async), since the post-crash assumption would
24  * be that such transactions failed anyway.
25  *
26  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
27  * Portions Copyright (c) 1994, Regents of the University of California
28  *
29  * src/backend/access/transam/clog.c
30  *
31  *-------------------------------------------------------------------------
32  */
33 #include "postgres.h"
34 
35 #include "access/clog.h"
36 #include "access/slru.h"
37 #include "access/transam.h"
38 #include "access/xlog.h"
39 #include "access/xloginsert.h"
40 #include "access/xlogutils.h"
41 #include "miscadmin.h"
42 #include "pg_trace.h"
43 
44 /*
45  * Defines for CLOG page sizes. A page is the same BLCKSZ as is used
46  * everywhere else in Postgres.
47  *
48  * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
49  * CLOG page numbering also wraps around at 0xFFFFFFFF/CLOG_XACTS_PER_PAGE,
50  * and CLOG segment numbering at
51  * 0xFFFFFFFF/CLOG_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need take no
52  * explicit notice of that fact in this module, except when comparing segment
53  * and page numbers in TruncateCLOG (see CLOGPagePrecedes).
54  */
55 
56 /* We need two bits per xact, so four xacts fit in a byte */
57 #define CLOG_BITS_PER_XACT 2
58 #define CLOG_XACTS_PER_BYTE 4
59 #define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE)
60 #define CLOG_XACT_BITMASK ((1 << CLOG_BITS_PER_XACT) - 1)
61 
62 #define TransactionIdToPage(xid) ((xid) / (TransactionId) CLOG_XACTS_PER_PAGE)
63 #define TransactionIdToPgIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE)
64 #define TransactionIdToByte(xid) (TransactionIdToPgIndex(xid) / CLOG_XACTS_PER_BYTE)
65 #define TransactionIdToBIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_BYTE)
66 
67 /* We store the latest async LSN for each group of transactions */
68 #define CLOG_XACTS_PER_LSN_GROUP 32 /* keep this a power of 2 */
69 #define CLOG_LSNS_PER_PAGE (CLOG_XACTS_PER_PAGE / CLOG_XACTS_PER_LSN_GROUP)
70 
71 #define GetLSNIndex(slotno, xid) ((slotno) * CLOG_LSNS_PER_PAGE + \
72  ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE) / CLOG_XACTS_PER_LSN_GROUP)
73 
74 
75 /*
76  * Link to shared-memory data structures for CLOG control
77  */
79 
80 #define ClogCtl (&ClogCtlData)
81 
82 
83 static int ZeroCLOGPage(int pageno, bool writeXlog);
84 static bool CLOGPagePrecedes(int page1, int page2);
85 static void WriteZeroPageXlogRec(int pageno);
86 static void WriteTruncateXlogRec(int pageno);
87 static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
88  TransactionId *subxids, XidStatus status,
89  XLogRecPtr lsn, int pageno);
91  XLogRecPtr lsn, int slotno);
92 static void set_status_by_pages(int nsubxids, TransactionId *subxids,
94 
95 
96 /*
97  * TransactionIdSetTreeStatus
98  *
99  * Record the final state of transaction entries in the commit log for
100  * a transaction and its subtransaction tree. Take care to ensure this is
101  * efficient, and as atomic as possible.
102  *
103  * xid is a single xid to set status for. This will typically be
104  * the top level transactionid for a top level commit or abort. It can
105  * also be a subtransaction when we record transaction aborts.
106  *
107  * subxids is an array of xids of length nsubxids, representing subtransactions
108  * in the tree of xid. In various cases nsubxids may be zero.
109  *
110  * lsn must be the WAL location of the commit record when recording an async
111  * commit. For a synchronous commit it can be InvalidXLogRecPtr, since the
112  * caller guarantees the commit record is already flushed in that case. It
113  * should be InvalidXLogRecPtr for abort cases, too.
114  *
115  * In the commit case, atomicity is limited by whether all the subxids are in
116  * the same CLOG page as xid. If they all are, then the lock will be grabbed
117  * only once, and the status will be set to committed directly. Otherwise
118  * we must
119  * 1. set sub-committed all subxids that are not on the same page as the
120  * main xid
121  * 2. atomically set committed the main xid and the subxids on the same page
122  * 3. go over the first bunch again and set them committed
123  * Note that as far as concurrent checkers are concerned, main transaction
124  * commit as a whole is still atomic.
125  *
126  * Example:
127  * TransactionId t commits and has subxids t1, t2, t3, t4
128  * t is on page p1, t1 is also on p1, t2 and t3 are on p2, t4 is on p3
129  * 1. update pages2-3:
130  * page2: set t2,t3 as sub-committed
131  * page3: set t4 as sub-committed
132  * 2. update page1:
133  * set t1 as sub-committed,
134  * then set t as committed,
135  then set t1 as committed
136  * 3. update pages2-3:
137  * page2: set t2,t3 as committed
138  * page3: set t4 as committed
139  *
140  * NB: this is a low-level routine and is NOT the preferred entry point
141  * for most uses; functions in transam.c are the intended callers.
142  *
143  * XXX Think about issuing FADVISE_WILLNEED on pages that we will need,
144  * but aren't yet in cache, as well as hinting pages not to fall out of
145  * cache yet.
146  */
147 void
149  TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
150 {
151  int pageno = TransactionIdToPage(xid); /* get page of parent */
152  int i;
153 
155  status == TRANSACTION_STATUS_ABORTED);
156 
157  /*
158  * See how many subxids, if any, are on the same page as the parent, if
159  * any.
160  */
161  for (i = 0; i < nsubxids; i++)
162  {
163  if (TransactionIdToPage(subxids[i]) != pageno)
164  break;
165  }
166 
167  /*
168  * Do all items fit on a single page?
169  */
170  if (i == nsubxids)
171  {
172  /*
173  * Set the parent and all subtransactions in a single call
174  */
175  TransactionIdSetPageStatus(xid, nsubxids, subxids, status, lsn,
176  pageno);
177  }
178  else
179  {
180  int nsubxids_on_first_page = i;
181 
182  /*
183  * If this is a commit then we care about doing this correctly (i.e.
184  * using the subcommitted intermediate status). By here, we know
185  * we're updating more than one page of clog, so we must mark entries
186  * that are *not* on the first page so that they show as subcommitted
187  * before we then return to update the status to fully committed.
188  *
189  * To avoid touching the first page twice, skip marking subcommitted
190  * for the subxids on that first page.
191  */
192  if (status == TRANSACTION_STATUS_COMMITTED)
193  set_status_by_pages(nsubxids - nsubxids_on_first_page,
194  subxids + nsubxids_on_first_page,
196 
197  /*
198  * Now set the parent and subtransactions on same page as the parent,
199  * if any
200  */
201  pageno = TransactionIdToPage(xid);
202  TransactionIdSetPageStatus(xid, nsubxids_on_first_page, subxids, status,
203  lsn, pageno);
204 
205  /*
206  * Now work through the rest of the subxids one clog page at a time,
207  * starting from the second page onwards, like we did above.
208  */
209  set_status_by_pages(nsubxids - nsubxids_on_first_page,
210  subxids + nsubxids_on_first_page,
211  status, lsn);
212  }
213 }
214 
215 /*
216  * Helper for TransactionIdSetTreeStatus: set the status for a bunch of
217  * transactions, chunking in the separate CLOG pages involved. We never
218  * pass the whole transaction tree to this function, only subtransactions
219  * that are on different pages to the top level transaction id.
220  */
221 static void
222 set_status_by_pages(int nsubxids, TransactionId *subxids,
224 {
225  int pageno = TransactionIdToPage(subxids[0]);
226  int offset = 0;
227  int i = 0;
228 
229  while (i < nsubxids)
230  {
231  int num_on_page = 0;
232 
233  while (TransactionIdToPage(subxids[i]) == pageno && i < nsubxids)
234  {
235  num_on_page++;
236  i++;
237  }
238 
240  num_on_page, subxids + offset,
241  status, lsn, pageno);
242  offset = i;
243  pageno = TransactionIdToPage(subxids[offset]);
244  }
245 }
246 
247 /*
248  * Record the final state of transaction entries in the commit log for
249  * all entries on a single page. Atomic only on this page.
250  *
251  * Otherwise API is same as TransactionIdSetTreeStatus()
252  */
253 static void
255  TransactionId *subxids, XidStatus status,
256  XLogRecPtr lsn, int pageno)
257 {
258  int slotno;
259  int i;
260 
262  status == TRANSACTION_STATUS_ABORTED ||
264 
265  LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
266 
267  /*
268  * If we're doing an async commit (ie, lsn is valid), then we must wait
269  * for any active write on the page slot to complete. Otherwise our
270  * update could reach disk in that write, which will not do since we
271  * mustn't let it reach disk until we've done the appropriate WAL flush.
272  * But when lsn is invalid, it's OK to scribble on a page while it is
273  * write-busy, since we don't care if the update reaches disk sooner than
274  * we think.
275  */
276  slotno = SimpleLruReadPage(ClogCtl, pageno, XLogRecPtrIsInvalid(lsn), xid);
277 
278  /*
279  * Set the main transaction id, if any.
280  *
281  * If we update more than one xid on this page while it is being written
282  * out, we might find that some of the bits go to disk and others don't.
283  * If we are updating commits on the page with the top-level xid that
284  * could break atomicity, so we subcommit the subxids first before we mark
285  * the top-level commit.
286  */
287  if (TransactionIdIsValid(xid))
288  {
289  /* Subtransactions first, if needed ... */
290  if (status == TRANSACTION_STATUS_COMMITTED)
291  {
292  for (i = 0; i < nsubxids; i++)
293  {
294  Assert(ClogCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
295  TransactionIdSetStatusBit(subxids[i],
297  lsn, slotno);
298  }
299  }
300 
301  /* ... then the main transaction */
302  TransactionIdSetStatusBit(xid, status, lsn, slotno);
303  }
304 
305  /* Set the subtransactions */
306  for (i = 0; i < nsubxids; i++)
307  {
308  Assert(ClogCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
309  TransactionIdSetStatusBit(subxids[i], status, lsn, slotno);
310  }
311 
312  ClogCtl->shared->page_dirty[slotno] = true;
313 
314  LWLockRelease(CLogControlLock);
315 }
316 
317 /*
318  * Sets the commit status of a single transaction.
319  *
320  * Must be called with CLogControlLock held
321  */
322 static void
324 {
325  int byteno = TransactionIdToByte(xid);
326  int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
327  char *byteptr;
328  char byteval;
329  char curval;
330 
331  byteptr = ClogCtl->shared->page_buffer[slotno] + byteno;
332  curval = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
333 
334  /*
335  * When replaying transactions during recovery we still need to perform
336  * the two phases of subcommit and then commit. However, some transactions
337  * are already correctly marked, so we just treat those as a no-op which
338  * allows us to keep the following Assert as restrictive as possible.
339  */
340  if (InRecovery && status == TRANSACTION_STATUS_SUB_COMMITTED &&
342  return;
343 
344  /*
345  * Current state change should be from 0 or subcommitted to target state
346  * or we should already be there when replaying changes during recovery.
347  */
348  Assert(curval == 0 ||
350  status != TRANSACTION_STATUS_IN_PROGRESS) ||
351  curval == status);
352 
353  /* note this assumes exclusive access to the clog page */
354  byteval = *byteptr;
355  byteval &= ~(((1 << CLOG_BITS_PER_XACT) - 1) << bshift);
356  byteval |= (status << bshift);
357  *byteptr = byteval;
358 
359  /*
360  * Update the group LSN if the transaction completion LSN is higher.
361  *
362  * Note: lsn will be invalid when supplied during InRecovery processing,
363  * so we don't need to do anything special to avoid LSN updates during
364  * recovery. After recovery completes the next clog change will set the
365  * LSN correctly.
366  */
367  if (!XLogRecPtrIsInvalid(lsn))
368  {
369  int lsnindex = GetLSNIndex(slotno, xid);
370 
371  if (ClogCtl->shared->group_lsn[lsnindex] < lsn)
372  ClogCtl->shared->group_lsn[lsnindex] = lsn;
373  }
374 }
375 
376 /*
377  * Interrogate the state of a transaction in the commit log.
378  *
379  * Aside from the actual commit status, this function returns (into *lsn)
380  * an LSN that is late enough to be able to guarantee that if we flush up to
381  * that LSN then we will have flushed the transaction's commit record to disk.
382  * The result is not necessarily the exact LSN of the transaction's commit
383  * record! For example, for long-past transactions (those whose clog pages
384  * already migrated to disk), we'll return InvalidXLogRecPtr. Also, because
385  * we group transactions on the same clog page to conserve storage, we might
386  * return the LSN of a later transaction that falls into the same group.
387  *
388  * NB: this is a low-level routine and is NOT the preferred entry point
389  * for most uses; TransactionLogFetch() in transam.c is the intended caller.
390  */
391 XidStatus
393 {
394  int pageno = TransactionIdToPage(xid);
395  int byteno = TransactionIdToByte(xid);
396  int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
397  int slotno;
398  int lsnindex;
399  char *byteptr;
401 
402  /* lock is acquired by SimpleLruReadPage_ReadOnly */
403 
404  slotno = SimpleLruReadPage_ReadOnly(ClogCtl, pageno, xid);
405  byteptr = ClogCtl->shared->page_buffer[slotno] + byteno;
406 
407  status = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
408 
409  lsnindex = GetLSNIndex(slotno, xid);
410  *lsn = ClogCtl->shared->group_lsn[lsnindex];
411 
412  LWLockRelease(CLogControlLock);
413 
414  return status;
415 }
416 
417 /*
418  * Number of shared CLOG buffers.
419  *
420  * On larger multi-processor systems, it is possible to have many CLOG page
421  * requests in flight at one time which could lead to disk access for CLOG
422  * page if the required page is not found in memory. Testing revealed that we
423  * can get the best performance by having 128 CLOG buffers, more than that it
424  * doesn't improve performance.
425  *
426  * Unconditionally keeping the number of CLOG buffers to 128 did not seem like
427  * a good idea, because it would increase the minimum amount of shared memory
428  * required to start, which could be a problem for people running very small
429  * configurations. The following formula seems to represent a reasonable
430  * compromise: people with very low values for shared_buffers will get fewer
431  * CLOG buffers as well, and everyone else will get 128.
432  */
433 Size
435 {
436  return Min(128, Max(4, NBuffers / 512));
437 }
438 
439 /*
440  * Initialization of shared memory for CLOG
441  */
442 Size
444 {
446 }
447 
448 void
450 {
451  ClogCtl->PagePrecedes = CLOGPagePrecedes;
453  CLogControlLock, "pg_clog", LWTRANCHE_CLOG_BUFFERS);
454 }
455 
456 /*
457  * This func must be called ONCE on system install. It creates
458  * the initial CLOG segment. (The CLOG directory is assumed to
459  * have been created by initdb, and CLOGShmemInit must have been
460  * called already.)
461  */
462 void
464 {
465  int slotno;
466 
467  LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
468 
469  /* Create and zero the first page of the commit log */
470  slotno = ZeroCLOGPage(0, false);
471 
472  /* Make sure it's written out */
473  SimpleLruWritePage(ClogCtl, slotno);
474  Assert(!ClogCtl->shared->page_dirty[slotno]);
475 
476  LWLockRelease(CLogControlLock);
477 }
478 
479 /*
480  * Initialize (or reinitialize) a page of CLOG to zeroes.
481  * If writeXlog is TRUE, also emit an XLOG record saying we did this.
482  *
483  * The page is not actually written, just set up in shared memory.
484  * The slot number of the new page is returned.
485  *
486  * Control lock must be held at entry, and will be held at exit.
487  */
488 static int
489 ZeroCLOGPage(int pageno, bool writeXlog)
490 {
491  int slotno;
492 
493  slotno = SimpleLruZeroPage(ClogCtl, pageno);
494 
495  if (writeXlog)
496  WriteZeroPageXlogRec(pageno);
497 
498  return slotno;
499 }
500 
501 /*
502  * This must be called ONCE during postmaster or standalone-backend startup,
503  * after StartupXLOG has initialized ShmemVariableCache->nextXid.
504  */
505 void
507 {
509  int pageno = TransactionIdToPage(xid);
510 
511  LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
512 
513  /*
514  * Initialize our idea of the latest page number.
515  */
516  ClogCtl->shared->latest_page_number = pageno;
517 
518  LWLockRelease(CLogControlLock);
519 }
520 
521 /*
522  * This must be called ONCE at the end of startup/recovery.
523  */
524 void
525 TrimCLOG(void)
526 {
528  int pageno = TransactionIdToPage(xid);
529 
530  LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
531 
532  /*
533  * Re-Initialize our idea of the latest page number.
534  */
535  ClogCtl->shared->latest_page_number = pageno;
536 
537  /*
538  * Zero out the remainder of the current clog page. Under normal
539  * circumstances it should be zeroes already, but it seems at least
540  * theoretically possible that XLOG replay will have settled on a nextXID
541  * value that is less than the last XID actually used and marked by the
542  * previous database lifecycle (since subtransaction commit writes clog
543  * but makes no WAL entry). Let's just be safe. (We need not worry about
544  * pages beyond the current one, since those will be zeroed when first
545  * used. For the same reason, there is no need to do anything when
546  * nextXid is exactly at a page boundary; and it's likely that the
547  * "current" page doesn't exist yet in that case.)
548  */
549  if (TransactionIdToPgIndex(xid) != 0)
550  {
551  int byteno = TransactionIdToByte(xid);
552  int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
553  int slotno;
554  char *byteptr;
555 
556  slotno = SimpleLruReadPage(ClogCtl, pageno, false, xid);
557  byteptr = ClogCtl->shared->page_buffer[slotno] + byteno;
558 
559  /* Zero so-far-unused positions in the current byte */
560  *byteptr &= (1 << bshift) - 1;
561  /* Zero the rest of the page */
562  MemSet(byteptr + 1, 0, BLCKSZ - byteno - 1);
563 
564  ClogCtl->shared->page_dirty[slotno] = true;
565  }
566 
567  LWLockRelease(CLogControlLock);
568 }
569 
570 /*
571  * This must be called ONCE during postmaster or standalone-backend shutdown
572  */
573 void
575 {
576  /* Flush dirty CLOG pages to disk */
577  TRACE_POSTGRESQL_CLOG_CHECKPOINT_START(false);
578  SimpleLruFlush(ClogCtl, false);
579  TRACE_POSTGRESQL_CLOG_CHECKPOINT_DONE(false);
580 }
581 
582 /*
583  * Perform a checkpoint --- either during shutdown, or on-the-fly
584  */
585 void
587 {
588  /* Flush dirty CLOG pages to disk */
589  TRACE_POSTGRESQL_CLOG_CHECKPOINT_START(true);
590  SimpleLruFlush(ClogCtl, true);
591  TRACE_POSTGRESQL_CLOG_CHECKPOINT_DONE(true);
592 }
593 
594 
595 /*
596  * Make sure that CLOG has room for a newly-allocated XID.
597  *
598  * NB: this is called while holding XidGenLock. We want it to be very fast
599  * most of the time; even when it's not so fast, no actual I/O need happen
600  * unless we're forced to write out a dirty clog or xlog page to make room
601  * in shared memory.
602  */
603 void
605 {
606  int pageno;
607 
608  /*
609  * No work except at first XID of a page. But beware: just after
610  * wraparound, the first XID of page zero is FirstNormalTransactionId.
611  */
612  if (TransactionIdToPgIndex(newestXact) != 0 &&
614  return;
615 
616  pageno = TransactionIdToPage(newestXact);
617 
618  LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
619 
620  /* Zero the page and make an XLOG entry about it */
621  ZeroCLOGPage(pageno, true);
622 
623  LWLockRelease(CLogControlLock);
624 }
625 
626 
627 /*
628  * Remove all CLOG segments before the one holding the passed transaction ID
629  *
630  * Before removing any CLOG data, we must flush XLOG to disk, to ensure
631  * that any recently-emitted HEAP_FREEZE records have reached disk; otherwise
632  * a crash and restart might leave us with some unfrozen tuples referencing
633  * removed CLOG data. We choose to emit a special TRUNCATE XLOG record too.
634  * Replaying the deletion from XLOG is not critical, since the files could
635  * just as well be removed later, but doing so prevents a long-running hot
636  * standby server from acquiring an unreasonably bloated CLOG directory.
637  *
638  * Since CLOG segments hold a large number of transactions, the opportunity to
639  * actually remove a segment is fairly rare, and so it seems best not to do
640  * the XLOG flush unless we have confirmed that there is a removable segment.
641  */
642 void
644 {
645  int cutoffPage;
646 
647  /*
648  * The cutoff point is the start of the segment containing oldestXact. We
649  * pass the *page* containing oldestXact to SimpleLruTruncate.
650  */
651  cutoffPage = TransactionIdToPage(oldestXact);
652 
653  /* Check to see if there's any files that could be removed */
655  return; /* nothing to remove */
656 
657  /* Write XLOG record and flush XLOG to disk */
658  WriteTruncateXlogRec(cutoffPage);
659 
660  /* Now we can remove the old CLOG segment(s) */
661  SimpleLruTruncate(ClogCtl, cutoffPage);
662 }
663 
664 
665 /*
666  * Decide which of two CLOG page numbers is "older" for truncation purposes.
667  *
668  * We need to use comparison of TransactionIds here in order to do the right
669  * thing with wraparound XID arithmetic. However, if we are asked about
670  * page number zero, we don't want to hand InvalidTransactionId to
671  * TransactionIdPrecedes: it'll get weird about permanent xact IDs. So,
672  * offset both xids by FirstNormalTransactionId to avoid that.
673  */
674 static bool
675 CLOGPagePrecedes(int page1, int page2)
676 {
677  TransactionId xid1;
678  TransactionId xid2;
679 
680  xid1 = ((TransactionId) page1) * CLOG_XACTS_PER_PAGE;
681  xid1 += FirstNormalTransactionId;
682  xid2 = ((TransactionId) page2) * CLOG_XACTS_PER_PAGE;
683  xid2 += FirstNormalTransactionId;
684 
685  return TransactionIdPrecedes(xid1, xid2);
686 }
687 
688 
689 /*
690  * Write a ZEROPAGE xlog record
691  */
692 static void
694 {
695  XLogBeginInsert();
696  XLogRegisterData((char *) (&pageno), sizeof(int));
697  (void) XLogInsert(RM_CLOG_ID, CLOG_ZEROPAGE);
698 }
699 
700 /*
701  * Write a TRUNCATE xlog record
702  *
703  * We must flush the xlog record to disk before returning --- see notes
704  * in TruncateCLOG().
705  */
706 static void
708 {
709  XLogRecPtr recptr;
710 
711  XLogBeginInsert();
712  XLogRegisterData((char *) (&pageno), sizeof(int));
713  recptr = XLogInsert(RM_CLOG_ID, CLOG_TRUNCATE);
714  XLogFlush(recptr);
715 }
716 
717 /*
718  * CLOG resource manager's routines
719  */
720 void
722 {
723  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
724 
725  /* Backup blocks are not used in clog records */
726  Assert(!XLogRecHasAnyBlockRefs(record));
727 
728  if (info == CLOG_ZEROPAGE)
729  {
730  int pageno;
731  int slotno;
732 
733  memcpy(&pageno, XLogRecGetData(record), sizeof(int));
734 
735  LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
736 
737  slotno = ZeroCLOGPage(pageno, false);
738  SimpleLruWritePage(ClogCtl, slotno);
739  Assert(!ClogCtl->shared->page_dirty[slotno]);
740 
741  LWLockRelease(CLogControlLock);
742  }
743  else if (info == CLOG_TRUNCATE)
744  {
745  int pageno;
746 
747  memcpy(&pageno, XLogRecGetData(record), sizeof(int));
748 
749  /*
750  * During XLOG replay, latest_page_number isn't set up yet; insert a
751  * suitable value to bypass the sanity test in SimpleLruTruncate.
752  */
753  ClogCtl->shared->latest_page_number = pageno;
754 
755  SimpleLruTruncate(ClogCtl, pageno);
756  }
757  else
758  elog(PANIC, "clog_redo: unknown op code %u", info);
759 }
#define TransactionIdToBIndex(xid)
Definition: clog.c:65
#define CLOG_XACT_BITMASK
Definition: clog.c:60
#define CLOG_XACTS_PER_PAGE
Definition: clog.c:59
#define TransactionIdToPage(xid)
Definition: clog.c:62
static void WriteZeroPageXlogRec(int pageno)
Definition: clog.c:693
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define TRANSACTION_STATUS_COMMITTED
Definition: clog.h:27
uint32 TransactionId
Definition: c.h:394
#define GetLSNIndex(slotno, xid)
Definition: clog.c:71
void clog_redo(XLogReaderState *record)
Definition: clog.c:721
static void TransactionIdSetStatusBit(TransactionId xid, XidStatus status, XLogRecPtr lsn, int slotno)
Definition: clog.c:323
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1153
void ExtendCLOG(TransactionId newestXact)
Definition: clog.c:604
bool InRecovery
Definition: xlog.c:191
#define Min(x, y)
Definition: c.h:802
#define TransactionIdToPgIndex(xid)
Definition: clog.c:63
void ShutdownCLOG(void)
Definition: clog.c:574
unsigned char uint8
Definition: c.h:263
int XidStatus
Definition: clog.h:24
#define MemSet(start, val, len)
Definition: c.h:853
static SlruCtlData ClogCtlData
Definition: clog.c:78
#define PANIC
Definition: elog.h:53
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:144
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1090
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2745
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
#define ClogCtl
Definition: clog.c:80
#define CLOG_LSNS_PER_PAGE
Definition: clog.c:69
#define CLOG_ZEROPAGE
Definition: clog.h:48
void TransactionIdSetTreeStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
Definition: clog.c:148
#define XLogRecGetData(decoder)
Definition: xlogreader.h:202
#define FirstNormalTransactionId
Definition: transam.h:34
TransactionId nextXid
Definition: transam.h:117
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:370
#define TRANSACTION_STATUS_ABORTED
Definition: clog.h:28
static bool CLOGPagePrecedes(int page1, int page2)
Definition: clog.c:675
VariableCache ShmemVariableCache
Definition: varsup.c:34
Size CLOGShmemBuffers(void)
Definition: clog.c:434
#define TRANSACTION_STATUS_SUB_COMMITTED
Definition: clog.h:29
#define InvalidTransactionId
Definition: transam.h:31
#define CLOG_BITS_PER_XACT
Definition: clog.c:57
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:573
void CheckPointCLOG(void)
Definition: clog.c:586
#define CLOG_TRUNCATE
Definition: clog.h:49
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:198
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
void TruncateCLOG(TransactionId oldestXact)
Definition: clog.c:643
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1306
static void WriteTruncateXlogRec(int pageno)
Definition: clog.c:707
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:462
Size CLOGShmemSize(void)
Definition: clog.c:443
static void set_status_by_pages(int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
Definition: clog.c:222
void BootStrapCLOG(void)
Definition: clog.c:463
#define Max(x, y)
Definition: c.h:796
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:671
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1361
size_t Size
Definition: c.h:353
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
void StartupCLOG(void)
Definition: clog.c:506
XidStatus TransactionIdGetStatus(TransactionId xid, XLogRecPtr *lsn)
Definition: clog.c:392
#define TransactionIdToByte(xid)
Definition: clog.c:64
static int ZeroCLOGPage(int pageno, bool writeXlog)
Definition: clog.c:489
int i
void CLOGShmemInit(void)
Definition: clog.c:449
int NBuffers
Definition: globals.c:122
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:204
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
void XLogBeginInsert(void)
Definition: xloginsert.c:120
static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn, int pageno)
Definition: clog.c:254
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:258
void TrimCLOG(void)
Definition: clog.c:525
#define TRANSACTION_STATUS_IN_PROGRESS
Definition: clog.h:26
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id)
Definition: slru.c:164