PostgreSQL Source Code git master
Loading...
Searching...
No Matches
clog.c File Reference
#include "postgres.h"
#include "access/clog.h"
#include "access/slru.h"
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "access/xlogutils.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "pgstat.h"
#include "storage/proc.h"
#include "storage/sync.h"
#include "utils/guc_hooks.h"
Include dependency graph for clog.c:

Go to the source code of this file.

Macros

#define CLOG_BITS_PER_XACT   2
 
#define CLOG_XACTS_PER_BYTE   4
 
#define CLOG_XACTS_PER_PAGE   (BLCKSZ * CLOG_XACTS_PER_BYTE)
 
#define CLOG_XACT_BITMASK   ((1 << CLOG_BITS_PER_XACT) - 1)
 
#define CLOG_MAX_ALLOWED_BUFFERS
 
#define TransactionIdToPgIndex(xid)   ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE)
 
#define TransactionIdToByte(xid)   (TransactionIdToPgIndex(xid) / CLOG_XACTS_PER_BYTE)
 
#define TransactionIdToBIndex(xid)   ((xid) % (TransactionId) CLOG_XACTS_PER_BYTE)
 
#define CLOG_XACTS_PER_LSN_GROUP   32 /* keep this a power of 2 */
 
#define CLOG_LSNS_PER_PAGE   (CLOG_XACTS_PER_PAGE / CLOG_XACTS_PER_LSN_GROUP)
 
#define GetLSNIndex(slotno, xid)
 
#define THRESHOLD_SUBTRANS_CLOG_OPT   5
 
#define XactCtl   (&XactCtlData)
 

Functions

static int64 TransactionIdToPage (TransactionId xid)
 
static bool CLOGPagePrecedes (int64 page1, int64 page2)
 
static void WriteTruncateXlogRec (int64 pageno, TransactionId oldestXact, Oid oldestXactDb)
 
static void TransactionIdSetPageStatus (TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn, int64 pageno, bool all_xact_same_page)
 
static void TransactionIdSetStatusBit (TransactionId xid, XidStatus status, XLogRecPtr lsn, int slotno)
 
static void set_status_by_pages (int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
 
static bool TransactionGroupUpdateXidStatus (TransactionId xid, XidStatus status, XLogRecPtr lsn, int64 pageno)
 
static void TransactionIdSetPageStatusInternal (TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn, int64 pageno)
 
void TransactionIdSetTreeStatus (TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
 
XidStatus TransactionIdGetStatus (TransactionId xid, XLogRecPtr *lsn)
 
static int CLOGShmemBuffers (void)
 
Size CLOGShmemSize (void)
 
void CLOGShmemInit (void)
 
bool check_transaction_buffers (int *newval, void **extra, GucSource source)
 
void BootStrapCLOG (void)
 
void StartupCLOG (void)
 
void TrimCLOG (void)
 
void CheckPointCLOG (void)
 
void ExtendCLOG (TransactionId newestXact)
 
void TruncateCLOG (TransactionId oldestXact, Oid oldestxid_datoid)
 
void clog_redo (XLogReaderState *record)
 
int clogsyncfiletag (const FileTag *ftag, char *path)
 

Variables

static SlruCtlData XactCtlData
 

Macro Definition Documentation

◆ CLOG_BITS_PER_XACT

#define CLOG_BITS_PER_XACT   2

Definition at line 62 of file clog.c.

◆ CLOG_LSNS_PER_PAGE

#define CLOG_LSNS_PER_PAGE   (CLOG_XACTS_PER_PAGE / CLOG_XACTS_PER_LSN_GROUP)

Definition at line 93 of file clog.c.

◆ CLOG_MAX_ALLOWED_BUFFERS

#define CLOG_MAX_ALLOWED_BUFFERS
Value:
#define Min(x, y)
Definition c.h:997
#define CLOG_XACTS_PER_PAGE
Definition clog.c:64
#define SLRU_MAX_ALLOWED_BUFFERS
Definition slru.h:24
#define MaxTransactionId
Definition transam.h:35

Definition at line 72 of file clog.c.

82{
83 return xid / (int64) CLOG_XACTS_PER_PAGE;
84}
85
86#define TransactionIdToPgIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE)
87#define TransactionIdToByte(xid) (TransactionIdToPgIndex(xid) / CLOG_XACTS_PER_BYTE)
88#define TransactionIdToBIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_BYTE)
89
90/* We store the latest async LSN for each group of transactions */
91#define CLOG_XACTS_PER_LSN_GROUP 32 /* keep this a power of 2 */
92#define CLOG_LSNS_PER_PAGE (CLOG_XACTS_PER_PAGE / CLOG_XACTS_PER_LSN_GROUP)
93
94#define GetLSNIndex(slotno, xid) ((slotno) * CLOG_LSNS_PER_PAGE + \
95 ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE) / CLOG_XACTS_PER_LSN_GROUP)
96
97/*
98 * The number of subtransactions below which we consider to apply clog group
99 * update optimization. Testing reveals that the number higher than this can
100 * hurt performance.
101 */
102#define THRESHOLD_SUBTRANS_CLOG_OPT 5
103
104/*
105 * Link to shared-memory data structures for CLOG control
106 */
108
109#define XactCtl (&XactCtlData)
110
111
113static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact,
114 Oid oldestXactDb);
116 TransactionId *subxids, XidStatus status,
117 XLogRecPtr lsn, int64 pageno,
118 bool all_xact_same_page);
120 XLogRecPtr lsn, int slotno);
121static void set_status_by_pages(int nsubxids, TransactionId *subxids,
122 XidStatus status, XLogRecPtr lsn);
124 XidStatus status, XLogRecPtr lsn, int64 pageno);
126 TransactionId *subxids, XidStatus status,
127 XLogRecPtr lsn, int64 pageno);
128
129
130/*
131 * TransactionIdSetTreeStatus
132 *
133 * Record the final state of transaction entries in the commit log for
134 * a transaction and its subtransaction tree. Take care to ensure this is
135 * efficient, and as atomic as possible.
136 *
137 * xid is a single xid to set status for. This will typically be
138 * the top level transactionid for a top level commit or abort. It can
139 * also be a subtransaction when we record transaction aborts.
140 *
141 * subxids is an array of xids of length nsubxids, representing subtransactions
142 * in the tree of xid. In various cases nsubxids may be zero.
143 *
144 * lsn must be the WAL location of the commit record when recording an async
145 * commit. For a synchronous commit it can be InvalidXLogRecPtr, since the
146 * caller guarantees the commit record is already flushed in that case. It
147 * should be InvalidXLogRecPtr for abort cases, too.
148 *
149 * In the commit case, atomicity is limited by whether all the subxids are in
150 * the same CLOG page as xid. If they all are, then the lock will be grabbed
151 * only once, and the status will be set to committed directly. Otherwise
152 * we must
153 * 1. set sub-committed all subxids that are not on the same page as the
154 * main xid
155 * 2. atomically set committed the main xid and the subxids on the same page
156 * 3. go over the first bunch again and set them committed
157 * Note that as far as concurrent checkers are concerned, main transaction
158 * commit as a whole is still atomic.
159 *
160 * Example:
161 * TransactionId t commits and has subxids t1, t2, t3, t4
162 * t is on page p1, t1 is also on p1, t2 and t3 are on p2, t4 is on p3
163 * 1. update pages2-3:
164 * page2: set t2,t3 as sub-committed
165 * page3: set t4 as sub-committed
166 * 2. update page1:
167 * page1: set t,t1 as committed
168 * 3. update pages2-3:
169 * page2: set t2,t3 as committed
170 * page3: set t4 as committed
171 *
172 * NB: this is a low-level routine and is NOT the preferred entry point
173 * for most uses; functions in transam.c are the intended callers.
174 *
175 * XXX Think about issuing POSIX_FADV_WILLNEED on pages that we will need,
176 * but aren't yet in cache, as well as hinting pages not to fall out of
177 * cache yet.
178 */
179void
181 TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
182{
183 int64 pageno = TransactionIdToPage(xid); /* get page of parent */
184 int i;
185
188
189 /*
190 * See how many subxids, if any, are on the same page as the parent, if
191 * any.
192 */
193 for (i = 0; i < nsubxids; i++)
194 {
195 if (TransactionIdToPage(subxids[i]) != pageno)
196 break;
197 }
198
199 /*
200 * Do all items fit on a single page?
201 */
202 if (i == nsubxids)
203 {
204 /*
205 * Set the parent and all subtransactions in a single call
206 */
207 TransactionIdSetPageStatus(xid, nsubxids, subxids, status, lsn,
208 pageno, true);
209 }
210 else
211 {
213
214 /*
215 * If this is a commit then we care about doing this correctly (i.e.
216 * using the subcommitted intermediate status). By here, we know
217 * we're updating more than one page of clog, so we must mark entries
218 * that are *not* on the first page so that they show as subcommitted
219 * before we then return to update the status to fully committed.
220 *
221 * To avoid touching the first page twice, skip marking subcommitted
222 * for the subxids on that first page.
223 */
224 if (status == TRANSACTION_STATUS_COMMITTED)
226 subxids + nsubxids_on_first_page,
228
229 /*
230 * Now set the parent and subtransactions on same page as the parent,
231 * if any
232 */
233 pageno = TransactionIdToPage(xid);
235 lsn, pageno, false);
236
237 /*
238 * Now work through the rest of the subxids one clog page at a time,
239 * starting from the second page onwards, like we did above.
240 */
242 subxids + nsubxids_on_first_page,
243 status, lsn);
244 }
245}
246
247/*
248 * Helper for TransactionIdSetTreeStatus: set the status for a bunch of
249 * transactions, chunking in the separate CLOG pages involved. We never
250 * pass the whole transaction tree to this function, only subtransactions
251 * that are on different pages to the top level transaction id.
252 */
253static void
255 XidStatus status, XLogRecPtr lsn)
256{
257 int64 pageno = TransactionIdToPage(subxids[0]);
258 int offset = 0;
259 int i = 0;
260
261 Assert(nsubxids > 0); /* else the pageno fetch above is unsafe */
262
263 while (i < nsubxids)
264 {
265 int num_on_page = 0;
267
268 do
269 {
270 nextpageno = TransactionIdToPage(subxids[i]);
271 if (nextpageno != pageno)
272 break;
273 num_on_page++;
274 i++;
275 } while (i < nsubxids);
276
278 num_on_page, subxids + offset,
279 status, lsn, pageno, false);
280 offset = i;
281 pageno = nextpageno;
282 }
283}
284
285/*
286 * Record the final state of transaction entries in the commit log for all
287 * entries on a single page. Atomic only on this page.
288 */
289static void
291 TransactionId *subxids, XidStatus status,
292 XLogRecPtr lsn, int64 pageno,
294{
295 LWLock *lock;
296
297 /* Can't use group update when PGPROC overflows. */
299 "group clog threshold less than PGPROC cached subxids");
300
301 /* Get the SLRU bank lock for the page we are going to access. */
302 lock = SimpleLruGetBankLock(XactCtl, pageno);
303
304 /*
305 * When there is contention on the SLRU bank lock we need, we try to group
306 * multiple updates; a single leader process will perform transaction
307 * status updates for multiple backends so that the number of times the
308 * bank lock needs to be acquired is reduced.
309 *
310 * For this optimization to be safe, the XID and subxids in MyProc must be
311 * the same as the ones for which we're setting the status. Check that
312 * this is the case.
313 *
314 * For this optimization to be efficient, we shouldn't have too many
315 * sub-XIDs and all of the XIDs for which we're adjusting clog should be
316 * on the same page. Check those conditions, too.
317 */
318 if (all_xact_same_page && xid == MyProc->xid &&
321 (nsubxids == 0 ||
322 memcmp(subxids, MyProc->subxids.xids,
323 nsubxids * sizeof(TransactionId)) == 0))
324 {
325 /*
326 * If we can immediately acquire the lock, we update the status of our
327 * own XID and release the lock. If not, try use group XID update. If
328 * that doesn't work out, fall back to waiting for the lock to perform
329 * an update for this transaction only.
330 */
332 {
333 /* Got the lock without waiting! Do the update. */
334 TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
335 lsn, pageno);
336 LWLockRelease(lock);
337 return;
338 }
339 else if (TransactionGroupUpdateXidStatus(xid, status, lsn, pageno))
340 {
341 /* Group update mechanism has done the work. */
342 return;
343 }
344
345 /* Fall through only if update isn't done yet. */
346 }
347
348 /* Group update not applicable, or couldn't accept this page number. */
350 TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
351 lsn, pageno);
352 LWLockRelease(lock);
353}
354
355/*
356 * Record the final state of transaction entry in the commit log
357 *
358 * We don't do any locking here; caller must handle that.
359 */
360static void
362 TransactionId *subxids, XidStatus status,
363 XLogRecPtr lsn, int64 pageno)
364{
365 int slotno;
366 int i;
367
369 status == TRANSACTION_STATUS_ABORTED ||
372 LW_EXCLUSIVE));
373
374 /*
375 * If we're doing an async commit (ie, lsn is valid), then we must wait
376 * for any active write on the page slot to complete. Otherwise our
377 * update could reach disk in that write, which will not do since we
378 * mustn't let it reach disk until we've done the appropriate WAL flush.
379 * But when lsn is invalid, it's OK to scribble on a page while it is
380 * write-busy, since we don't care if the update reaches disk sooner than
381 * we think.
382 */
384 xid);
385
386 /*
387 * Set the main transaction id, if any.
388 *
389 * If we update more than one xid on this page while it is being written
390 * out, we might find that some of the bits go to disk and others don't.
391 * If we are updating commits on the page with the top-level xid that
392 * could break atomicity, so we subcommit the subxids first before we mark
393 * the top-level commit.
394 */
395 if (TransactionIdIsValid(xid))
396 {
397 /* Subtransactions first, if needed ... */
398 if (status == TRANSACTION_STATUS_COMMITTED)
399 {
400 for (i = 0; i < nsubxids; i++)
401 {
402 Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
405 lsn, slotno);
406 }
407 }
408
409 /* ... then the main transaction */
410 TransactionIdSetStatusBit(xid, status, lsn, slotno);
411 }
412
413 /* Set the subtransactions */
414 for (i = 0; i < nsubxids; i++)
415 {
416 Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
417 TransactionIdSetStatusBit(subxids[i], status, lsn, slotno);
418 }
419
420 XactCtl->shared->page_dirty[slotno] = true;
421}
422
423/*
424 * Subroutine for TransactionIdSetPageStatus, q.v.
425 *
426 * When we cannot immediately acquire the SLRU bank lock in exclusive mode at
427 * commit time, add ourselves to a list of processes that need their XIDs
428 * status update. The first process to add itself to the list will acquire
429 * the lock in exclusive mode and set transaction status as required on behalf
430 * of all group members. This avoids a great deal of contention when many
431 * processes are trying to commit at once, since the lock need not be
432 * repeatedly handed off from one committing process to the next.
433 *
434 * Returns true when transaction status has been updated in clog; returns
435 * false if we decided against applying the optimization because the page
436 * number we need to update differs from those processes already waiting.
437 */
438static bool
440 XLogRecPtr lsn, int64 pageno)
441{
442 volatile PROC_HDR *procglobal = ProcGlobal;
443 PGPROC *proc = MyProc;
448
449 /* We should definitely have an XID whose status needs to be updated. */
451
452 /*
453 * Prepare to add ourselves to the list of processes needing a group XID
454 * status update.
455 */
456 proc->clogGroupMember = true;
457 proc->clogGroupMemberXid = xid;
458 proc->clogGroupMemberXidStatus = status;
459 proc->clogGroupMemberPage = pageno;
460 proc->clogGroupMemberLsn = lsn;
461
462 /*
463 * We put ourselves in the queue by writing MyProcNumber to
464 * ProcGlobal->clogGroupFirst. However, if there's already a process
465 * listed there, we compare our pageno with that of that process; if it
466 * differs, we cannot participate in the group, so we return for caller to
467 * update pg_xact in the normal way.
468 *
469 * If we're not the first process in the list, we must follow the leader.
470 * We do this by storing the data we want updated in our PGPROC entry
471 * where the leader can find it, then going to sleep.
472 *
473 * If no process is already in the list, we're the leader; our first step
474 * is to lock the SLRU bank to which our page belongs, then we close out
475 * the group by resetting the list pointer from ProcGlobal->clogGroupFirst
476 * (this lets other processes set up other groups later); finally we do
477 * the SLRU updates, release the SLRU bank lock, and wake up the sleeping
478 * processes.
479 *
480 * If another group starts to update a page in a different SLRU bank, they
481 * can proceed concurrently, since the bank lock they're going to use is
482 * different from ours. If another group starts to update a page in the
483 * same bank as ours, they wait until we release the lock.
484 */
485 nextidx = pg_atomic_read_u32(&procglobal->clogGroupFirst);
486
487 while (true)
488 {
489 /*
490 * Add the proc to list, if the clog page where we need to update the
491 * current transaction status is same as group leader's clog page.
492 *
493 * There is a race condition here, which is that after doing the below
494 * check and before adding this proc's clog update to a group, the
495 * group leader might have already finished the group update for this
496 * page and becomes group leader of another group, updating a
497 * different page. This will lead to a situation where a single group
498 * can have different clog page updates. This isn't likely and will
499 * still work, just less efficiently -- we handle this case by
500 * switching to a different bank lock in the loop below.
501 */
503 GetPGProcByNumber(nextidx)->clogGroupMemberPage != proc->clogGroupMemberPage)
504 {
505 /*
506 * Ensure that this proc is not a member of any clog group that
507 * needs an XID status update.
508 */
509 proc->clogGroupMember = false;
511 return false;
512 }
513
515
516 if (pg_atomic_compare_exchange_u32(&procglobal->clogGroupFirst,
517 &nextidx,
519 break;
520 }
521
522 /*
523 * If the list was not empty, the leader will update the status of our
524 * XID. It is impossible to have followers without a leader because the
525 * first process that has added itself to the list will always have
526 * nextidx as INVALID_PROC_NUMBER.
527 */
529 {
530 int extraWaits = 0;
531
532 /* Sleep until the leader updates our XID status. */
534 for (;;)
535 {
536 /* acts as a read barrier */
537 PGSemaphoreLock(proc->sem);
538 if (!proc->clogGroupMember)
539 break;
540 extraWaits++;
541 }
543
545
546 /* Fix semaphore count for any absorbed wakeups */
547 while (extraWaits-- > 0)
548 PGSemaphoreUnlock(proc->sem);
549 return true;
550 }
551
552 /*
553 * By here, we know we're the leader process. Acquire the SLRU bank lock
554 * that corresponds to the page we originally wanted to modify.
555 */
559
560 /*
561 * Now that we've got the lock, clear the list of processes waiting for
562 * group XID status update, saving a pointer to the head of the list.
563 * (Trying to pop elements one at a time could lead to an ABA problem.)
564 *
565 * At this point, any processes trying to do this would create a separate
566 * group.
567 */
568 nextidx = pg_atomic_exchange_u32(&procglobal->clogGroupFirst,
570
571 /* Remember head of list so we can perform wakeups after dropping lock. */
573
574 /* Walk the list and update the status of all XIDs. */
576 {
578 int64 thispageno = nextproc->clogGroupMemberPage;
579
580 /*
581 * If the page to update belongs to a different bank than the previous
582 * one, exchange bank lock to the new one. This should be quite rare,
583 * as described above.
584 *
585 * (We could try to optimize this by waking up the processes for which
586 * we have already updated the status while we exchange the lock, but
587 * the code doesn't do that at present. I think it'd require
588 * additional bookkeeping, making the common path slower in order to
589 * improve an infrequent case.)
590 */
591 if (thispageno != prevpageno)
592 {
594
595 if (prevlock != lock)
596 {
599 }
600 prevlock = lock;
602 }
603
604 /*
605 * Transactions with more than THRESHOLD_SUBTRANS_CLOG_OPT sub-XIDs
606 * should not use group XID status update mechanism.
607 */
608 Assert(nextproc->subxidStatus.count <= THRESHOLD_SUBTRANS_CLOG_OPT);
609
611 nextproc->subxidStatus.count,
612 nextproc->subxids.xids,
613 nextproc->clogGroupMemberXidStatus,
614 nextproc->clogGroupMemberLsn,
615 nextproc->clogGroupMemberPage);
616
617 /* Move to next proc in list. */
618 nextidx = pg_atomic_read_u32(&nextproc->clogGroupNext);
619 }
620
621 /* We're done with the lock now. */
622 if (prevlock != NULL)
624
625 /*
626 * Now that we've released the lock, go back and wake everybody up. We
627 * don't do this under the lock so as to keep lock hold times to a
628 * minimum.
629 *
630 * (Perhaps we could do this in two passes, the first setting
631 * clogGroupNext to invalid while saving the semaphores to an array, then
632 * a single write barrier, then another pass unlocking the semaphores.)
633 */
635 {
637
638 wakeidx = pg_atomic_read_u32(&wakeproc->clogGroupNext);
640
641 /* ensure all previous writes are visible before follower continues. */
643
644 wakeproc->clogGroupMember = false;
645
646 if (wakeproc != MyProc)
648 }
649
650 return true;
651}
652
653/*
654 * Sets the commit status of a single transaction.
655 *
656 * Caller must hold the corresponding SLRU bank lock, will be held at exit.
657 */
658static void
660{
661 int byteno = TransactionIdToByte(xid);
663 char *byteptr;
664 char byteval;
665 char curval;
666
667 Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(xid));
669 XactCtl->shared->page_number[slotno]),
670 LW_EXCLUSIVE));
671
672 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
674
675 /*
676 * When replaying transactions during recovery we still need to perform
677 * the two phases of subcommit and then commit. However, some transactions
678 * are already correctly marked, so we just treat those as a no-op which
679 * allows us to keep the following Assert as restrictive as possible.
680 */
683 return;
684
685 /*
686 * Current state change should be from 0 or subcommitted to target state
687 * or we should already be there when replaying changes during recovery.
688 */
689 Assert(curval == 0 ||
692 curval == status);
693
694 /* note this assumes exclusive access to the clog page */
695 byteval = *byteptr;
696 byteval &= ~(((1 << CLOG_BITS_PER_XACT) - 1) << bshift);
697 byteval |= (status << bshift);
698 *byteptr = byteval;
699
700 /*
701 * Update the group LSN if the transaction completion LSN is higher.
702 *
703 * Note: lsn will be invalid when supplied during InRecovery processing,
704 * so we don't need to do anything special to avoid LSN updates during
705 * recovery. After recovery completes the next clog change will set the
706 * LSN correctly.
707 */
708 if (XLogRecPtrIsValid(lsn))
709 {
710 int lsnindex = GetLSNIndex(slotno, xid);
711
712 if (XactCtl->shared->group_lsn[lsnindex] < lsn)
713 XactCtl->shared->group_lsn[lsnindex] = lsn;
714 }
715}
716
717/*
718 * Interrogate the state of a transaction in the commit log.
719 *
720 * Aside from the actual commit status, this function returns (into *lsn)
721 * an LSN that is late enough to be able to guarantee that if we flush up to
722 * that LSN then we will have flushed the transaction's commit record to disk.
723 * The result is not necessarily the exact LSN of the transaction's commit
724 * record! For example, for long-past transactions (those whose clog pages
725 * already migrated to disk), we'll return InvalidXLogRecPtr. Also, because
726 * we group transactions on the same clog page to conserve storage, we might
727 * return the LSN of a later transaction that falls into the same group.
728 *
729 * NB: this is a low-level routine and is NOT the preferred entry point
730 * for most uses; TransactionLogFetch() in transam.c is the intended caller.
731 */
734{
735 int64 pageno = TransactionIdToPage(xid);
736 int byteno = TransactionIdToByte(xid);
738 int slotno;
739 int lsnindex;
740 char *byteptr;
741 XidStatus status;
742
743 /* lock is acquired by SimpleLruReadPage_ReadOnly */
744
746 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
747
748 status = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
749
751 *lsn = XactCtl->shared->group_lsn[lsnindex];
752
754
755 return status;
756}
757
758/*
759 * Number of shared CLOG buffers.
760 *
761 * If asked to autotune, use 2MB for every 1GB of shared buffers, up to 8MB.
762 * Otherwise just cap the configured amount to be between 16 and the maximum
763 * allowed.
764 */
765static int
767{
768 /* auto-tune based on shared buffers */
769 if (transaction_buffers == 0)
770 return SimpleLruAutotuneBuffers(512, 1024);
771
773}
774
775/*
776 * Initialization of shared memory for CLOG
777 */
778Size
779CLOGShmemSize(void)
780{
782}
783
784void
785CLOGShmemInit(void)
786{
787 /* If auto-tuning is requested, now is the time to do it */
788 if (transaction_buffers == 0)
789 {
790 char buf[32];
791
792 snprintf(buf, sizeof(buf), "%d", CLOGShmemBuffers());
793 SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
795
796 /*
797 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
798 * However, if the DBA explicitly set transaction_buffers = 0 in the
799 * config file, then PGC_S_DYNAMIC_DEFAULT will fail to override that
800 * and we must force the matter with PGC_S_OVERRIDE.
801 */
802 if (transaction_buffers == 0) /* failed to apply it? */
803 SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
805 }
807
808 XactCtl->PagePrecedes = CLOGPagePrecedes;
810 "pg_xact", LWTRANCHE_XACT_BUFFER,
813}
814
815/*
816 * GUC check_hook for transaction_buffers
817 */
818bool
820{
821 return check_slru_buffers("transaction_buffers", newval);
822}
823
824/*
825 * This func must be called ONCE on system install. It creates
826 * the initial CLOG segment. (The CLOG directory is assumed to
827 * have been created by initdb, and CLOGShmemInit must have been
828 * called already.)
829 */
830void
831BootStrapCLOG(void)
832{
833 /* Zero the initial page and flush it to disk */
835}
836
837/*
838 * This must be called ONCE during postmaster or standalone-backend startup,
839 * after StartupXLOG has initialized TransamVariables->nextXid.
840 */
841void
842StartupCLOG(void)
843{
845 int64 pageno = TransactionIdToPage(xid);
846
847 /*
848 * Initialize our idea of the latest page number.
849 */
850 pg_atomic_write_u64(&XactCtl->shared->latest_page_number, pageno);
851}
852
853/*
854 * This must be called ONCE at the end of startup/recovery.
855 */
856void
857TrimCLOG(void)
858{
860 int64 pageno = TransactionIdToPage(xid);
861 LWLock *lock = SimpleLruGetBankLock(XactCtl, pageno);
862
864
865 /*
866 * Zero out the remainder of the current clog page. Under normal
867 * circumstances it should be zeroes already, but it seems at least
868 * theoretically possible that XLOG replay will have settled on a nextXID
869 * value that is less than the last XID actually used and marked by the
870 * previous database lifecycle (since subtransaction commit writes clog
871 * but makes no WAL entry). Let's just be safe. (We need not worry about
872 * pages beyond the current one, since those will be zeroed when first
873 * used. For the same reason, there is no need to do anything when
874 * nextXid is exactly at a page boundary; and it's likely that the
875 * "current" page doesn't exist yet in that case.)
876 */
877 if (TransactionIdToPgIndex(xid) != 0)
878 {
879 int byteno = TransactionIdToByte(xid);
881 int slotno;
882 char *byteptr;
883
884 slotno = SimpleLruReadPage(XactCtl, pageno, false, xid);
885 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
886
887 /* Zero so-far-unused positions in the current byte */
888 *byteptr &= (1 << bshift) - 1;
889 /* Zero the rest of the page */
890 MemSet(byteptr + 1, 0, BLCKSZ - byteno - 1);
891
892 XactCtl->shared->page_dirty[slotno] = true;
893 }
894
895 LWLockRelease(lock);
896}
897
898/*
899 * Perform a checkpoint --- either during shutdown, or on-the-fly
900 */
901void
902CheckPointCLOG(void)
903{
904 /*
905 * Write dirty CLOG pages to disk. This may result in sync requests
906 * queued for later handling by ProcessSyncRequests(), as part of the
907 * checkpoint.
908 */
912}
913
914
915/*
916 * Make sure that CLOG has room for a newly-allocated XID.
917 *
918 * NB: this is called while holding XidGenLock. We want it to be very fast
919 * most of the time; even when it's not so fast, no actual I/O need happen
920 * unless we're forced to write out a dirty clog or xlog page to make room
921 * in shared memory.
922 */
923void
925{
926 int64 pageno;
927 LWLock *lock;
928
929 /*
930 * No work except at first XID of a page. But beware: just after
931 * wraparound, the first XID of page zero is FirstNormalTransactionId.
932 */
935 return;
936
938 lock = SimpleLruGetBankLock(XactCtl, pageno);
939
941
942 /* Zero the page and make a WAL entry about it */
943 SimpleLruZeroPage(XactCtl, pageno);
945
946 LWLockRelease(lock);
947}
948
949
950/*
951 * Remove all CLOG segments before the one holding the passed transaction ID
952 *
953 * Before removing any CLOG data, we must flush XLOG to disk, to ensure that
954 * any recently-emitted records with freeze plans have reached disk; otherwise
955 * a crash and restart might leave us with some unfrozen tuples referencing
956 * removed CLOG data. We choose to emit a special TRUNCATE XLOG record too.
957 * Replaying the deletion from XLOG is not critical, since the files could
958 * just as well be removed later, but doing so prevents a long-running hot
959 * standby server from acquiring an unreasonably bloated CLOG directory.
960 *
961 * Since CLOG segments hold a large number of transactions, the opportunity to
962 * actually remove a segment is fairly rare, and so it seems best not to do
963 * the XLOG flush unless we have confirmed that there is a removable segment.
964 */
965void
967{
969
970 /*
971 * The cutoff point is the start of the segment containing oldestXact. We
972 * pass the *page* containing oldestXact to SimpleLruTruncate.
973 */
974 cutoffPage = TransactionIdToPage(oldestXact);
975
976 /* Check to see if there's any files that could be removed */
978 return; /* nothing to remove */
979
980 /*
981 * Advance oldestClogXid before truncating clog, so concurrent xact status
982 * lookups can ensure they don't attempt to access truncated-away clog.
983 *
984 * It's only necessary to do this if we will actually truncate away clog
985 * pages.
986 */
987 AdvanceOldestClogXid(oldestXact);
988
989 /*
990 * Write XLOG record and flush XLOG to disk. We record the oldest xid
991 * we're keeping information about here so we can ensure that it's always
992 * ahead of clog truncation in case we crash, and so a standby finds out
993 * the new valid xid before the next checkpoint.
994 */
996
997 /* Now we can remove the old CLOG segment(s) */
999}
1000
1001
1002/*
1003 * Decide whether a CLOG page number is "older" for truncation purposes.
1004 *
1005 * We need to use comparison of TransactionIds here in order to do the right
1006 * thing with wraparound XID arithmetic. However, TransactionIdPrecedes()
1007 * would get weird about permanent xact IDs. So, offset both such that xid1,
1008 * xid2, and xid2 + CLOG_XACTS_PER_PAGE - 1 are all normal XIDs; this offset
1009 * is relevant to page 0 and to the page preceding page 0.
1010 *
1011 * The page containing oldestXact-2^31 is the important edge case. The
1012 * portion of that page equaling or following oldestXact-2^31 is expendable,
1013 * but the portion preceding oldestXact-2^31 is not. When oldestXact-2^31 is
1014 * the first XID of a page and segment, the entire page and segment is
1015 * expendable, and we could truncate the segment. Recognizing that case would
1016 * require making oldestXact, not just the page containing oldestXact,
1017 * available to this callback. The benefit would be rare and small, so we
1018 * don't optimize that edge case.
1019 */
1020static bool
1022{
1025
1030
1031 return (TransactionIdPrecedes(xid1, xid2) &&
1033}
1034
1035
1036/*
1037 * Write a TRUNCATE xlog record
1038 *
1039 * We must flush the xlog record to disk before returning --- see notes
1040 * in TruncateCLOG().
1041 */
1042static void
1043WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact, Oid oldestXactDb)
1044{
1047
1048 xlrec.pageno = pageno;
1049 xlrec.oldestXact = oldestXact;
1050 xlrec.oldestXactDb = oldestXactDb;
1051
1056}
1057
1058/*
1059 * CLOG resource manager's routines
1060 */
1061void
1063{
1064 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1065
1066 /* Backup blocks are not used in clog records */
1068
1069 if (info == CLOG_ZEROPAGE)
1070 {
1071 int64 pageno;
1072
1073 memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1075 }
1076 else if (info == CLOG_TRUNCATE)
1077 {
1079
1080 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_clog_truncate));
1081
1082 AdvanceOldestClogXid(xlrec.oldestXact);
1083
1085 }
1086 else
1087 elog(PANIC, "clog_redo: unknown op code %u", info);
1088}
1089
1090/*
1091 * Entrypoint for sync.c to sync clog files.
1092 */
1093int
1094clogsyncfiletag(const FileTag *ftag, char *path)
1095{
1096 return SlruSyncFileTag(XactCtl, ftag, path);
1097}
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition atomics.h:349
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:485
#define pg_write_barrier()
Definition atomics.h:155
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:274
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:237
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition atomics.h:330
uint8_t uint8
Definition c.h:544
#define Max(x, y)
Definition c.h:991
#define Assert(condition)
Definition c.h:873
int64_t int64
Definition c.h:543
uint32_t uint32
Definition c.h:546
#define MemSet(start, val, len)
Definition c.h:1013
#define StaticAssertDecl(condition, errmessage)
Definition c.h:942
uint32 TransactionId
Definition c.h:666
size_t Size
Definition c.h:619
#define CLOG_MAX_ALLOWED_BUFFERS
Definition clog.c:72
#define CLOG_XACT_BITMASK
Definition clog.c:65
#define THRESHOLD_SUBTRANS_CLOG_OPT
Definition clog.c:103
static void TransactionIdSetStatusBit(TransactionId xid, XidStatus status, XLogRecPtr lsn, int slotno)
Definition clog.c:660
XidStatus TransactionIdGetStatus(TransactionId xid, XLogRecPtr *lsn)
Definition clog.c:734
void TransactionIdSetTreeStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
Definition clog.c:181
void CLOGShmemInit(void)
Definition clog.c:786
#define TransactionIdToBIndex(xid)
Definition clog.c:89
static int CLOGShmemBuffers(void)
Definition clog.c:767
void ExtendCLOG(TransactionId newestXact)
Definition clog.c:925
void clog_redo(XLogReaderState *record)
Definition clog.c:1063
static bool CLOGPagePrecedes(int64 page1, int64 page2)
Definition clog.c:1022
void TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
Definition clog.c:967
Size CLOGShmemSize(void)
Definition clog.c:780
bool check_transaction_buffers(int *newval, void **extra, GucSource source)
Definition clog.c:820
static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn, int64 pageno, bool all_xact_same_page)
Definition clog.c:291
int clogsyncfiletag(const FileTag *ftag, char *path)
Definition clog.c:1095
void BootStrapCLOG(void)
Definition clog.c:832
#define CLOG_BITS_PER_XACT
Definition clog.c:62
#define CLOG_LSNS_PER_PAGE
Definition clog.c:93
static int64 TransactionIdToPage(TransactionId xid)
Definition clog.c:82
#define TransactionIdToByte(xid)
Definition clog.c:88
#define TransactionIdToPgIndex(xid)
Definition clog.c:87
void StartupCLOG(void)
Definition clog.c:843
static void set_status_by_pages(int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
Definition clog.c:255
static bool TransactionGroupUpdateXidStatus(TransactionId xid, XidStatus status, XLogRecPtr lsn, int64 pageno)
Definition clog.c:440
static void TransactionIdSetPageStatusInternal(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn, int64 pageno)
Definition clog.c:362
#define GetLSNIndex(slotno, xid)
Definition clog.c:95
void CheckPointCLOG(void)
Definition clog.c:903
static SlruCtlData XactCtlData
Definition clog.c:108
#define XactCtl
Definition clog.c:110
void TrimCLOG(void)
Definition clog.c:858
static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact, Oid oldestXactDb)
Definition clog.c:1044
#define TRANSACTION_STATUS_IN_PROGRESS
Definition clog.h:27
int XidStatus
Definition clog.h:25
#define CLOG_ZEROPAGE
Definition clog.h:55
#define TRANSACTION_STATUS_ABORTED
Definition clog.h:29
#define TRANSACTION_STATUS_SUB_COMMITTED
Definition clog.h:30
#define CLOG_TRUNCATE
Definition clog.h:56
#define TRANSACTION_STATUS_COMMITTED
Definition clog.h:28
#define PANIC
Definition elog.h:42
#define elog(elevel,...)
Definition elog.h:226
int transaction_buffers
Definition globals.c:167
ProcNumber MyProcNumber
Definition globals.c:90
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4196
#define newval
GucSource
Definition guc.h:112
@ PGC_S_DYNAMIC_DEFAULT
Definition guc.h:114
@ PGC_S_OVERRIDE
Definition guc.h:123
@ PGC_POSTMASTER
Definition guc.h:74
int i
Definition isn.c:77
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1955
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1347
@ LW_EXCLUSIVE
Definition lwlock.h:112
static rewind_source * source
Definition pg_rewind.c:89
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define snprintf
Definition port.h:260
void PGSemaphoreUnlock(PGSemaphore sema)
Definition posix_sema.c:335
void PGSemaphoreLock(PGSemaphore sema)
Definition posix_sema.c:315
unsigned int Oid
static int fb(int x)
#define GetPGProcByNumber(n)
Definition proc.h:446
#define PGPROC_MAX_CACHED_SUBXIDS
Definition proc.h:39
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, const char *subdir, int buffer_tranche_id, int bank_tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
Definition slru.c:252
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
Definition slru.c:630
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition slru.c:1347
int SimpleLruAutotuneBuffers(int divisor, int max)
Definition slru.c:231
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition slru.c:1816
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
Definition slru.c:527
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition slru.c:1856
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition slru.c:375
void SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
Definition slru.c:444
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition slru.c:1433
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition slru.c:198
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition slru.c:1737
bool check_slru_buffers(const char *name, int *newval)
Definition slru.c:355
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition slru.h:160
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition slru.h:185
PGPROC * MyProc
Definition proc.c:67
PROC_HDR * ProcGlobal
Definition proc.c:79
Definition sync.h:51
Definition proc.h:179
XLogRecPtr clogGroupMemberLsn
Definition proc.h:312
TransactionId clogGroupMemberXid
Definition proc.h:307
int64 clogGroupMemberPage
Definition proc.h:310
bool clogGroupMember
Definition proc.h:305
pg_atomic_uint32 clogGroupNext
Definition proc.h:306
XidStatus clogGroupMemberXidStatus
Definition proc.h:308
XidCacheStatus subxidStatus
Definition proc.h:286
TransactionId xid
Definition proc.h:189
struct XidCache subxids
Definition proc.h:288
PGSemaphore sem
Definition proc.h:183
FullTransactionId nextXid
Definition transam.h:220
uint8 count
Definition proc.h:44
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition proc.h:51
int64 pageno
Definition clog.h:34
@ SYNC_HANDLER_CLOG
Definition sync.h:38
#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
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
void AdvanceOldestClogXid(TransactionId oldest_datfrozenxid)
Definition varsup.c:355
TransamVariablesData * TransamVariables
Definition varsup.c:34
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:69
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
void XLogFlush(XLogRecPtr record)
Definition xlog.c:2783
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
uint64 XLogRecPtr
Definition xlogdefs.h:21
XLogRecPtr XLogSimpleInsertInt64(RmgrId rmid, uint8 info, int64 value)
Definition xloginsert.c:543
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition xloginsert.c:478
void XLogRegisterData(const void *data, uint32 len)
Definition xloginsert.c:368
void XLogBeginInsert(void)
Definition xloginsert.c:152
#define XLogRecGetInfo(decoder)
Definition xlogreader.h:409
#define XLogRecGetData(decoder)
Definition xlogreader.h:414
#define XLogRecHasAnyBlockRefs(decoder)
Definition xlogreader.h:416
bool InRecovery
Definition xlogutils.c:50

◆ CLOG_XACT_BITMASK

#define CLOG_XACT_BITMASK   ((1 << CLOG_BITS_PER_XACT) - 1)

Definition at line 65 of file clog.c.

◆ CLOG_XACTS_PER_BYTE

#define CLOG_XACTS_PER_BYTE   4

Definition at line 63 of file clog.c.

◆ CLOG_XACTS_PER_LSN_GROUP

#define CLOG_XACTS_PER_LSN_GROUP   32 /* keep this a power of 2 */

Definition at line 92 of file clog.c.

◆ CLOG_XACTS_PER_PAGE

#define CLOG_XACTS_PER_PAGE   (BLCKSZ * CLOG_XACTS_PER_BYTE)

Definition at line 64 of file clog.c.

◆ GetLSNIndex

#define GetLSNIndex (   slotno,
  xid 
)
Value:

Definition at line 95 of file clog.c.

◆ THRESHOLD_SUBTRANS_CLOG_OPT

#define THRESHOLD_SUBTRANS_CLOG_OPT   5

Definition at line 103 of file clog.c.

◆ TransactionIdToBIndex

#define TransactionIdToBIndex (   xid)    ((xid) % (TransactionId) CLOG_XACTS_PER_BYTE)

Definition at line 89 of file clog.c.

◆ TransactionIdToByte

#define TransactionIdToByte (   xid)    (TransactionIdToPgIndex(xid) / CLOG_XACTS_PER_BYTE)

Definition at line 88 of file clog.c.

◆ TransactionIdToPgIndex

#define TransactionIdToPgIndex (   xid)    ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE)

Definition at line 87 of file clog.c.

◆ XactCtl

#define XactCtl   (&XactCtlData)

Definition at line 110 of file clog.c.

Function Documentation

◆ BootStrapCLOG()

void BootStrapCLOG ( void  )

Definition at line 832 of file clog.c.

833{
834 /* Zero the initial page and flush it to disk */
836}

References SimpleLruZeroAndWritePage(), and XactCtl.

Referenced by BootStrapXLOG().

◆ check_transaction_buffers()

bool check_transaction_buffers ( int newval,
void **  extra,
GucSource  source 
)

Definition at line 820 of file clog.c.

821{
822 return check_slru_buffers("transaction_buffers", newval);
823}

References check_slru_buffers(), and newval.

◆ CheckPointCLOG()

void CheckPointCLOG ( void  )

Definition at line 903 of file clog.c.

904{
905 /*
906 * Write dirty CLOG pages to disk. This may result in sync requests
907 * queued for later handling by ProcessSyncRequests(), as part of the
908 * checkpoint.
909 */
913}

References fb(), SimpleLruWriteAll(), and XactCtl.

Referenced by CheckPointGuts().

◆ clog_redo()

void clog_redo ( XLogReaderState record)

Definition at line 1063 of file clog.c.

1064{
1065 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1066
1067 /* Backup blocks are not used in clog records */
1069
1070 if (info == CLOG_ZEROPAGE)
1071 {
1072 int64 pageno;
1073
1074 memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1076 }
1077 else if (info == CLOG_TRUNCATE)
1078 {
1080
1081 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_clog_truncate));
1082
1083 AdvanceOldestClogXid(xlrec.oldestXact);
1084
1086 }
1087 else
1088 elog(PANIC, "clog_redo: unknown op code %u", info);
1089}

References AdvanceOldestClogXid(), Assert, CLOG_TRUNCATE, CLOG_ZEROPAGE, elog, fb(), PANIC, SimpleLruTruncate(), SimpleLruZeroAndWritePage(), XactCtl, XLogRecGetData, XLogRecGetInfo, and XLogRecHasAnyBlockRefs.

◆ CLOGPagePrecedes()

static bool CLOGPagePrecedes ( int64  page1,
int64  page2 
)
static

◆ CLOGShmemBuffers()

static int CLOGShmemBuffers ( void  )
static

Definition at line 767 of file clog.c.

768{
769 /* auto-tune based on shared buffers */
770 if (transaction_buffers == 0)
771 return SimpleLruAutotuneBuffers(512, 1024);
772
774}

References CLOG_MAX_ALLOWED_BUFFERS, Max, Min, SimpleLruAutotuneBuffers(), and transaction_buffers.

Referenced by CLOGShmemInit(), and CLOGShmemSize().

◆ CLOGShmemInit()

void CLOGShmemInit ( void  )

Definition at line 786 of file clog.c.

787{
788 /* If auto-tuning is requested, now is the time to do it */
789 if (transaction_buffers == 0)
790 {
791 char buf[32];
792
793 snprintf(buf, sizeof(buf), "%d", CLOGShmemBuffers());
794 SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
796
797 /*
798 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
799 * However, if the DBA explicitly set transaction_buffers = 0 in the
800 * config file, then PGC_S_DYNAMIC_DEFAULT will fail to override that
801 * and we must force the matter with PGC_S_OVERRIDE.
802 */
803 if (transaction_buffers == 0) /* failed to apply it? */
804 SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
806 }
808
809 XactCtl->PagePrecedes = CLOGPagePrecedes;
811 "pg_xact", LWTRANCHE_XACT_BUFFER,
814}

References Assert, buf, CLOG_LSNS_PER_PAGE, CLOG_XACTS_PER_PAGE, CLOGPagePrecedes(), CLOGShmemBuffers(), fb(), PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT, PGC_S_OVERRIDE, SetConfigOption(), SimpleLruInit(), SlruPagePrecedesUnitTests, snprintf, SYNC_HANDLER_CLOG, transaction_buffers, and XactCtl.

Referenced by CreateOrAttachShmemStructs().

◆ CLOGShmemSize()

Size CLOGShmemSize ( void  )

Definition at line 780 of file clog.c.

References CLOG_LSNS_PER_PAGE, CLOGShmemBuffers(), and SimpleLruShmemSize().

Referenced by CalculateShmemSize().

◆ clogsyncfiletag()

int clogsyncfiletag ( const FileTag ftag,
char path 
)

Definition at line 1095 of file clog.c.

1096{
1097 return SlruSyncFileTag(XactCtl, ftag, path);
1098}

References SlruSyncFileTag(), and XactCtl.

◆ ExtendCLOG()

void ExtendCLOG ( TransactionId  newestXact)

Definition at line 925 of file clog.c.

926{
927 int64 pageno;
928 LWLock *lock;
929
930 /*
931 * No work except at first XID of a page. But beware: just after
932 * wraparound, the first XID of page zero is FirstNormalTransactionId.
933 */
936 return;
937
939 lock = SimpleLruGetBankLock(XactCtl, pageno);
940
942
943 /* Zero the page and make a WAL entry about it */
944 SimpleLruZeroPage(XactCtl, pageno);
946
947 LWLockRelease(lock);
948}

References CLOG_ZEROPAGE, fb(), FirstNormalTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SimpleLruGetBankLock(), SimpleLruZeroPage(), TransactionIdEquals, TransactionIdToPage(), TransactionIdToPgIndex, XactCtl, and XLogSimpleInsertInt64().

Referenced by GetNewTransactionId().

◆ set_status_by_pages()

static void set_status_by_pages ( int  nsubxids,
TransactionId subxids,
XidStatus  status,
XLogRecPtr  lsn 
)
static

Definition at line 255 of file clog.c.

257{
258 int64 pageno = TransactionIdToPage(subxids[0]);
259 int offset = 0;
260 int i = 0;
261
262 Assert(nsubxids > 0); /* else the pageno fetch above is unsafe */
263
264 while (i < nsubxids)
265 {
266 int num_on_page = 0;
268
269 do
270 {
271 nextpageno = TransactionIdToPage(subxids[i]);
272 if (nextpageno != pageno)
273 break;
274 num_on_page++;
275 i++;
276 } while (i < nsubxids);
277
279 num_on_page, subxids + offset,
280 status, lsn, pageno, false);
281 offset = i;
282 pageno = nextpageno;
283 }
284}

References Assert, fb(), i, InvalidTransactionId, TransactionIdSetPageStatus(), and TransactionIdToPage().

Referenced by TransactionIdSetTreeStatus().

◆ StartupCLOG()

void StartupCLOG ( void  )

Definition at line 843 of file clog.c.

844{
846 int64 pageno = TransactionIdToPage(xid);
847
848 /*
849 * Initialize our idea of the latest page number.
850 */
851 pg_atomic_write_u64(&XactCtl->shared->latest_page_number, pageno);
852}

References TransamVariablesData::nextXid, pg_atomic_write_u64(), TransactionIdToPage(), TransamVariables, XactCtl, and XidFromFullTransactionId.

Referenced by StartupXLOG().

◆ TransactionGroupUpdateXidStatus()

static bool TransactionGroupUpdateXidStatus ( TransactionId  xid,
XidStatus  status,
XLogRecPtr  lsn,
int64  pageno 
)
static

Definition at line 440 of file clog.c.

442{
443 volatile PROC_HDR *procglobal = ProcGlobal;
444 PGPROC *proc = MyProc;
449
450 /* We should definitely have an XID whose status needs to be updated. */
452
453 /*
454 * Prepare to add ourselves to the list of processes needing a group XID
455 * status update.
456 */
457 proc->clogGroupMember = true;
458 proc->clogGroupMemberXid = xid;
459 proc->clogGroupMemberXidStatus = status;
460 proc->clogGroupMemberPage = pageno;
461 proc->clogGroupMemberLsn = lsn;
462
463 /*
464 * We put ourselves in the queue by writing MyProcNumber to
465 * ProcGlobal->clogGroupFirst. However, if there's already a process
466 * listed there, we compare our pageno with that of that process; if it
467 * differs, we cannot participate in the group, so we return for caller to
468 * update pg_xact in the normal way.
469 *
470 * If we're not the first process in the list, we must follow the leader.
471 * We do this by storing the data we want updated in our PGPROC entry
472 * where the leader can find it, then going to sleep.
473 *
474 * If no process is already in the list, we're the leader; our first step
475 * is to lock the SLRU bank to which our page belongs, then we close out
476 * the group by resetting the list pointer from ProcGlobal->clogGroupFirst
477 * (this lets other processes set up other groups later); finally we do
478 * the SLRU updates, release the SLRU bank lock, and wake up the sleeping
479 * processes.
480 *
481 * If another group starts to update a page in a different SLRU bank, they
482 * can proceed concurrently, since the bank lock they're going to use is
483 * different from ours. If another group starts to update a page in the
484 * same bank as ours, they wait until we release the lock.
485 */
486 nextidx = pg_atomic_read_u32(&procglobal->clogGroupFirst);
487
488 while (true)
489 {
490 /*
491 * Add the proc to list, if the clog page where we need to update the
492 * current transaction status is same as group leader's clog page.
493 *
494 * There is a race condition here, which is that after doing the below
495 * check and before adding this proc's clog update to a group, the
496 * group leader might have already finished the group update for this
497 * page and becomes group leader of another group, updating a
498 * different page. This will lead to a situation where a single group
499 * can have different clog page updates. This isn't likely and will
500 * still work, just less efficiently -- we handle this case by
501 * switching to a different bank lock in the loop below.
502 */
504 GetPGProcByNumber(nextidx)->clogGroupMemberPage != proc->clogGroupMemberPage)
505 {
506 /*
507 * Ensure that this proc is not a member of any clog group that
508 * needs an XID status update.
509 */
510 proc->clogGroupMember = false;
512 return false;
513 }
514
516
517 if (pg_atomic_compare_exchange_u32(&procglobal->clogGroupFirst,
518 &nextidx,
520 break;
521 }
522
523 /*
524 * If the list was not empty, the leader will update the status of our
525 * XID. It is impossible to have followers without a leader because the
526 * first process that has added itself to the list will always have
527 * nextidx as INVALID_PROC_NUMBER.
528 */
530 {
531 int extraWaits = 0;
532
533 /* Sleep until the leader updates our XID status. */
535 for (;;)
536 {
537 /* acts as a read barrier */
538 PGSemaphoreLock(proc->sem);
539 if (!proc->clogGroupMember)
540 break;
541 extraWaits++;
542 }
544
546
547 /* Fix semaphore count for any absorbed wakeups */
548 while (extraWaits-- > 0)
549 PGSemaphoreUnlock(proc->sem);
550 return true;
551 }
552
553 /*
554 * By here, we know we're the leader process. Acquire the SLRU bank lock
555 * that corresponds to the page we originally wanted to modify.
556 */
560
561 /*
562 * Now that we've got the lock, clear the list of processes waiting for
563 * group XID status update, saving a pointer to the head of the list.
564 * (Trying to pop elements one at a time could lead to an ABA problem.)
565 *
566 * At this point, any processes trying to do this would create a separate
567 * group.
568 */
569 nextidx = pg_atomic_exchange_u32(&procglobal->clogGroupFirst,
571
572 /* Remember head of list so we can perform wakeups after dropping lock. */
574
575 /* Walk the list and update the status of all XIDs. */
577 {
579 int64 thispageno = nextproc->clogGroupMemberPage;
580
581 /*
582 * If the page to update belongs to a different bank than the previous
583 * one, exchange bank lock to the new one. This should be quite rare,
584 * as described above.
585 *
586 * (We could try to optimize this by waking up the processes for which
587 * we have already updated the status while we exchange the lock, but
588 * the code doesn't do that at present. I think it'd require
589 * additional bookkeeping, making the common path slower in order to
590 * improve an infrequent case.)
591 */
592 if (thispageno != prevpageno)
593 {
595
596 if (prevlock != lock)
597 {
600 }
601 prevlock = lock;
603 }
604
605 /*
606 * Transactions with more than THRESHOLD_SUBTRANS_CLOG_OPT sub-XIDs
607 * should not use group XID status update mechanism.
608 */
609 Assert(nextproc->subxidStatus.count <= THRESHOLD_SUBTRANS_CLOG_OPT);
610
612 nextproc->subxidStatus.count,
613 nextproc->subxids.xids,
614 nextproc->clogGroupMemberXidStatus,
615 nextproc->clogGroupMemberLsn,
616 nextproc->clogGroupMemberPage);
617
618 /* Move to next proc in list. */
619 nextidx = pg_atomic_read_u32(&nextproc->clogGroupNext);
620 }
621
622 /* We're done with the lock now. */
623 if (prevlock != NULL)
625
626 /*
627 * Now that we've released the lock, go back and wake everybody up. We
628 * don't do this under the lock so as to keep lock hold times to a
629 * minimum.
630 *
631 * (Perhaps we could do this in two passes, the first setting
632 * clogGroupNext to invalid while saving the semaphores to an array, then
633 * a single write barrier, then another pass unlocking the semaphores.)
634 */
636 {
638
639 wakeidx = pg_atomic_read_u32(&wakeproc->clogGroupNext);
641
642 /* ensure all previous writes are visible before follower continues. */
644
645 wakeproc->clogGroupMember = false;
646
647 if (wakeproc != MyProc)
649 }
650
651 return true;
652}

References Assert, PGPROC::clogGroupMember, PGPROC::clogGroupMemberLsn, PGPROC::clogGroupMemberPage, PGPROC::clogGroupMemberXid, PGPROC::clogGroupMemberXidStatus, PGPROC::clogGroupNext, fb(), GetPGProcByNumber, INVALID_PROC_NUMBER, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProc, MyProcNumber, pg_atomic_compare_exchange_u32(), pg_atomic_exchange_u32(), pg_atomic_read_u32(), pg_atomic_write_u32(), pg_write_barrier, PGSemaphoreLock(), PGSemaphoreUnlock(), pgstat_report_wait_end(), pgstat_report_wait_start(), ProcGlobal, PGPROC::sem, SimpleLruGetBankLock(), THRESHOLD_SUBTRANS_CLOG_OPT, TransactionIdIsValid, TransactionIdSetPageStatusInternal(), and XactCtl.

Referenced by TransactionIdSetPageStatus().

◆ TransactionIdGetStatus()

XidStatus TransactionIdGetStatus ( TransactionId  xid,
XLogRecPtr lsn 
)

Definition at line 734 of file clog.c.

735{
736 int64 pageno = TransactionIdToPage(xid);
737 int byteno = TransactionIdToByte(xid);
739 int slotno;
740 int lsnindex;
741 char *byteptr;
742 XidStatus status;
743
744 /* lock is acquired by SimpleLruReadPage_ReadOnly */
745
747 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
748
749 status = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
750
752 *lsn = XactCtl->shared->group_lsn[lsnindex];
753
755
756 return status;
757}

References CLOG_BITS_PER_XACT, CLOG_XACT_BITMASK, fb(), GetLSNIndex, LWLockRelease(), SimpleLruGetBankLock(), SimpleLruReadPage_ReadOnly(), TransactionIdToBIndex, TransactionIdToByte, TransactionIdToPage(), and XactCtl.

Referenced by TransactionIdGetCommitLSN(), and TransactionLogFetch().

◆ TransactionIdSetPageStatus()

static void TransactionIdSetPageStatus ( TransactionId  xid,
int  nsubxids,
TransactionId subxids,
XidStatus  status,
XLogRecPtr  lsn,
int64  pageno,
bool  all_xact_same_page 
)
static

Definition at line 291 of file clog.c.

295{
296 LWLock *lock;
297
298 /* Can't use group update when PGPROC overflows. */
300 "group clog threshold less than PGPROC cached subxids");
301
302 /* Get the SLRU bank lock for the page we are going to access. */
303 lock = SimpleLruGetBankLock(XactCtl, pageno);
304
305 /*
306 * When there is contention on the SLRU bank lock we need, we try to group
307 * multiple updates; a single leader process will perform transaction
308 * status updates for multiple backends so that the number of times the
309 * bank lock needs to be acquired is reduced.
310 *
311 * For this optimization to be safe, the XID and subxids in MyProc must be
312 * the same as the ones for which we're setting the status. Check that
313 * this is the case.
314 *
315 * For this optimization to be efficient, we shouldn't have too many
316 * sub-XIDs and all of the XIDs for which we're adjusting clog should be
317 * on the same page. Check those conditions, too.
318 */
319 if (all_xact_same_page && xid == MyProc->xid &&
322 (nsubxids == 0 ||
323 memcmp(subxids, MyProc->subxids.xids,
324 nsubxids * sizeof(TransactionId)) == 0))
325 {
326 /*
327 * If we can immediately acquire the lock, we update the status of our
328 * own XID and release the lock. If not, try use group XID update. If
329 * that doesn't work out, fall back to waiting for the lock to perform
330 * an update for this transaction only.
331 */
333 {
334 /* Got the lock without waiting! Do the update. */
335 TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
336 lsn, pageno);
337 LWLockRelease(lock);
338 return;
339 }
340 else if (TransactionGroupUpdateXidStatus(xid, status, lsn, pageno))
341 {
342 /* Group update mechanism has done the work. */
343 return;
344 }
345
346 /* Fall through only if update isn't done yet. */
347 }
348
349 /* Group update not applicable, or couldn't accept this page number. */
351 TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
352 lsn, pageno);
353 LWLockRelease(lock);
354}

References XidCacheStatus::count, fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), LWLockRelease(), MyProc, PGPROC_MAX_CACHED_SUBXIDS, SimpleLruGetBankLock(), StaticAssertDecl, PGPROC::subxids, PGPROC::subxidStatus, THRESHOLD_SUBTRANS_CLOG_OPT, TransactionGroupUpdateXidStatus(), TransactionIdSetPageStatusInternal(), XactCtl, PGPROC::xid, and XidCache::xids.

Referenced by set_status_by_pages(), and TransactionIdSetTreeStatus().

◆ TransactionIdSetPageStatusInternal()

static void TransactionIdSetPageStatusInternal ( TransactionId  xid,
int  nsubxids,
TransactionId subxids,
XidStatus  status,
XLogRecPtr  lsn,
int64  pageno 
)
static

Definition at line 362 of file clog.c.

365{
366 int slotno;
367 int i;
368
370 status == TRANSACTION_STATUS_ABORTED ||
373 LW_EXCLUSIVE));
374
375 /*
376 * If we're doing an async commit (ie, lsn is valid), then we must wait
377 * for any active write on the page slot to complete. Otherwise our
378 * update could reach disk in that write, which will not do since we
379 * mustn't let it reach disk until we've done the appropriate WAL flush.
380 * But when lsn is invalid, it's OK to scribble on a page while it is
381 * write-busy, since we don't care if the update reaches disk sooner than
382 * we think.
383 */
385 xid);
386
387 /*
388 * Set the main transaction id, if any.
389 *
390 * If we update more than one xid on this page while it is being written
391 * out, we might find that some of the bits go to disk and others don't.
392 * If we are updating commits on the page with the top-level xid that
393 * could break atomicity, so we subcommit the subxids first before we mark
394 * the top-level commit.
395 */
396 if (TransactionIdIsValid(xid))
397 {
398 /* Subtransactions first, if needed ... */
399 if (status == TRANSACTION_STATUS_COMMITTED)
400 {
401 for (i = 0; i < nsubxids; i++)
402 {
403 Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
406 lsn, slotno);
407 }
408 }
409
410 /* ... then the main transaction */
411 TransactionIdSetStatusBit(xid, status, lsn, slotno);
412 }
413
414 /* Set the subtransactions */
415 for (i = 0; i < nsubxids; i++)
416 {
417 Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
418 TransactionIdSetStatusBit(subxids[i], status, lsn, slotno);
419 }
420
421 XactCtl->shared->page_dirty[slotno] = true;
422}

References Assert, fb(), i, LW_EXCLUSIVE, LWLockHeldByMeInMode(), SimpleLruGetBankLock(), SimpleLruReadPage(), TRANSACTION_STATUS_ABORTED, TRANSACTION_STATUS_COMMITTED, TRANSACTION_STATUS_SUB_COMMITTED, TransactionIdIsValid, TransactionIdSetStatusBit(), TransactionIdToPage(), XactCtl, and XLogRecPtrIsValid.

Referenced by TransactionGroupUpdateXidStatus(), and TransactionIdSetPageStatus().

◆ TransactionIdSetStatusBit()

static void TransactionIdSetStatusBit ( TransactionId  xid,
XidStatus  status,
XLogRecPtr  lsn,
int  slotno 
)
static

Definition at line 660 of file clog.c.

661{
662 int byteno = TransactionIdToByte(xid);
664 char *byteptr;
665 char byteval;
666 char curval;
667
668 Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(xid));
670 XactCtl->shared->page_number[slotno]),
671 LW_EXCLUSIVE));
672
673 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
675
676 /*
677 * When replaying transactions during recovery we still need to perform
678 * the two phases of subcommit and then commit. However, some transactions
679 * are already correctly marked, so we just treat those as a no-op which
680 * allows us to keep the following Assert as restrictive as possible.
681 */
684 return;
685
686 /*
687 * Current state change should be from 0 or subcommitted to target state
688 * or we should already be there when replaying changes during recovery.
689 */
690 Assert(curval == 0 ||
693 curval == status);
694
695 /* note this assumes exclusive access to the clog page */
696 byteval = *byteptr;
697 byteval &= ~(((1 << CLOG_BITS_PER_XACT) - 1) << bshift);
698 byteval |= (status << bshift);
699 *byteptr = byteval;
700
701 /*
702 * Update the group LSN if the transaction completion LSN is higher.
703 *
704 * Note: lsn will be invalid when supplied during InRecovery processing,
705 * so we don't need to do anything special to avoid LSN updates during
706 * recovery. After recovery completes the next clog change will set the
707 * LSN correctly.
708 */
709 if (XLogRecPtrIsValid(lsn))
710 {
711 int lsnindex = GetLSNIndex(slotno, xid);
712
713 if (XactCtl->shared->group_lsn[lsnindex] < lsn)
714 XactCtl->shared->group_lsn[lsnindex] = lsn;
715 }
716}

References Assert, CLOG_BITS_PER_XACT, CLOG_XACT_BITMASK, fb(), GetLSNIndex, InRecovery, LW_EXCLUSIVE, LWLockHeldByMeInMode(), SimpleLruGetBankLock(), TRANSACTION_STATUS_COMMITTED, TRANSACTION_STATUS_IN_PROGRESS, TRANSACTION_STATUS_SUB_COMMITTED, TransactionIdToBIndex, TransactionIdToByte, TransactionIdToPage(), XactCtl, and XLogRecPtrIsValid.

Referenced by TransactionIdSetPageStatusInternal().

◆ TransactionIdSetTreeStatus()

void TransactionIdSetTreeStatus ( TransactionId  xid,
int  nsubxids,
TransactionId subxids,
XidStatus  status,
XLogRecPtr  lsn 
)

Definition at line 181 of file clog.c.

183{
184 int64 pageno = TransactionIdToPage(xid); /* get page of parent */
185 int i;
186
189
190 /*
191 * See how many subxids, if any, are on the same page as the parent, if
192 * any.
193 */
194 for (i = 0; i < nsubxids; i++)
195 {
196 if (TransactionIdToPage(subxids[i]) != pageno)
197 break;
198 }
199
200 /*
201 * Do all items fit on a single page?
202 */
203 if (i == nsubxids)
204 {
205 /*
206 * Set the parent and all subtransactions in a single call
207 */
208 TransactionIdSetPageStatus(xid, nsubxids, subxids, status, lsn,
209 pageno, true);
210 }
211 else
212 {
214
215 /*
216 * If this is a commit then we care about doing this correctly (i.e.
217 * using the subcommitted intermediate status). By here, we know
218 * we're updating more than one page of clog, so we must mark entries
219 * that are *not* on the first page so that they show as subcommitted
220 * before we then return to update the status to fully committed.
221 *
222 * To avoid touching the first page twice, skip marking subcommitted
223 * for the subxids on that first page.
224 */
225 if (status == TRANSACTION_STATUS_COMMITTED)
227 subxids + nsubxids_on_first_page,
229
230 /*
231 * Now set the parent and subtransactions on same page as the parent,
232 * if any
233 */
234 pageno = TransactionIdToPage(xid);
236 lsn, pageno, false);
237
238 /*
239 * Now work through the rest of the subxids one clog page at a time,
240 * starting from the second page onwards, like we did above.
241 */
243 subxids + nsubxids_on_first_page,
244 status, lsn);
245 }
246}

References Assert, fb(), i, set_status_by_pages(), TRANSACTION_STATUS_ABORTED, TRANSACTION_STATUS_COMMITTED, TRANSACTION_STATUS_SUB_COMMITTED, TransactionIdSetPageStatus(), and TransactionIdToPage().

Referenced by TransactionIdAbortTree(), TransactionIdAsyncCommitTree(), and TransactionIdCommitTree().

◆ TransactionIdToPage()

◆ TrimCLOG()

void TrimCLOG ( void  )

Definition at line 858 of file clog.c.

859{
861 int64 pageno = TransactionIdToPage(xid);
862 LWLock *lock = SimpleLruGetBankLock(XactCtl, pageno);
863
865
866 /*
867 * Zero out the remainder of the current clog page. Under normal
868 * circumstances it should be zeroes already, but it seems at least
869 * theoretically possible that XLOG replay will have settled on a nextXID
870 * value that is less than the last XID actually used and marked by the
871 * previous database lifecycle (since subtransaction commit writes clog
872 * but makes no WAL entry). Let's just be safe. (We need not worry about
873 * pages beyond the current one, since those will be zeroed when first
874 * used. For the same reason, there is no need to do anything when
875 * nextXid is exactly at a page boundary; and it's likely that the
876 * "current" page doesn't exist yet in that case.)
877 */
878 if (TransactionIdToPgIndex(xid) != 0)
879 {
880 int byteno = TransactionIdToByte(xid);
882 int slotno;
883 char *byteptr;
884
885 slotno = SimpleLruReadPage(XactCtl, pageno, false, xid);
886 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
887
888 /* Zero so-far-unused positions in the current byte */
889 *byteptr &= (1 << bshift) - 1;
890 /* Zero the rest of the page */
891 MemSet(byteptr + 1, 0, BLCKSZ - byteno - 1);
892
893 XactCtl->shared->page_dirty[slotno] = true;
894 }
895
896 LWLockRelease(lock);
897}

References CLOG_BITS_PER_XACT, fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemSet, TransamVariablesData::nextXid, SimpleLruGetBankLock(), SimpleLruReadPage(), TransactionIdToBIndex, TransactionIdToByte, TransactionIdToPage(), TransactionIdToPgIndex, TransamVariables, XactCtl, and XidFromFullTransactionId.

Referenced by StartupXLOG().

◆ TruncateCLOG()

void TruncateCLOG ( TransactionId  oldestXact,
Oid  oldestxid_datoid 
)

Definition at line 967 of file clog.c.

968{
970
971 /*
972 * The cutoff point is the start of the segment containing oldestXact. We
973 * pass the *page* containing oldestXact to SimpleLruTruncate.
974 */
975 cutoffPage = TransactionIdToPage(oldestXact);
976
977 /* Check to see if there's any files that could be removed */
979 return; /* nothing to remove */
980
981 /*
982 * Advance oldestClogXid before truncating clog, so concurrent xact status
983 * lookups can ensure they don't attempt to access truncated-away clog.
984 *
985 * It's only necessary to do this if we will actually truncate away clog
986 * pages.
987 */
988 AdvanceOldestClogXid(oldestXact);
989
990 /*
991 * Write XLOG record and flush XLOG to disk. We record the oldest xid
992 * we're keeping information about here so we can ensure that it's always
993 * ahead of clog truncation in case we crash, and so a standby finds out
994 * the new valid xid before the next checkpoint.
995 */
997
998 /* Now we can remove the old CLOG segment(s) */
1000}

References AdvanceOldestClogXid(), fb(), SimpleLruTruncate(), SlruScanDirCbReportPresence(), SlruScanDirectory(), TransactionIdToPage(), WriteTruncateXlogRec(), and XactCtl.

Referenced by vac_truncate_clog().

◆ WriteTruncateXlogRec()

static void WriteTruncateXlogRec ( int64  pageno,
TransactionId  oldestXact,
Oid  oldestXactDb 
)
static

Definition at line 1044 of file clog.c.

1045{
1048
1049 xlrec.pageno = pageno;
1050 xlrec.oldestXact = oldestXact;
1051 xlrec.oldestXactDb = oldestXactDb;
1052
1057}

References CLOG_TRUNCATE, fb(), xl_clog_truncate::pageno, XLogBeginInsert(), XLogFlush(), XLogInsert(), and XLogRegisterData().

Referenced by TruncateCLOG().

Variable Documentation

◆ XactCtlData

SlruCtlData XactCtlData
static

Definition at line 108 of file clog.c.