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/subsystems.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   (&XactSlruDesc)
 

Functions

static int64 TransactionIdToPage (TransactionId xid)
 
static void CLOGShmemRequest (void *arg)
 
static void CLOGShmemInit (void *arg)
 
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)
 
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

const ShmemCallbacks CLOGShmemCallbacks
 
static SlruDesc XactSlruDesc
 

Macro Definition Documentation

◆ CLOG_BITS_PER_XACT

#define CLOG_BITS_PER_XACT   2

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

◆ CLOG_MAX_ALLOWED_BUFFERS

#define CLOG_MAX_ALLOWED_BUFFERS
Value:
#define Min(x, y)
Definition c.h:1131
#define CLOG_XACTS_PER_PAGE
Definition clog.c:66
#define SLRU_MAX_ALLOWED_BUFFERS
Definition slru.h:26
#define MaxTransactionId
Definition transam.h:35

Definition at line 74 of file clog.c.

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

◆ CLOG_XACT_BITMASK

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

Definition at line 67 of file clog.c.

◆ CLOG_XACTS_PER_BYTE

#define CLOG_XACTS_PER_BYTE   4

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

◆ CLOG_XACTS_PER_PAGE

#define CLOG_XACTS_PER_PAGE   (BLCKSZ * CLOG_XACTS_PER_BYTE)

Definition at line 66 of file clog.c.

◆ GetLSNIndex

#define GetLSNIndex (   slotno,
  xid 
)
Value:

Definition at line 97 of file clog.c.

◆ THRESHOLD_SUBTRANS_CLOG_OPT

#define THRESHOLD_SUBTRANS_CLOG_OPT   5

Definition at line 105 of file clog.c.

◆ TransactionIdToBIndex

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

Definition at line 91 of file clog.c.

◆ TransactionIdToByte

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

Definition at line 90 of file clog.c.

◆ TransactionIdToPgIndex

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

Definition at line 89 of file clog.c.

◆ XactCtl

#define XactCtl   (&XactSlruDesc)

Definition at line 122 of file clog.c.

Function Documentation

◆ BootStrapCLOG()

void BootStrapCLOG ( void  )

Definition at line 850 of file clog.c.

851{
852 /* Zero the initial page and flush it to disk */
854}

References SimpleLruZeroAndWritePage(), and XactCtl.

Referenced by BootStrapXLOG().

◆ check_transaction_buffers()

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

Definition at line 838 of file clog.c.

839{
840 return check_slru_buffers("transaction_buffers", newval);
841}

References check_slru_buffers(), and newval.

◆ CheckPointCLOG()

void CheckPointCLOG ( void  )

Definition at line 921 of file clog.c.

922{
923 /*
924 * Write dirty CLOG pages to disk. This may result in sync requests
925 * queued for later handling by ProcessSyncRequests(), as part of the
926 * checkpoint.
927 */
931}

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

1056{
1057 TransactionId xid = *(const TransactionId *) opaque_data;
1058
1059 return errdetail("Could not access commit status of transaction %u.", xid);
1060}

References errdetail(), and fb().

Referenced by CLOGShmemRequest().

◆ clog_redo()

void clog_redo ( XLogReaderState record)

Definition at line 1089 of file clog.c.

1090{
1091 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1092
1093 /* Backup blocks are not used in clog records */
1095
1096 if (info == CLOG_ZEROPAGE)
1097 {
1098 int64 pageno;
1099
1100 memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1102 }
1103 else if (info == CLOG_TRUNCATE)
1104 {
1106
1107 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_clog_truncate));
1108
1109 AdvanceOldestClogXid(xlrec.oldestXact);
1110
1112 }
1113 else
1114 elog(PANIC, "clog_redo: unknown op code %u", info);
1115}

References AdvanceOldestClogXid(), Assert, CLOG_TRUNCATE, CLOG_ZEROPAGE, elog, fb(), memcpy(), 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 776 of file clog.c.

777{
778 /* auto-tune based on shared buffers */
779 if (transaction_buffers == 0)
780 return SimpleLruAutotuneBuffers(512, 1024);
781
783}

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

Referenced by CLOGShmemRequest().

◆ CLOGShmemInit()

static void CLOGShmemInit ( void arg)
static

◆ CLOGShmemRequest()

static void CLOGShmemRequest ( void arg)
static

Definition at line 789 of file clog.c.

790{
791 /* If auto-tuning is requested, now is the time to do it */
792 if (transaction_buffers == 0)
793 {
794 char buf[32];
795
796 snprintf(buf, sizeof(buf), "%d", CLOGShmemBuffers());
797 SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
799
800 /*
801 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
802 * However, if the DBA explicitly set transaction_buffers = 0 in the
803 * config file, then PGC_S_DYNAMIC_DEFAULT will fail to override that
804 * and we must force the matter with PGC_S_OVERRIDE.
805 */
806 if (transaction_buffers == 0) /* failed to apply it? */
807 SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
809 }
812 .name = "transaction",
813 .Dir = "pg_xact",
814 .long_segment_names = false,
815
816 .nslots = CLOGShmemBuffers(),
817 .nlsns = CLOG_LSNS_PER_PAGE,
818
819 .sync_handler = SYNC_HANDLER_CLOG,
820 .PagePrecedes = CLOGPagePrecedes,
821 .errdetail_for_io_error = clog_errdetail_for_io_error,
822
823 .buffer_tranche_id = LWTRANCHE_XACT_BUFFER,
824 .bank_tranche_id = LWTRANCHE_XACT_SLRU,
825 );
826}

References Assert, buf, clog_errdetail_for_io_error(), CLOG_LSNS_PER_PAGE, CLOGPagePrecedes(), CLOGShmemBuffers(), fb(), name, PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT, PGC_S_OVERRIDE, SetConfigOption(), SimpleLruRequest, snprintf, SYNC_HANDLER_CLOG, transaction_buffers, and XactSlruDesc.

◆ clogsyncfiletag()

int clogsyncfiletag ( const FileTag ftag,
char path 
)

Definition at line 1121 of file clog.c.

1122{
1123 return SlruSyncFileTag(XactCtl, ftag, path);
1124}

References SlruSyncFileTag(), and XactCtl.

◆ ExtendCLOG()

void ExtendCLOG ( TransactionId  newestXact)

Definition at line 943 of file clog.c.

944{
945 int64 pageno;
946 LWLock *lock;
947
948 /*
949 * No work except at first XID of a page. But beware: just after
950 * wraparound, the first XID of page zero is FirstNormalTransactionId.
951 */
954 return;
955
957 lock = SimpleLruGetBankLock(XactCtl, pageno);
958
960
961 /* Zero the page and make a WAL entry about it */
962 SimpleLruZeroPage(XactCtl, pageno);
964
965 LWLockRelease(lock);
966}

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

268{
269 int64 pageno = TransactionIdToPage(subxids[0]);
270 int offset = 0;
271 int i = 0;
272
273 Assert(nsubxids > 0); /* else the pageno fetch above is unsafe */
274
275 while (i < nsubxids)
276 {
277 int num_on_page = 0;
279
280 do
281 {
282 nextpageno = TransactionIdToPage(subxids[i]);
283 if (nextpageno != pageno)
284 break;
285 num_on_page++;
286 i++;
287 } while (i < nsubxids);
288
290 num_on_page, subxids + offset,
291 status, lsn, pageno, false);
292 offset = i;
293 pageno = nextpageno;
294 }
295}

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

Referenced by TransactionIdSetTreeStatus().

◆ StartupCLOG()

void StartupCLOG ( void  )

Definition at line 861 of file clog.c.

862{
864 int64 pageno = TransactionIdToPage(xid);
865
866 /*
867 * Initialize our idea of the latest page number.
868 */
869 pg_atomic_write_u64(&XactCtl->shared->latest_page_number, pageno);
870}

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

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

References Assert, PROC_HDR::clogGroupFirst, 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 743 of file clog.c.

744{
745 int64 pageno = TransactionIdToPage(xid);
746 int byteno = TransactionIdToByte(xid);
748 int slotno;
749 int lsnindex;
750 char *byteptr;
751 XidStatus status;
752
753 /* lock is acquired by SimpleLruReadPage_ReadOnly */
754
756 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
757
758 status = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
759
761 *lsn = XactCtl->shared->group_lsn[lsnindex];
762
764
765 return status;
766}

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

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

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

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

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

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

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

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

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

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

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

986{
988
989 /*
990 * The cutoff point is the start of the segment containing oldestXact. We
991 * pass the *page* containing oldestXact to SimpleLruTruncate.
992 */
993 cutoffPage = TransactionIdToPage(oldestXact);
994
995 /* Check to see if there's any files that could be removed */
997 return; /* nothing to remove */
998
999 /*
1000 * Advance oldestClogXid before truncating clog, so concurrent xact status
1001 * lookups can ensure they don't attempt to access truncated-away clog.
1002 *
1003 * It's only necessary to do this if we will actually truncate away clog
1004 * pages.
1005 */
1006 AdvanceOldestClogXid(oldestXact);
1007
1008 /*
1009 * Write XLOG record and flush XLOG to disk. We record the oldest xid
1010 * we're keeping information about here so we can ensure that it's always
1011 * ahead of clog truncation in case we crash, and so a standby finds out
1012 * the new valid xid before the next checkpoint.
1013 */
1015
1016 /* Now we can remove the old CLOG segment(s) */
1018}

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

1071{
1074
1075 xlrec.pageno = pageno;
1076 xlrec.oldestXact = oldestXact;
1077 xlrec.oldestXactDb = oldestXactDb;
1078
1083}

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

Referenced by TruncateCLOG().

Variable Documentation

◆ CLOGShmemCallbacks

const ShmemCallbacks CLOGShmemCallbacks
Initial value:
= {
.request_fn = CLOGShmemRequest,
.init_fn = CLOGShmemInit,
}

Definition at line 115 of file clog.c.

115 {
116 .request_fn = CLOGShmemRequest,
117 .init_fn = CLOGShmemInit,
118};

◆ XactSlruDesc

SlruDesc XactSlruDesc
static

Definition at line 120 of file clog.c.

Referenced by CLOGShmemRequest().