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 "utils/wait_event.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 int clog_errdetail_for_io_error (const void *opaque_data)
 
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 63 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 94 of file clog.c.

◆ CLOG_MAX_ALLOWED_BUFFERS

#define CLOG_MAX_ALLOWED_BUFFERS
Value:
#define Min(x, y)
Definition c.h:1075
#define CLOG_XACTS_PER_PAGE
Definition clog.c:65
#define SLRU_MAX_ALLOWED_BUFFERS
Definition slru.h:25
#define MaxTransactionId
Definition transam.h:35

Definition at line 73 of file clog.c.

83{
84 return xid / (int64) CLOG_XACTS_PER_PAGE;
85}
86
87#define TransactionIdToPgIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE)
88#define TransactionIdToByte(xid) (TransactionIdToPgIndex(xid) / CLOG_XACTS_PER_BYTE)
89#define TransactionIdToBIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_BYTE)
90
91/* We store the latest async LSN for each group of transactions */
92#define CLOG_XACTS_PER_LSN_GROUP 32 /* keep this a power of 2 */
93#define CLOG_LSNS_PER_PAGE (CLOG_XACTS_PER_PAGE / CLOG_XACTS_PER_LSN_GROUP)
94
95#define GetLSNIndex(slotno, xid) ((slotno) * CLOG_LSNS_PER_PAGE + \
96 ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE) / CLOG_XACTS_PER_LSN_GROUP)
97
98/*
99 * The number of subtransactions below which we consider to apply clog group
100 * update optimization. Testing reveals that the number higher than this can
101 * hurt performance.
102 */
103#define THRESHOLD_SUBTRANS_CLOG_OPT 5
104
105/*
106 * Link to shared-memory data structures for CLOG control
107 */
109
110#define XactCtl (&XactCtlData)
111
112
114static int clog_errdetail_for_io_error(const void *opaque_data);
115static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact,
116 Oid oldestXactDb);
118 TransactionId *subxids, XidStatus status,
119 XLogRecPtr lsn, int64 pageno,
120 bool all_xact_same_page);
122 XLogRecPtr lsn, int slotno);
123static void set_status_by_pages(int nsubxids, TransactionId *subxids,
124 XidStatus status, XLogRecPtr lsn);
126 XidStatus status, XLogRecPtr lsn, int64 pageno);
128 TransactionId *subxids, XidStatus status,
129 XLogRecPtr lsn, int64 pageno);
130
131
132/*
133 * TransactionIdSetTreeStatus
134 *
135 * Record the final state of transaction entries in the commit log for
136 * a transaction and its subtransaction tree. Take care to ensure this is
137 * efficient, and as atomic as possible.
138 *
139 * xid is a single xid to set status for. This will typically be
140 * the top level transactionid for a top level commit or abort. It can
141 * also be a subtransaction when we record transaction aborts.
142 *
143 * subxids is an array of xids of length nsubxids, representing subtransactions
144 * in the tree of xid. In various cases nsubxids may be zero.
145 *
146 * lsn must be the WAL location of the commit record when recording an async
147 * commit. For a synchronous commit it can be InvalidXLogRecPtr, since the
148 * caller guarantees the commit record is already flushed in that case. It
149 * should be InvalidXLogRecPtr for abort cases, too.
150 *
151 * In the commit case, atomicity is limited by whether all the subxids are in
152 * the same CLOG page as xid. If they all are, then the lock will be grabbed
153 * only once, and the status will be set to committed directly. Otherwise
154 * we must
155 * 1. set sub-committed all subxids that are not on the same page as the
156 * main xid
157 * 2. atomically set committed the main xid and the subxids on the same page
158 * 3. go over the first bunch again and set them committed
159 * Note that as far as concurrent checkers are concerned, main transaction
160 * commit as a whole is still atomic.
161 *
162 * Example:
163 * TransactionId t commits and has subxids t1, t2, t3, t4
164 * t is on page p1, t1 is also on p1, t2 and t3 are on p2, t4 is on p3
165 * 1. update pages2-3:
166 * page2: set t2,t3 as sub-committed
167 * page3: set t4 as sub-committed
168 * 2. update page1:
169 * page1: set t,t1 as committed
170 * 3. update pages2-3:
171 * page2: set t2,t3 as committed
172 * page3: set t4 as committed
173 *
174 * NB: this is a low-level routine and is NOT the preferred entry point
175 * for most uses; functions in transam.c are the intended callers.
176 *
177 * XXX Think about issuing POSIX_FADV_WILLNEED on pages that we will need,
178 * but aren't yet in cache, as well as hinting pages not to fall out of
179 * cache yet.
180 */
181void
183 TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
184{
185 int64 pageno = TransactionIdToPage(xid); /* get page of parent */
186 int i;
187
190
191 /*
192 * See how many subxids, if any, are on the same page as the parent, if
193 * any.
194 */
195 for (i = 0; i < nsubxids; i++)
196 {
197 if (TransactionIdToPage(subxids[i]) != pageno)
198 break;
199 }
200
201 /*
202 * Do all items fit on a single page?
203 */
204 if (i == nsubxids)
205 {
206 /*
207 * Set the parent and all subtransactions in a single call
208 */
209 TransactionIdSetPageStatus(xid, nsubxids, subxids, status, lsn,
210 pageno, true);
211 }
212 else
213 {
215
216 /*
217 * If this is a commit then we care about doing this correctly (i.e.
218 * using the subcommitted intermediate status). By here, we know
219 * we're updating more than one page of clog, so we must mark entries
220 * that are *not* on the first page so that they show as subcommitted
221 * before we then return to update the status to fully committed.
222 *
223 * To avoid touching the first page twice, skip marking subcommitted
224 * for the subxids on that first page.
225 */
226 if (status == TRANSACTION_STATUS_COMMITTED)
228 subxids + nsubxids_on_first_page,
230
231 /*
232 * Now set the parent and subtransactions on same page as the parent,
233 * if any
234 */
235 pageno = TransactionIdToPage(xid);
237 lsn, pageno, false);
238
239 /*
240 * Now work through the rest of the subxids one clog page at a time,
241 * starting from the second page onwards, like we did above.
242 */
244 subxids + nsubxids_on_first_page,
245 status, lsn);
246 }
247}
248
249/*
250 * Helper for TransactionIdSetTreeStatus: set the status for a bunch of
251 * transactions, chunking in the separate CLOG pages involved. We never
252 * pass the whole transaction tree to this function, only subtransactions
253 * that are on different pages to the top level transaction id.
254 */
255static void
257 XidStatus status, XLogRecPtr lsn)
258{
259 int64 pageno = TransactionIdToPage(subxids[0]);
260 int offset = 0;
261 int i = 0;
262
263 Assert(nsubxids > 0); /* else the pageno fetch above is unsafe */
264
265 while (i < nsubxids)
266 {
267 int num_on_page = 0;
269
270 do
271 {
272 nextpageno = TransactionIdToPage(subxids[i]);
273 if (nextpageno != pageno)
274 break;
275 num_on_page++;
276 i++;
277 } while (i < nsubxids);
278
280 num_on_page, subxids + offset,
281 status, lsn, pageno, false);
282 offset = i;
283 pageno = nextpageno;
284 }
285}
286
287/*
288 * Record the final state of transaction entries in the commit log for all
289 * entries on a single page. Atomic only on this page.
290 */
291static void
293 TransactionId *subxids, XidStatus status,
294 XLogRecPtr lsn, int64 pageno,
296{
297 LWLock *lock;
298
299 /* Can't use group update when PGPROC overflows. */
301 "group clog threshold less than PGPROC cached subxids");
302
303 /* Get the SLRU bank lock for the page we are going to access. */
304 lock = SimpleLruGetBankLock(XactCtl, pageno);
305
306 /*
307 * When there is contention on the SLRU bank lock we need, we try to group
308 * multiple updates; a single leader process will perform transaction
309 * status updates for multiple backends so that the number of times the
310 * bank lock needs to be acquired is reduced.
311 *
312 * For this optimization to be safe, the XID and subxids in MyProc must be
313 * the same as the ones for which we're setting the status. Check that
314 * this is the case.
315 *
316 * For this optimization to be efficient, we shouldn't have too many
317 * sub-XIDs and all of the XIDs for which we're adjusting clog should be
318 * on the same page. Check those conditions, too.
319 */
320 if (all_xact_same_page && xid == MyProc->xid &&
323 (nsubxids == 0 ||
324 memcmp(subxids, MyProc->subxids.xids,
325 nsubxids * sizeof(TransactionId)) == 0))
326 {
327 /*
328 * If we can immediately acquire the lock, we update the status of our
329 * own XID and release the lock. If not, try use group XID update. If
330 * that doesn't work out, fall back to waiting for the lock to perform
331 * an update for this transaction only.
332 */
334 {
335 /* Got the lock without waiting! Do the update. */
336 TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
337 lsn, pageno);
338 LWLockRelease(lock);
339 return;
340 }
341 else if (TransactionGroupUpdateXidStatus(xid, status, lsn, pageno))
342 {
343 /* Group update mechanism has done the work. */
344 return;
345 }
346
347 /* Fall through only if update isn't done yet. */
348 }
349
350 /* Group update not applicable, or couldn't accept this page number. */
352 TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
353 lsn, pageno);
354 LWLockRelease(lock);
355}
356
357/*
358 * Record the final state of transaction entry in the commit log
359 *
360 * We don't do any locking here; caller must handle that.
361 */
362static void
364 TransactionId *subxids, XidStatus status,
365 XLogRecPtr lsn, int64 pageno)
366{
367 int slotno;
368 int i;
369
371 status == TRANSACTION_STATUS_ABORTED ||
374 LW_EXCLUSIVE));
375
376 /*
377 * If we're doing an async commit (ie, lsn is valid), then we must wait
378 * for any active write on the page slot to complete. Otherwise our
379 * update could reach disk in that write, which will not do since we
380 * mustn't let it reach disk until we've done the appropriate WAL flush.
381 * But when lsn is invalid, it's OK to scribble on a page while it is
382 * write-busy, since we don't care if the update reaches disk sooner than
383 * we think.
384 */
385 slotno = SimpleLruReadPage(XactCtl, pageno, !XLogRecPtrIsValid(lsn), &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}
423
424/*
425 * Subroutine for TransactionIdSetPageStatus, q.v.
426 *
427 * When we cannot immediately acquire the SLRU bank lock in exclusive mode at
428 * commit time, add ourselves to a list of processes that need their XIDs
429 * status update. The first process to add itself to the list will acquire
430 * the lock in exclusive mode and set transaction status as required on behalf
431 * of all group members. This avoids a great deal of contention when many
432 * processes are trying to commit at once, since the lock need not be
433 * repeatedly handed off from one committing process to the next.
434 *
435 * Returns true when transaction status has been updated in clog; returns
436 * false if we decided against applying the optimization because the page
437 * number we need to update differs from those processes already waiting.
438 */
439static bool
441 XLogRecPtr lsn, int64 pageno)
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}
653
654/*
655 * Sets the commit status of a single transaction.
656 *
657 * Caller must hold the corresponding SLRU bank lock, will be held at exit.
658 */
659static void
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}
717
718/*
719 * Interrogate the state of a transaction in the commit log.
720 *
721 * Aside from the actual commit status, this function returns (into *lsn)
722 * an LSN that is late enough to be able to guarantee that if we flush up to
723 * that LSN then we will have flushed the transaction's commit record to disk.
724 * The result is not necessarily the exact LSN of the transaction's commit
725 * record! For example, for long-past transactions (those whose clog pages
726 * already migrated to disk), we'll return InvalidXLogRecPtr. Also, because
727 * we group transactions on the same clog page to conserve storage, we might
728 * return the LSN of a later transaction that falls into the same group.
729 *
730 * NB: this is a low-level routine and is NOT the preferred entry point
731 * for most uses; TransactionLogFetch() in transam.c is the intended caller.
732 */
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}
758
759/*
760 * Number of shared CLOG buffers.
761 *
762 * If asked to autotune, use 2MB for every 1GB of shared buffers, up to 8MB.
763 * Otherwise just cap the configured amount to be between 16 and the maximum
764 * allowed.
765 */
766static int
768{
769 /* auto-tune based on shared buffers */
770 if (transaction_buffers == 0)
771 return SimpleLruAutotuneBuffers(512, 1024);
772
774}
775
776/*
777 * Initialization of shared memory for CLOG
778 */
779Size
780CLOGShmemSize(void)
781{
783}
784
785void
786CLOGShmemInit(void)
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;
810 XactCtl->errdetail_for_io_error = clog_errdetail_for_io_error;
812 "pg_xact", LWTRANCHE_XACT_BUFFER,
815}
816
817/*
818 * GUC check_hook for transaction_buffers
819 */
820bool
822{
823 return check_slru_buffers("transaction_buffers", newval);
824}
825
826/*
827 * This func must be called ONCE on system install. It creates
828 * the initial CLOG segment. (The CLOG directory is assumed to
829 * have been created by initdb, and CLOGShmemInit must have been
830 * called already.)
831 */
832void
833BootStrapCLOG(void)
834{
835 /* Zero the initial page and flush it to disk */
837}
838
839/*
840 * This must be called ONCE during postmaster or standalone-backend startup,
841 * after StartupXLOG has initialized TransamVariables->nextXid.
842 */
843void
844StartupCLOG(void)
845{
847 int64 pageno = TransactionIdToPage(xid);
848
849 /*
850 * Initialize our idea of the latest page number.
851 */
852 pg_atomic_write_u64(&XactCtl->shared->latest_page_number, pageno);
853}
854
855/*
856 * This must be called ONCE at the end of startup/recovery.
857 */
858void
859TrimCLOG(void)
860{
862 int64 pageno = TransactionIdToPage(xid);
863 LWLock *lock = SimpleLruGetBankLock(XactCtl, pageno);
864
866
867 /*
868 * Zero out the remainder of the current clog page. Under normal
869 * circumstances it should be zeroes already, but it seems at least
870 * theoretically possible that XLOG replay will have settled on a nextXID
871 * value that is less than the last XID actually used and marked by the
872 * previous database lifecycle (since subtransaction commit writes clog
873 * but makes no WAL entry). Let's just be safe. (We need not worry about
874 * pages beyond the current one, since those will be zeroed when first
875 * used. For the same reason, there is no need to do anything when
876 * nextXid is exactly at a page boundary; and it's likely that the
877 * "current" page doesn't exist yet in that case.)
878 */
879 if (TransactionIdToPgIndex(xid) != 0)
880 {
881 int byteno = TransactionIdToByte(xid);
883 int slotno;
884 char *byteptr;
885
886 slotno = SimpleLruReadPage(XactCtl, pageno, false, &xid);
887 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
888
889 /* Zero so-far-unused positions in the current byte */
890 *byteptr &= (1 << bshift) - 1;
891 /* Zero the rest of the page */
892 MemSet(byteptr + 1, 0, BLCKSZ - byteno - 1);
893
894 XactCtl->shared->page_dirty[slotno] = true;
895 }
896
897 LWLockRelease(lock);
898}
899
900/*
901 * Perform a checkpoint --- either during shutdown, or on-the-fly
902 */
903void
904CheckPointCLOG(void)
905{
906 /*
907 * Write dirty CLOG pages to disk. This may result in sync requests
908 * queued for later handling by ProcessSyncRequests(), as part of the
909 * checkpoint.
910 */
914}
915
916
917/*
918 * Make sure that CLOG has room for a newly-allocated XID.
919 *
920 * NB: this is called while holding XidGenLock. We want it to be very fast
921 * most of the time; even when it's not so fast, no actual I/O need happen
922 * unless we're forced to write out a dirty clog or xlog page to make room
923 * in shared memory.
924 */
925void
927{
928 int64 pageno;
929 LWLock *lock;
930
931 /*
932 * No work except at first XID of a page. But beware: just after
933 * wraparound, the first XID of page zero is FirstNormalTransactionId.
934 */
937 return;
938
940 lock = SimpleLruGetBankLock(XactCtl, pageno);
941
943
944 /* Zero the page and make a WAL entry about it */
945 SimpleLruZeroPage(XactCtl, pageno);
947
948 LWLockRelease(lock);
949}
950
951
952/*
953 * Remove all CLOG segments before the one holding the passed transaction ID
954 *
955 * Before removing any CLOG data, we must flush XLOG to disk, to ensure that
956 * any recently-emitted records with freeze plans have reached disk; otherwise
957 * a crash and restart might leave us with some unfrozen tuples referencing
958 * removed CLOG data. We choose to emit a special TRUNCATE XLOG record too.
959 * Replaying the deletion from XLOG is not critical, since the files could
960 * just as well be removed later, but doing so prevents a long-running hot
961 * standby server from acquiring an unreasonably bloated CLOG directory.
962 *
963 * Since CLOG segments hold a large number of transactions, the opportunity to
964 * actually remove a segment is fairly rare, and so it seems best not to do
965 * the XLOG flush unless we have confirmed that there is a removable segment.
966 */
967void
969{
971
972 /*
973 * The cutoff point is the start of the segment containing oldestXact. We
974 * pass the *page* containing oldestXact to SimpleLruTruncate.
975 */
976 cutoffPage = TransactionIdToPage(oldestXact);
977
978 /* Check to see if there's any files that could be removed */
980 return; /* nothing to remove */
981
982 /*
983 * Advance oldestClogXid before truncating clog, so concurrent xact status
984 * lookups can ensure they don't attempt to access truncated-away clog.
985 *
986 * It's only necessary to do this if we will actually truncate away clog
987 * pages.
988 */
989 AdvanceOldestClogXid(oldestXact);
990
991 /*
992 * Write XLOG record and flush XLOG to disk. We record the oldest xid
993 * we're keeping information about here so we can ensure that it's always
994 * ahead of clog truncation in case we crash, and so a standby finds out
995 * the new valid xid before the next checkpoint.
996 */
998
999 /* Now we can remove the old CLOG segment(s) */
1001}
1002
1003
1004/*
1005 * Decide whether a CLOG page number is "older" for truncation purposes.
1006 *
1007 * We need to use comparison of TransactionIds here in order to do the right
1008 * thing with wraparound XID arithmetic. However, TransactionIdPrecedes()
1009 * would get weird about permanent xact IDs. So, offset both such that xid1,
1010 * xid2, and xid2 + CLOG_XACTS_PER_PAGE - 1 are all normal XIDs; this offset
1011 * is relevant to page 0 and to the page preceding page 0.
1012 *
1013 * The page containing oldestXact-2^31 is the important edge case. The
1014 * portion of that page equaling or following oldestXact-2^31 is expendable,
1015 * but the portion preceding oldestXact-2^31 is not. When oldestXact-2^31 is
1016 * the first XID of a page and segment, the entire page and segment is
1017 * expendable, and we could truncate the segment. Recognizing that case would
1018 * require making oldestXact, not just the page containing oldestXact,
1019 * available to this callback. The benefit would be rare and small, so we
1020 * don't optimize that edge case.
1021 */
1022static bool
1024{
1027
1032
1033 return (TransactionIdPrecedes(xid1, xid2) &&
1035}
1036
1037static int
1039{
1040 TransactionId xid = *(const TransactionId *) opaque_data;
1041
1042 return errdetail("Could not access commit status of transaction %u.", xid);
1043}
1044
1045
1046/*
1047 * Write a TRUNCATE xlog record
1048 *
1049 * We must flush the xlog record to disk before returning --- see notes
1050 * in TruncateCLOG().
1051 */
1052static void
1053WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact, Oid oldestXactDb)
1054{
1057
1058 xlrec.pageno = pageno;
1059 xlrec.oldestXact = oldestXact;
1060 xlrec.oldestXactDb = oldestXactDb;
1061
1066}
1067
1068/*
1069 * CLOG resource manager's routines
1070 */
1071void
1073{
1074 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1075
1076 /* Backup blocks are not used in clog records */
1078
1079 if (info == CLOG_ZEROPAGE)
1080 {
1081 int64 pageno;
1082
1083 memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1085 }
1086 else if (info == CLOG_TRUNCATE)
1087 {
1089
1090 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_clog_truncate));
1091
1092 AdvanceOldestClogXid(xlrec.oldestXact);
1093
1095 }
1096 else
1097 elog(PANIC, "clog_redo: unknown op code %u", info);
1098}
1099
1100/*
1101 * Entrypoint for sync.c to sync clog files.
1102 */
1103int
1104clogsyncfiletag(const FileTag *ftag, char *path)
1105{
1106 return SlruSyncFileTag(XactCtl, ftag, path);
1107}
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:598
#define Max(x, y)
Definition c.h:1069
#define Assert(condition)
Definition c.h:927
int64_t int64
Definition c.h:597
uint32_t uint32
Definition c.h:600
#define MemSet(start, val, len)
Definition c.h:1091
#define StaticAssertDecl(condition, errmessage)
Definition c.h:992
uint32 TransactionId
Definition c.h:720
size_t Size
Definition c.h:673
#define CLOG_MAX_ALLOWED_BUFFERS
Definition clog.c:73
#define CLOG_XACT_BITMASK
Definition clog.c:66
#define THRESHOLD_SUBTRANS_CLOG_OPT
Definition clog.c:104
static void TransactionIdSetStatusBit(TransactionId xid, XidStatus status, XLogRecPtr lsn, int slotno)
Definition clog.c:661
XidStatus TransactionIdGetStatus(TransactionId xid, XLogRecPtr *lsn)
Definition clog.c:735
void TransactionIdSetTreeStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
Definition clog.c:183
void CLOGShmemInit(void)
Definition clog.c:787
#define TransactionIdToBIndex(xid)
Definition clog.c:90
static int CLOGShmemBuffers(void)
Definition clog.c:768
void ExtendCLOG(TransactionId newestXact)
Definition clog.c:927
void clog_redo(XLogReaderState *record)
Definition clog.c:1073
static bool CLOGPagePrecedes(int64 page1, int64 page2)
Definition clog.c:1024
void TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
Definition clog.c:969
Size CLOGShmemSize(void)
Definition clog.c:781
bool check_transaction_buffers(int *newval, void **extra, GucSource source)
Definition clog.c:822
static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn, int64 pageno, bool all_xact_same_page)
Definition clog.c:293
int clogsyncfiletag(const FileTag *ftag, char *path)
Definition clog.c:1105
void BootStrapCLOG(void)
Definition clog.c:834
#define CLOG_BITS_PER_XACT
Definition clog.c:63
#define CLOG_LSNS_PER_PAGE
Definition clog.c:94
static int64 TransactionIdToPage(TransactionId xid)
Definition clog.c:83
#define TransactionIdToByte(xid)
Definition clog.c:89
#define TransactionIdToPgIndex(xid)
Definition clog.c:88
void StartupCLOG(void)
Definition clog.c:845
static int clog_errdetail_for_io_error(const void *opaque_data)
Definition clog.c:1039
static void set_status_by_pages(int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
Definition clog.c:257
static bool TransactionGroupUpdateXidStatus(TransactionId xid, XidStatus status, XLogRecPtr lsn, int64 pageno)
Definition clog.c:441
static void TransactionIdSetPageStatusInternal(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn, int64 pageno)
Definition clog.c:364
#define GetLSNIndex(slotno, xid)
Definition clog.c:96
void CheckPointCLOG(void)
Definition clog.c:905
static SlruCtlData XactCtlData
Definition clog.c:109
#define XactCtl
Definition clog.c:111
void TrimCLOG(void)
Definition clog.c:860
static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact, Oid oldestXactDb)
Definition clog.c:1054
#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
int errdetail(const char *fmt,...) pg_attribute_printf(1
#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:1177
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1956
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1348
@ 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:504
#define PGPROC_MAX_CACHED_SUBXIDS
Definition proc.h:40
#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:254
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition slru.c:1355
int SimpleLruAutotuneBuffers(int divisor, int max)
Definition slru.c:233
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, const void *opaque_data)
Definition slru.c:533
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition slru.c:1824
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition slru.c:1864
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition slru.c:380
void SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
Definition slru.c:449
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition slru.c:1441
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, const void *opaque_data)
Definition slru.c:637
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition slru.c:200
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition slru.c:1745
bool check_slru_buffers(const char *name, int *newval)
Definition slru.c:360
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition slru.h:171
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition slru.h:196
PGPROC * MyProc
Definition proc.c:68
PROC_HDR * ProcGlobal
Definition proc.c:71
Definition sync.h:51
Definition proc.h:176
XLogRecPtr clogGroupMemberLsn
Definition proc.h:363
TransactionId clogGroupMemberXid
Definition proc.h:358
int64 clogGroupMemberPage
Definition proc.h:361
bool clogGroupMember
Definition proc.h:356
pg_atomic_uint32 clogGroupNext
Definition proc.h:357
XidStatus clogGroupMemberXidStatus
Definition proc.h:359
XidCacheStatus subxidStatus
Definition proc.h:239
TransactionId xid
Definition proc.h:229
struct XidCache subxids
Definition proc.h:241
PGSemaphore sem
Definition proc.h:250
FullTransactionId nextXid
Definition transam.h:220
uint8 count
Definition proc.h:45
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition proc.h:52
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:2767
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
uint64 XLogRecPtr
Definition xlogdefs.h:21
XLogRecPtr XLogSimpleInsertInt64(RmgrId rmid, uint8 info, int64 value)
Definition xloginsert.c:544
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition xloginsert.c:479
void XLogRegisterData(const void *data, uint32 len)
Definition xloginsert.c:369
void XLogBeginInsert(void)
Definition xloginsert.c:153
#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 66 of file clog.c.

◆ CLOG_XACTS_PER_BYTE

#define CLOG_XACTS_PER_BYTE   4

Definition at line 64 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 93 of file clog.c.

◆ CLOG_XACTS_PER_PAGE

#define CLOG_XACTS_PER_PAGE   (BLCKSZ * CLOG_XACTS_PER_BYTE)

Definition at line 65 of file clog.c.

◆ GetLSNIndex

#define GetLSNIndex (   slotno,
  xid 
)
Value:

Definition at line 96 of file clog.c.

◆ THRESHOLD_SUBTRANS_CLOG_OPT

#define THRESHOLD_SUBTRANS_CLOG_OPT   5

Definition at line 104 of file clog.c.

◆ TransactionIdToBIndex

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

Definition at line 90 of file clog.c.

◆ TransactionIdToByte

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

Definition at line 89 of file clog.c.

◆ TransactionIdToPgIndex

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

Definition at line 88 of file clog.c.

◆ XactCtl

#define XactCtl   (&XactCtlData)

Definition at line 111 of file clog.c.

Function Documentation

◆ BootStrapCLOG()

void BootStrapCLOG ( void  )

Definition at line 834 of file clog.c.

835{
836 /* Zero the initial page and flush it to disk */
838}

References SimpleLruZeroAndWritePage(), and XactCtl.

Referenced by BootStrapXLOG().

◆ check_transaction_buffers()

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

Definition at line 822 of file clog.c.

823{
824 return check_slru_buffers("transaction_buffers", newval);
825}

References check_slru_buffers(), and newval.

◆ CheckPointCLOG()

void CheckPointCLOG ( void  )

Definition at line 905 of file clog.c.

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

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

Referenced by CheckPointGuts().

◆ clog_errdetail_for_io_error()

static int clog_errdetail_for_io_error ( const void opaque_data)
static

Definition at line 1039 of file clog.c.

1040{
1041 TransactionId xid = *(const TransactionId *) opaque_data;
1042
1043 return errdetail("Could not access commit status of transaction %u.", xid);
1044}

References errdetail(), and fb().

Referenced by CLOGShmemInit().

◆ clog_redo()

void clog_redo ( XLogReaderState record)

Definition at line 1073 of file clog.c.

1074{
1075 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1076
1077 /* Backup blocks are not used in clog records */
1079
1080 if (info == CLOG_ZEROPAGE)
1081 {
1082 int64 pageno;
1083
1084 memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1086 }
1087 else if (info == CLOG_TRUNCATE)
1088 {
1090
1091 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_clog_truncate));
1092
1093 AdvanceOldestClogXid(xlrec.oldestXact);
1094
1096 }
1097 else
1098 elog(PANIC, "clog_redo: unknown op code %u", info);
1099}

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 768 of file clog.c.

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

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

Referenced by CLOGShmemInit(), and CLOGShmemSize().

◆ CLOGShmemInit()

void CLOGShmemInit ( void  )

Definition at line 787 of file clog.c.

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

References Assert, buf, clog_errdetail_for_io_error(), 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 781 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 1105 of file clog.c.

1106{
1107 return SlruSyncFileTag(XactCtl, ftag, path);
1108}

References SlruSyncFileTag(), and XactCtl.

◆ ExtendCLOG()

void ExtendCLOG ( TransactionId  newestXact)

Definition at line 927 of file clog.c.

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

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 257 of file clog.c.

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

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

Referenced by TransactionIdSetTreeStatus().

◆ StartupCLOG()

void StartupCLOG ( void  )

Definition at line 845 of file clog.c.

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

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 441 of file clog.c.

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

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 735 of file clog.c.

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

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 293 of file clog.c.

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

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 364 of file clog.c.

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

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 661 of file clog.c.

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

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 183 of file clog.c.

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

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 860 of file clog.c.

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

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 969 of file clog.c.

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

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 1054 of file clog.c.

1055{
1058
1059 xlrec.pageno = pageno;
1060 xlrec.oldestXact = oldestXact;
1061 xlrec.oldestXactDb = oldestXactDb;
1062
1067}

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 109 of file clog.c.