Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * clog.c
4 : : * PostgreSQL transaction-commit-log manager
5 : : *
6 : : * This module stores two bits per transaction regarding its commit/abort
7 : : * status; the status for four transactions fit in a byte.
8 : : *
9 : : * This would be a pretty simple abstraction on top of slru.c, except that
10 : : * for performance reasons we allow multiple transactions that are
11 : : * committing concurrently to form a queue, so that a single process can
12 : : * update the status for all of them within a single lock acquisition run.
13 : : *
14 : : * XLOG interactions: this module generates an XLOG record whenever a new
15 : : * CLOG page is initialized to zeroes. Other writes of CLOG come from
16 : : * recording of transaction commit or abort in xact.c, which generates its
17 : : * own XLOG records for these events and will re-perform the status update
18 : : * on redo; so we need make no additional XLOG entry here. For synchronous
19 : : * transaction commits, the XLOG is guaranteed flushed through the XLOG commit
20 : : * record before we are called to log a commit, so the WAL rule "write xlog
21 : : * before data" is satisfied automatically. However, for async commits we
22 : : * must track the latest LSN affecting each CLOG page, so that we can flush
23 : : * XLOG that far and satisfy the WAL rule. We don't have to worry about this
24 : : * for aborts (whether sync or async), since the post-crash assumption would
25 : : * be that such transactions failed anyway.
26 : : *
27 : : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
28 : : * Portions Copyright (c) 1994, Regents of the University of California
29 : : *
30 : : * src/backend/access/transam/clog.c
31 : : *
32 : : *-------------------------------------------------------------------------
33 : : */
34 : : #include "postgres.h"
35 : :
36 : : #include "access/clog.h"
37 : : #include "access/slru.h"
38 : : #include "access/transam.h"
39 : : #include "access/xlog.h"
40 : : #include "access/xloginsert.h"
41 : : #include "access/xlogutils.h"
42 : : #include "miscadmin.h"
43 : : #include "pg_trace.h"
44 : : #include "pgstat.h"
45 : : #include "storage/proc.h"
46 : : #include "storage/sync.h"
47 : : #include "utils/guc_hooks.h"
48 : :
49 : : /*
50 : : * Defines for CLOG page sizes. A page is the same BLCKSZ as is used
51 : : * everywhere else in Postgres.
52 : : *
53 : : * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
54 : : * CLOG page numbering also wraps around at 0xFFFFFFFF/CLOG_XACTS_PER_PAGE,
55 : : * and CLOG segment numbering at
56 : : * 0xFFFFFFFF/CLOG_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need take no
57 : : * explicit notice of that fact in this module, except when comparing segment
58 : : * and page numbers in TruncateCLOG (see CLOGPagePrecedes).
59 : : */
60 : :
61 : : /* We need two bits per xact, so four xacts fit in a byte */
62 : : #define CLOG_BITS_PER_XACT 2
63 : : #define CLOG_XACTS_PER_BYTE 4
64 : : #define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE)
65 : : #define CLOG_XACT_BITMASK ((1 << CLOG_BITS_PER_XACT) - 1)
66 : :
67 : : /*
68 : : * Because space used in CLOG by each transaction is so small, we place a
69 : : * smaller limit on the number of CLOG buffers than SLRU allows. No other
70 : : * SLRU needs this.
71 : : */
72 : : #define CLOG_MAX_ALLOWED_BUFFERS \
73 : : Min(SLRU_MAX_ALLOWED_BUFFERS, \
74 : : (((MaxTransactionId / 2) + (CLOG_XACTS_PER_PAGE - 1)) / CLOG_XACTS_PER_PAGE))
75 : :
76 : :
77 : : /*
78 : : * Although we return an int64 the actual value can't currently exceed
79 : : * 0xFFFFFFFF/CLOG_XACTS_PER_PAGE.
80 : : */
81 : : static inline int64
137 akorotkov@postgresql 82 :GNC 1377631 : TransactionIdToPage(TransactionId xid)
83 : : {
84 : 1377631 : 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 : : */
108 : : static SlruCtlData XactCtlData;
109 : :
110 : : #define XactCtl (&XactCtlData)
111 : :
112 : :
113 : : static int ZeroCLOGPage(int64 pageno, bool writeXlog);
114 : : static bool CLOGPagePrecedes(int64 page1, int64 page2);
115 : : static void WriteZeroPageXlogRec(int64 pageno);
116 : : static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact,
117 : : Oid oldestXactDb);
118 : : static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
119 : : TransactionId *subxids, XidStatus status,
120 : : XLogRecPtr lsn, int64 pageno,
121 : : bool all_xact_same_page);
122 : : static void TransactionIdSetStatusBit(TransactionId xid, XidStatus status,
123 : : XLogRecPtr lsn, int slotno);
124 : : static void set_status_by_pages(int nsubxids, TransactionId *subxids,
125 : : XidStatus status, XLogRecPtr lsn);
126 : : static bool TransactionGroupUpdateXidStatus(TransactionId xid,
127 : : XidStatus status, XLogRecPtr lsn, int64 pageno);
128 : : static void TransactionIdSetPageStatusInternal(TransactionId xid, int nsubxids,
129 : : TransactionId *subxids, XidStatus status,
130 : : XLogRecPtr lsn, int64 pageno);
131 : :
132 : :
133 : : /*
134 : : * TransactionIdSetTreeStatus
135 : : *
136 : : * Record the final state of transaction entries in the commit log for
137 : : * a transaction and its subtransaction tree. Take care to ensure this is
138 : : * efficient, and as atomic as possible.
139 : : *
140 : : * xid is a single xid to set status for. This will typically be
141 : : * the top level transactionid for a top level commit or abort. It can
142 : : * also be a subtransaction when we record transaction aborts.
143 : : *
144 : : * subxids is an array of xids of length nsubxids, representing subtransactions
145 : : * in the tree of xid. In various cases nsubxids may be zero.
146 : : *
147 : : * lsn must be the WAL location of the commit record when recording an async
148 : : * commit. For a synchronous commit it can be InvalidXLogRecPtr, since the
149 : : * caller guarantees the commit record is already flushed in that case. It
150 : : * should be InvalidXLogRecPtr for abort cases, too.
151 : : *
152 : : * In the commit case, atomicity is limited by whether all the subxids are in
153 : : * the same CLOG page as xid. If they all are, then the lock will be grabbed
154 : : * only once, and the status will be set to committed directly. Otherwise
155 : : * we must
156 : : * 1. set sub-committed all subxids that are not on the same page as the
157 : : * main xid
158 : : * 2. atomically set committed the main xid and the subxids on the same page
159 : : * 3. go over the first bunch again and set them committed
160 : : * Note that as far as concurrent checkers are concerned, main transaction
161 : : * commit as a whole is still atomic.
162 : : *
163 : : * Example:
164 : : * TransactionId t commits and has subxids t1, t2, t3, t4
165 : : * t is on page p1, t1 is also on p1, t2 and t3 are on p2, t4 is on p3
166 : : * 1. update pages2-3:
167 : : * page2: set t2,t3 as sub-committed
168 : : * page3: set t4 as sub-committed
169 : : * 2. update page1:
170 : : * page1: set t,t1 as committed
171 : : * 3. update pages2-3:
172 : : * page2: set t2,t3 as committed
173 : : * page3: set t4 as committed
174 : : *
175 : : * NB: this is a low-level routine and is NOT the preferred entry point
176 : : * for most uses; functions in transam.c are the intended callers.
177 : : *
178 : : * XXX Think about issuing POSIX_FADV_WILLNEED on pages that we will need,
179 : : * but aren't yet in cache, as well as hinting pages not to fall out of
180 : : * cache yet.
181 : : */
182 : : void
5655 alvherre@alvh.no-ip. 183 :CBC 136733 : TransactionIdSetTreeStatus(TransactionId xid, int nsubxids,
184 : : TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
185 : : {
137 akorotkov@postgresql 186 :GNC 136733 : int64 pageno = TransactionIdToPage(xid); /* get page of parent */
187 : : int i;
188 : :
5655 alvherre@alvh.no-ip. 189 [ + + - + ]:CBC 136733 : Assert(status == TRANSACTION_STATUS_COMMITTED ||
190 : : status == TRANSACTION_STATUS_ABORTED);
191 : :
192 : : /*
193 : : * See how many subxids, if any, are on the same page as the parent, if
194 : : * any.
195 : : */
196 [ + + ]: 142995 : for (i = 0; i < nsubxids; i++)
197 : : {
198 [ - + ]: 6262 : if (TransactionIdToPage(subxids[i]) != pageno)
5655 alvherre@alvh.no-ip. 199 :UBC 0 : break;
200 : : }
201 : :
202 : : /*
203 : : * Do all items fit on a single page?
204 : : */
5655 alvherre@alvh.no-ip. 205 [ + - ]:CBC 136733 : if (i == nsubxids)
206 : : {
207 : : /*
208 : : * Set the parent and all subtransactions in a single call
209 : : */
210 : 136733 : TransactionIdSetPageStatus(xid, nsubxids, subxids, status, lsn,
211 : : pageno, true);
212 : : }
213 : : else
214 : : {
5421 bruce@momjian.us 215 :UBC 0 : int nsubxids_on_first_page = i;
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 : : */
5655 alvherre@alvh.no-ip. 227 [ # # ]: 0 : if (status == TRANSACTION_STATUS_COMMITTED)
228 : 0 : set_status_by_pages(nsubxids - nsubxids_on_first_page,
229 : 0 : subxids + nsubxids_on_first_page,
230 : : TRANSACTION_STATUS_SUB_COMMITTED, lsn);
231 : :
232 : : /*
233 : : * Now set the parent and subtransactions on same page as the parent,
234 : : * if any
235 : : */
236 : 0 : pageno = TransactionIdToPage(xid);
237 : 0 : TransactionIdSetPageStatus(xid, nsubxids_on_first_page, subxids, status,
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 : : */
244 : 0 : set_status_by_pages(nsubxids - nsubxids_on_first_page,
245 : 0 : subxids + nsubxids_on_first_page,
246 : : status, lsn);
247 : : }
5655 alvherre@alvh.no-ip. 248 :CBC 136733 : }
249 : :
250 : : /*
251 : : * Helper for TransactionIdSetTreeStatus: set the status for a bunch of
252 : : * transactions, chunking in the separate CLOG pages involved. We never
253 : : * pass the whole transaction tree to this function, only subtransactions
254 : : * that are on different pages to the top level transaction id.
255 : : */
256 : : static void
5655 alvherre@alvh.no-ip. 257 :UBC 0 : set_status_by_pages(int nsubxids, TransactionId *subxids,
258 : : XidStatus status, XLogRecPtr lsn)
259 : : {
137 akorotkov@postgresql 260 :UNC 0 : int64 pageno = TransactionIdToPage(subxids[0]);
5421 bruce@momjian.us 261 :UBC 0 : int offset = 0;
262 : 0 : int i = 0;
263 : :
2382 tgl@sss.pgh.pa.us 264 [ # # ]: 0 : Assert(nsubxids > 0); /* else the pageno fetch above is unsafe */
265 : :
5655 alvherre@alvh.no-ip. 266 [ # # ]: 0 : while (i < nsubxids)
267 : : {
5421 bruce@momjian.us 268 : 0 : int num_on_page = 0;
269 : : int64 nextpageno;
270 : :
271 : : do
272 : : {
2382 tgl@sss.pgh.pa.us 273 : 0 : nextpageno = TransactionIdToPage(subxids[i]);
274 [ # # ]: 0 : if (nextpageno != pageno)
275 : 0 : break;
5655 alvherre@alvh.no-ip. 276 : 0 : num_on_page++;
277 : 0 : i++;
2382 tgl@sss.pgh.pa.us 278 [ # # ]: 0 : } while (i < nsubxids);
279 : :
5655 alvherre@alvh.no-ip. 280 : 0 : TransactionIdSetPageStatus(InvalidTransactionId,
281 : 0 : num_on_page, subxids + offset,
282 : : status, lsn, pageno, false);
283 : 0 : offset = i;
2382 tgl@sss.pgh.pa.us 284 : 0 : pageno = nextpageno;
285 : : }
5655 alvherre@alvh.no-ip. 286 : 0 : }
287 : :
288 : : /*
289 : : * Record the final state of transaction entries in the commit log for all
290 : : * entries on a single page. Atomic only on this page.
291 : : */
292 : : static void
5655 alvherre@alvh.no-ip. 293 :CBC 136733 : TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
294 : : TransactionId *subxids, XidStatus status,
295 : : XLogRecPtr lsn, int64 pageno,
296 : : bool all_xact_same_page)
297 : : {
298 : : LWLock *lock;
299 : :
300 : : /* Can't use group update when PGPROC overflows. */
301 : : StaticAssertDecl(THRESHOLD_SUBTRANS_CLOG_OPT <= PGPROC_MAX_CACHED_SUBXIDS,
302 : : "group clog threshold less than PGPROC cached subxids");
303 : :
304 : : /* Get the SLRU bank lock for the page we are going to access. */
46 alvherre@alvh.no-ip. 305 :GNC 136733 : 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 : : */
1339 andres@anarazel.de 321 [ + - + + :CBC 136733 : if (all_xact_same_page && xid == MyProc->xid &&
+ + ]
2417 rhaas@postgresql.org 322 : 113211 : nsubxids <= THRESHOLD_SUBTRANS_CLOG_OPT &&
1339 andres@anarazel.de 323 [ + + + + ]: 113211 : nsubxids == MyProc->subxidStatus.count &&
773 tgl@sss.pgh.pa.us 324 : 446 : (nsubxids == 0 ||
325 [ + - ]: 446 : 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 : : */
46 alvherre@alvh.no-ip. 334 [ + + ]:GNC 113096 : if (LWLockConditionalAcquire(lock, LW_EXCLUSIVE))
335 : : {
336 : : /* Got the lock without waiting! Do the update. */
2417 rhaas@postgresql.org 337 :CBC 112961 : TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
338 : : lsn, pageno);
46 alvherre@alvh.no-ip. 339 :GNC 112961 : LWLockRelease(lock);
2417 rhaas@postgresql.org 340 :CBC 112961 : return;
341 : : }
342 [ + - ]: 135 : else if (TransactionGroupUpdateXidStatus(xid, status, lsn, pageno))
343 : : {
344 : : /* Group update mechanism has done the work. */
345 : 135 : 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. */
46 alvherre@alvh.no-ip. 352 :GNC 23637 : LWLockAcquire(lock, LW_EXCLUSIVE);
2417 rhaas@postgresql.org 353 :CBC 23637 : TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
354 : : lsn, pageno);
46 alvherre@alvh.no-ip. 355 :GNC 23637 : LWLockRelease(lock);
356 : : }
357 : :
358 : : /*
359 : : * Record the final state of transaction entry in the commit log
360 : : *
361 : : * We don't do any locking here; caller must handle that.
362 : : */
363 : : static void
2417 rhaas@postgresql.org 364 :CBC 136733 : TransactionIdSetPageStatusInternal(TransactionId xid, int nsubxids,
365 : : TransactionId *subxids, XidStatus status,
366 : : XLogRecPtr lsn, int64 pageno)
367 : : {
368 : : int slotno;
369 : : int i;
370 : :
8268 tgl@sss.pgh.pa.us 371 [ + + - + : 136733 : Assert(status == TRANSACTION_STATUS_COMMITTED ||
- - - - ]
372 : : status == TRANSACTION_STATUS_ABORTED ||
373 : : (status == TRANSACTION_STATUS_SUB_COMMITTED && !TransactionIdIsValid(xid)));
46 alvherre@alvh.no-ip. 374 [ - + ]:GNC 136733 : Assert(LWLockHeldByMeInMode(SimpleLruGetBankLock(XactCtl, pageno),
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 : : */
1430 tgl@sss.pgh.pa.us 386 :CBC 136733 : slotno = SimpleLruReadPage(XactCtl, pageno, XLogRecPtrIsInvalid(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 : : */
5655 alvherre@alvh.no-ip. 397 [ + - ]: 136733 : if (TransactionIdIsValid(xid))
398 : : {
399 : : /* Subtransactions first, if needed ... */
400 [ + + ]: 136733 : if (status == TRANSACTION_STATUS_COMMITTED)
401 : : {
402 [ + + ]: 135442 : for (i = 0; i < nsubxids; i++)
403 : : {
1430 tgl@sss.pgh.pa.us 404 [ - + ]: 5742 : Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
5655 alvherre@alvh.no-ip. 405 : 5742 : TransactionIdSetStatusBit(subxids[i],
406 : : TRANSACTION_STATUS_SUB_COMMITTED,
407 : : lsn, slotno);
408 : : }
409 : : }
410 : :
411 : : /* ... then the main transaction */
412 : 136733 : TransactionIdSetStatusBit(xid, status, lsn, slotno);
413 : : }
414 : :
415 : : /* Set the subtransactions */
416 [ + + ]: 142995 : for (i = 0; i < nsubxids; i++)
417 : : {
1430 tgl@sss.pgh.pa.us 418 [ - + ]: 6262 : Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
5655 alvherre@alvh.no-ip. 419 : 6262 : TransactionIdSetStatusBit(subxids[i], status, lsn, slotno);
420 : : }
421 : :
1430 tgl@sss.pgh.pa.us 422 : 136733 : XactCtl->shared->page_dirty[slotno] = true;
2417 rhaas@postgresql.org 423 : 136733 : }
424 : :
425 : : /*
426 : : * Subroutine for TransactionIdSetPageStatus, q.v.
427 : : *
428 : : * When we cannot immediately acquire the SLRU bank lock in exclusive mode at
429 : : * commit time, add ourselves to a list of processes that need their XIDs
430 : : * status update. The first process to add itself to the list will acquire
431 : : * the lock in exclusive mode and set transaction status as required on behalf
432 : : * of all group members. This avoids a great deal of contention when many
433 : : * processes are trying to commit at once, since the lock need not be
434 : : * repeatedly handed off from one committing process to the next.
435 : : *
436 : : * Returns true when transaction status has been updated in clog; returns
437 : : * false if we decided against applying the optimization because the page
438 : : * number we need to update differs from those processes already waiting.
439 : : */
440 : : static bool
441 : 135 : TransactionGroupUpdateXidStatus(TransactionId xid, XidStatus status,
442 : : XLogRecPtr lsn, int64 pageno)
443 : : {
444 : 135 : volatile PROC_HDR *procglobal = ProcGlobal;
445 : 135 : PGPROC *proc = MyProc;
446 : : uint32 nextidx;
447 : : uint32 wakeidx;
448 : : int prevpageno;
46 alvherre@alvh.no-ip. 449 :GNC 135 : LWLock *prevlock = NULL;
450 : :
451 : : /* We should definitely have an XID whose status needs to be updated. */
2417 rhaas@postgresql.org 452 [ - + ]:CBC 135 : Assert(TransactionIdIsValid(xid));
453 : :
454 : : /*
455 : : * Prepare to add ourselves to the list of processes needing a group XID
456 : : * status update.
457 : : */
458 : 135 : proc->clogGroupMember = true;
459 : 135 : proc->clogGroupMemberXid = xid;
460 : 135 : proc->clogGroupMemberXidStatus = status;
461 : 135 : proc->clogGroupMemberPage = pageno;
462 : 135 : 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 : 135 : 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 : : */
42 heikki.linnakangas@i 504 [ + + ]:GNC 139 : if (nextidx != INVALID_PROC_NUMBER &&
52 505 [ - + ]: 21 : 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 : : */
2417 rhaas@postgresql.org 511 :UBC 0 : proc->clogGroupMember = false;
42 heikki.linnakangas@i 512 :UNC 0 : pg_atomic_write_u32(&proc->clogGroupNext, INVALID_PROC_NUMBER);
2417 rhaas@postgresql.org 513 :UBC 0 : return false;
514 : : }
515 : :
2417 rhaas@postgresql.org 516 :CBC 139 : pg_atomic_write_u32(&proc->clogGroupNext, nextidx);
517 : :
518 [ + + ]: 139 : if (pg_atomic_compare_exchange_u32(&procglobal->clogGroupFirst,
519 : : &nextidx,
520 : : (uint32) MyProcNumber))
521 : 135 : 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 : : */
42 heikki.linnakangas@i 530 [ + + ]:GNC 135 : if (nextidx != INVALID_PROC_NUMBER)
531 : : {
2417 rhaas@postgresql.org 532 :CBC 19 : int extraWaits = 0;
533 : :
534 : : /* Sleep until the leader updates our XID status. */
1429 tgl@sss.pgh.pa.us 535 : 19 : pgstat_report_wait_start(WAIT_EVENT_XACT_GROUP_UPDATE);
536 : : for (;;)
537 : : {
538 : : /* acts as a read barrier */
2417 rhaas@postgresql.org 539 : 19 : PGSemaphoreLock(proc->sem);
540 [ + - ]: 19 : if (!proc->clogGroupMember)
541 : 19 : break;
2417 rhaas@postgresql.org 542 :UBC 0 : extraWaits++;
543 : : }
2417 rhaas@postgresql.org 544 :CBC 19 : pgstat_report_wait_end();
545 : :
42 heikki.linnakangas@i 546 [ - + ]:GNC 19 : Assert(pg_atomic_read_u32(&proc->clogGroupNext) == INVALID_PROC_NUMBER);
547 : :
548 : : /* Fix semaphore count for any absorbed wakeups */
2417 rhaas@postgresql.org 549 [ - + ]:CBC 19 : while (extraWaits-- > 0)
2417 rhaas@postgresql.org 550 :UBC 0 : PGSemaphoreUnlock(proc->sem);
2417 rhaas@postgresql.org 551 :CBC 19 : 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 : : */
46 alvherre@alvh.no-ip. 558 :GNC 116 : prevpageno = proc->clogGroupMemberPage;
559 : 116 : prevlock = SimpleLruGetBankLock(XactCtl, prevpageno);
560 : 116 : LWLockAcquire(prevlock, LW_EXCLUSIVE);
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 : : */
2417 rhaas@postgresql.org 570 :CBC 116 : nextidx = pg_atomic_exchange_u32(&procglobal->clogGroupFirst,
571 : : INVALID_PROC_NUMBER);
572 : :
573 : : /* Remember head of list so we can perform wakeups after dropping lock. */
574 : 116 : wakeidx = nextidx;
575 : :
576 : : /* Walk the list and update the status of all XIDs. */
42 heikki.linnakangas@i 577 [ + + ]:GNC 251 : while (nextidx != INVALID_PROC_NUMBER)
578 : : {
557 drowley@postgresql.o 579 :CBC 135 : PGPROC *nextproc = &ProcGlobal->allProcs[nextidx];
46 alvherre@alvh.no-ip. 580 :GNC 135 : int 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 [ - + ]: 135 : if (thispageno != prevpageno)
594 : : {
46 alvherre@alvh.no-ip. 595 :UNC 0 : LWLock *lock = SimpleLruGetBankLock(XactCtl, thispageno);
596 : :
597 [ # # ]: 0 : if (prevlock != lock)
598 : : {
599 : 0 : LWLockRelease(prevlock);
600 : 0 : LWLockAcquire(lock, LW_EXCLUSIVE);
601 : : }
602 : 0 : prevlock = lock;
603 : 0 : prevpageno = thispageno;
604 : : }
605 : :
606 : : /*
607 : : * Transactions with more than THRESHOLD_SUBTRANS_CLOG_OPT sub-XIDs
608 : : * should not use group XID status update mechanism.
609 : : */
557 drowley@postgresql.o 610 [ - + ]:CBC 135 : Assert(nextproc->subxidStatus.count <= THRESHOLD_SUBTRANS_CLOG_OPT);
611 : :
612 : 135 : TransactionIdSetPageStatusInternal(nextproc->clogGroupMemberXid,
613 : 135 : nextproc->subxidStatus.count,
614 : 135 : nextproc->subxids.xids,
615 : : nextproc->clogGroupMemberXidStatus,
616 : : nextproc->clogGroupMemberLsn,
617 : : nextproc->clogGroupMemberPage);
618 : :
619 : : /* Move to next proc in list. */
620 : 135 : nextidx = pg_atomic_read_u32(&nextproc->clogGroupNext);
621 : : }
622 : :
623 : : /* We're done with the lock now. */
46 alvherre@alvh.no-ip. 624 [ + - ]:GNC 116 : if (prevlock != NULL)
625 : 116 : LWLockRelease(prevlock);
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 : : */
42 heikki.linnakangas@i 636 [ + + ]: 251 : while (wakeidx != INVALID_PROC_NUMBER)
637 : : {
557 drowley@postgresql.o 638 :CBC 135 : PGPROC *wakeproc = &ProcGlobal->allProcs[wakeidx];
639 : :
640 : 135 : wakeidx = pg_atomic_read_u32(&wakeproc->clogGroupNext);
42 heikki.linnakangas@i 641 :GNC 135 : pg_atomic_write_u32(&wakeproc->clogGroupNext, INVALID_PROC_NUMBER);
642 : :
643 : : /* ensure all previous writes are visible before follower continues. */
2417 rhaas@postgresql.org 644 :CBC 135 : pg_write_barrier();
645 : :
557 drowley@postgresql.o 646 : 135 : wakeproc->clogGroupMember = false;
647 : :
648 [ + + ]: 135 : if (wakeproc != MyProc)
649 : 19 : PGSemaphoreUnlock(wakeproc->sem);
650 : : }
651 : :
2417 rhaas@postgresql.org 652 : 116 : return true;
653 : : }
654 : :
655 : : /*
656 : : * Sets the commit status of a single transaction.
657 : : *
658 : : * Caller must hold the corresponding SLRU bank lock, will be held at exit.
659 : : */
660 : : static void
5655 alvherre@alvh.no-ip. 661 : 148737 : TransactionIdSetStatusBit(TransactionId xid, XidStatus status, XLogRecPtr lsn, int slotno)
662 : : {
663 : 148737 : int byteno = TransactionIdToByte(xid);
664 : 148737 : int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
665 : : char *byteptr;
666 : : char byteval;
667 : : char curval;
668 : :
46 alvherre@alvh.no-ip. 669 [ - + ]:GNC 148737 : Assert(XactCtl->shared->page_number[slotno] == TransactionIdToPage(xid));
670 [ - + ]: 148737 : Assert(LWLockHeldByMeInMode(SimpleLruGetBankLock(XactCtl,
671 : : XactCtl->shared->page_number[slotno]),
672 : : LW_EXCLUSIVE));
673 : :
1430 tgl@sss.pgh.pa.us 674 :CBC 148737 : byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
5641 alvherre@alvh.no-ip. 675 : 148737 : curval = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
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 : : */
683 [ + + + + : 148737 : if (InRecovery && status == TRANSACTION_STATUS_SUB_COMMITTED &&
- + ]
684 : : curval == TRANSACTION_STATUS_COMMITTED)
5641 alvherre@alvh.no-ip. 685 :UBC 0 : 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 : : */
5641 alvherre@alvh.no-ip. 691 [ + + + + :CBC 148737 : Assert(curval == 0 ||
- + - + ]
692 : : (curval == TRANSACTION_STATUS_SUB_COMMITTED &&
693 : : status != TRANSACTION_STATUS_IN_PROGRESS) ||
694 : : curval == status);
695 : :
696 : : /* note this assumes exclusive access to the clog page */
7225 tgl@sss.pgh.pa.us 697 : 148737 : byteval = *byteptr;
698 : 148737 : byteval &= ~(((1 << CLOG_BITS_PER_XACT) - 1) << bshift);
699 : 148737 : byteval |= (status << bshift);
700 : 148737 : *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 : : */
6101 710 [ + + ]: 148737 : if (!XLogRecPtrIsInvalid(lsn))
711 : : {
712 : 28711 : int lsnindex = GetLSNIndex(slotno, xid);
713 : :
1430 714 [ + + ]: 28711 : if (XactCtl->shared->group_lsn[lsnindex] < lsn)
715 : 24130 : XactCtl->shared->group_lsn[lsnindex] = lsn;
716 : : }
717 : : }
718 : :
719 : : /*
720 : : * Interrogate the state of a transaction in the commit log.
721 : : *
722 : : * Aside from the actual commit status, this function returns (into *lsn)
723 : : * an LSN that is late enough to be able to guarantee that if we flush up to
724 : : * that LSN then we will have flushed the transaction's commit record to disk.
725 : : * The result is not necessarily the exact LSN of the transaction's commit
726 : : * record! For example, for long-past transactions (those whose clog pages
727 : : * already migrated to disk), we'll return InvalidXLogRecPtr. Also, because
728 : : * we group transactions on the same clog page to conserve storage, we might
729 : : * return the LSN of a later transaction that falls into the same group.
730 : : *
731 : : * NB: this is a low-level routine and is NOT the preferred entry point
732 : : * for most uses; TransactionLogFetch() in transam.c is the intended caller.
733 : : */
734 : : XidStatus
6101 735 : 639910 : TransactionIdGetStatus(TransactionId xid, XLogRecPtr *lsn)
736 : : {
137 akorotkov@postgresql 737 :GNC 639910 : int64 pageno = TransactionIdToPage(xid);
8268 tgl@sss.pgh.pa.us 738 :CBC 639910 : int byteno = TransactionIdToByte(xid);
739 : 639910 : int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
740 : : int slotno;
741 : : int lsnindex;
742 : : char *byteptr;
743 : : XidStatus status;
744 : :
745 : : /* lock is acquired by SimpleLruReadPage_ReadOnly */
746 : :
1430 747 : 639910 : slotno = SimpleLruReadPage_ReadOnly(XactCtl, pageno, xid);
748 : 639910 : byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
749 : :
8268 750 : 639910 : status = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
751 : :
6101 752 : 639910 : lsnindex = GetLSNIndex(slotno, xid);
1430 753 : 639910 : *lsn = XactCtl->shared->group_lsn[lsnindex];
754 : :
46 alvherre@alvh.no-ip. 755 :GNC 639910 : LWLockRelease(SimpleLruGetBankLock(XactCtl, pageno));
756 : :
8268 tgl@sss.pgh.pa.us 757 :CBC 639910 : return status;
758 : : }
759 : :
760 : : /*
761 : : * Number of shared CLOG buffers.
762 : : *
763 : : * If asked to autotune, use 2MB for every 1GB of shared buffers, up to 8MB.
764 : : * Otherwise just cap the configured amount to be between 16 and the maximum
765 : : * allowed.
766 : : */
767 : : static int
4482 rhaas@postgresql.org 768 : 3470 : CLOGShmemBuffers(void)
769 : : {
770 : : /* auto-tune based on shared buffers */
46 alvherre@alvh.no-ip. 771 [ + + ]:GNC 3470 : if (transaction_buffers == 0)
772 : 2567 : return SimpleLruAutotuneBuffers(512, 1024);
773 : :
774 [ + - + - ]: 903 : return Min(Max(16, transaction_buffers), CLOG_MAX_ALLOWED_BUFFERS);
775 : : }
776 : :
777 : : /*
778 : : * Initialization of shared memory for CLOG
779 : : */
780 : : Size
8268 tgl@sss.pgh.pa.us 781 :CBC 1679 : CLOGShmemSize(void)
782 : : {
4482 rhaas@postgresql.org 783 : 1679 : return SimpleLruShmemSize(CLOGShmemBuffers(), CLOG_LSNS_PER_PAGE);
784 : : }
785 : :
786 : : void
8268 tgl@sss.pgh.pa.us 787 : 898 : CLOGShmemInit(void)
788 : : {
789 : : /* If auto-tuning is requested, now is the time to do it */
46 alvherre@alvh.no-ip. 790 [ + + ]:GNC 898 : if (transaction_buffers == 0)
791 : : {
792 : : char buf[32];
793 : :
794 : 893 : snprintf(buf, sizeof(buf), "%d", CLOGShmemBuffers());
795 : 893 : SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
796 : : PGC_S_DYNAMIC_DEFAULT);
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 [ - + ]: 893 : if (transaction_buffers == 0) /* failed to apply it? */
46 alvherre@alvh.no-ip. 805 :UNC 0 : SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
806 : : PGC_S_OVERRIDE);
807 : : }
46 alvherre@alvh.no-ip. 808 [ - + ]:GNC 898 : Assert(transaction_buffers != 0);
809 : :
1430 tgl@sss.pgh.pa.us 810 :CBC 898 : XactCtl->PagePrecedes = CLOGPagePrecedes;
46 alvherre@alvh.no-ip. 811 :GNC 898 : SimpleLruInit(XactCtl, "transaction", CLOGShmemBuffers(), CLOG_LSNS_PER_PAGE,
812 : : "pg_xact", LWTRANCHE_XACT_BUFFER,
813 : : LWTRANCHE_XACT_SLRU, SYNC_HANDLER_CLOG, false);
1184 noah@leadboat.com 814 :CBC 898 : SlruPagePrecedesUnitTests(XactCtl, CLOG_XACTS_PER_PAGE);
8268 tgl@sss.pgh.pa.us 815 : 898 : }
816 : :
817 : : /*
818 : : * GUC check_hook for transaction_buffers
819 : : */
820 : : bool
46 alvherre@alvh.no-ip. 821 :GNC 1821 : check_transaction_buffers(int *newval, void **extra, GucSource source)
822 : : {
823 : 1821 : 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 : : */
832 : : void
8268 tgl@sss.pgh.pa.us 833 :CBC 39 : BootStrapCLOG(void)
834 : : {
835 : : int slotno;
46 alvherre@alvh.no-ip. 836 :GNC 39 : LWLock *lock = SimpleLruGetBankLock(XactCtl, 0);
837 : :
838 : 39 : LWLockAcquire(lock, LW_EXCLUSIVE);
839 : :
840 : : /* Create and zero the first page of the commit log */
8268 tgl@sss.pgh.pa.us 841 :CBC 39 : slotno = ZeroCLOGPage(0, false);
842 : :
843 : : /* Make sure it's written out */
1430 844 : 39 : SimpleLruWritePage(XactCtl, slotno);
845 [ - + ]: 39 : Assert(!XactCtl->shared->page_dirty[slotno]);
846 : :
46 alvherre@alvh.no-ip. 847 :GNC 39 : LWLockRelease(lock);
8268 tgl@sss.pgh.pa.us 848 :CBC 39 : }
849 : :
850 : : /*
851 : : * Initialize (or reinitialize) a page of CLOG to zeroes.
852 : : * If writeXlog is true, also emit an XLOG record saying we did this.
853 : : *
854 : : * The page is not actually written, just set up in shared memory.
855 : : * The slot number of the new page is returned.
856 : : *
857 : : * Control lock must be held at entry, and will be held at exit.
858 : : */
859 : : static int
137 akorotkov@postgresql 860 :GNC 431730 : ZeroCLOGPage(int64 pageno, bool writeXlog)
861 : : {
862 : : int slotno;
863 : :
1430 tgl@sss.pgh.pa.us 864 :CBC 431730 : slotno = SimpleLruZeroPage(XactCtl, pageno);
865 : :
8268 866 [ + + ]: 431730 : if (writeXlog)
867 : 431691 : WriteZeroPageXlogRec(pageno);
868 : :
869 : 431730 : return slotno;
870 : : }
871 : :
872 : : /*
873 : : * This must be called ONCE during postmaster or standalone-backend startup,
874 : : * after StartupXLOG has initialized TransamVariables->nextXid.
875 : : */
876 : : void
877 : 823 : StartupCLOG(void)
878 : : {
128 heikki.linnakangas@i 879 :GNC 823 : TransactionId xid = XidFromFullTransactionId(TransamVariables->nextXid);
137 akorotkov@postgresql 880 : 823 : int64 pageno = TransactionIdToPage(xid);
881 : :
882 : : /*
883 : : * Initialize our idea of the latest page number.
884 : : */
68 alvherre@alvh.no-ip. 885 : 823 : pg_atomic_write_u64(&XactCtl->shared->latest_page_number, pageno);
4547 simon@2ndQuadrant.co 886 :CBC 823 : }
887 : :
888 : : /*
889 : : * This must be called ONCE at the end of startup/recovery.
890 : : */
891 : : void
892 : 729 : TrimCLOG(void)
893 : : {
128 heikki.linnakangas@i 894 :GNC 729 : TransactionId xid = XidFromFullTransactionId(TransamVariables->nextXid);
137 akorotkov@postgresql 895 : 729 : int64 pageno = TransactionIdToPage(xid);
46 alvherre@alvh.no-ip. 896 : 729 : LWLock *lock = SimpleLruGetBankLock(XactCtl, pageno);
897 : :
898 : 729 : LWLockAcquire(lock, LW_EXCLUSIVE);
899 : :
900 : : /*
901 : : * Zero out the remainder of the current clog page. Under normal
902 : : * circumstances it should be zeroes already, but it seems at least
903 : : * theoretically possible that XLOG replay will have settled on a nextXID
904 : : * value that is less than the last XID actually used and marked by the
905 : : * previous database lifecycle (since subtransaction commit writes clog
906 : : * but makes no WAL entry). Let's just be safe. (We need not worry about
907 : : * pages beyond the current one, since those will be zeroed when first
908 : : * used. For the same reason, there is no need to do anything when
909 : : * nextXid is exactly at a page boundary; and it's likely that the
910 : : * "current" page doesn't exist yet in that case.)
911 : : */
7053 tgl@sss.pgh.pa.us 912 [ + + ]:CBC 729 : if (TransactionIdToPgIndex(xid) != 0)
913 : : {
914 : 728 : int byteno = TransactionIdToByte(xid);
915 : 728 : int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
916 : : int slotno;
917 : : char *byteptr;
918 : :
1430 919 : 728 : slotno = SimpleLruReadPage(XactCtl, pageno, false, xid);
920 : 728 : byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
921 : :
922 : : /* Zero so-far-unused positions in the current byte */
7053 923 : 728 : *byteptr &= (1 << bshift) - 1;
924 : : /* Zero the rest of the page */
925 [ + + + - : 728 : MemSet(byteptr + 1, 0, BLCKSZ - byteno - 1);
+ - - + -
- ]
926 : :
1430 927 : 728 : XactCtl->shared->page_dirty[slotno] = true;
928 : : }
929 : :
46 alvherre@alvh.no-ip. 930 :GNC 729 : LWLockRelease(lock);
8268 tgl@sss.pgh.pa.us 931 :CBC 729 : }
932 : :
933 : : /*
934 : : * Perform a checkpoint --- either during shutdown, or on-the-fly
935 : : */
936 : : void
937 : 1153 : CheckPointCLOG(void)
938 : : {
939 : : /*
940 : : * Write dirty CLOG pages to disk. This may result in sync requests
941 : : * queued for later handling by ProcessSyncRequests(), as part of the
942 : : * checkpoint.
943 : : */
944 : : TRACE_POSTGRESQL_CLOG_CHECKPOINT_START(true);
1297 tmunro@postgresql.or 945 : 1153 : SimpleLruWriteAll(XactCtl, true);
946 : : TRACE_POSTGRESQL_CLOG_CHECKPOINT_DONE(true);
8268 tgl@sss.pgh.pa.us 947 : 1153 : }
948 : :
949 : :
950 : : /*
951 : : * Make sure that CLOG has room for a newly-allocated XID.
952 : : *
953 : : * NB: this is called while holding XidGenLock. We want it to be very fast
954 : : * most of the time; even when it's not so fast, no actual I/O need happen
955 : : * unless we're forced to write out a dirty clog or xlog page to make room
956 : : * in shared memory.
957 : : */
958 : : void
959 : 24481530 : ExtendCLOG(TransactionId newestXact)
960 : : {
961 : : int64 pageno;
962 : : LWLock *lock;
963 : :
964 : : /*
965 : : * No work except at first XID of a page. But beware: just after
966 : : * wraparound, the first XID of page zero is FirstNormalTransactionId.
967 : : */
8267 968 [ + + + + ]: 24481530 : if (TransactionIdToPgIndex(newestXact) != 0 &&
969 : : !TransactionIdEquals(newestXact, FirstNormalTransactionId))
8268 970 : 24049839 : return;
971 : :
972 : 431691 : pageno = TransactionIdToPage(newestXact);
46 alvherre@alvh.no-ip. 973 :GNC 431691 : lock = SimpleLruGetBankLock(XactCtl, pageno);
974 : :
975 : 431691 : LWLockAcquire(lock, LW_EXCLUSIVE);
976 : :
977 : : /* Zero the page and make an XLOG entry about it */
3796 heikki.linnakangas@i 978 :CBC 431691 : ZeroCLOGPage(pageno, true);
979 : :
46 alvherre@alvh.no-ip. 980 :GNC 431691 : LWLockRelease(lock);
981 : : }
982 : :
983 : :
984 : : /*
985 : : * Remove all CLOG segments before the one holding the passed transaction ID
986 : : *
987 : : * Before removing any CLOG data, we must flush XLOG to disk, to ensure
988 : : * that any recently-emitted FREEZE_PAGE records have reached disk; otherwise
989 : : * a crash and restart might leave us with some unfrozen tuples referencing
990 : : * removed CLOG data. We choose to emit a special TRUNCATE XLOG record too.
991 : : * Replaying the deletion from XLOG is not critical, since the files could
992 : : * just as well be removed later, but doing so prevents a long-running hot
993 : : * standby server from acquiring an unreasonably bloated CLOG directory.
994 : : *
995 : : * Since CLOG segments hold a large number of transactions, the opportunity to
996 : : * actually remove a segment is fairly rare, and so it seems best not to do
997 : : * the XLOG flush unless we have confirmed that there is a removable segment.
998 : : */
999 : : void
2579 rhaas@postgresql.org 1000 :CBC 742 : TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
1001 : : {
1002 : : int64 cutoffPage;
1003 : :
1004 : : /*
1005 : : * The cutoff point is the start of the segment containing oldestXact. We
1006 : : * pass the *page* containing oldestXact to SimpleLruTruncate.
1007 : : */
8268 tgl@sss.pgh.pa.us 1008 : 742 : cutoffPage = TransactionIdToPage(oldestXact);
1009 : :
1010 : : /* Check to see if there's any files that could be removed */
1430 1011 [ + + ]: 742 : if (!SlruScanDirectory(XactCtl, SlruScanDirCbReportPresence, &cutoffPage))
7174 1012 : 691 : return; /* nothing to remove */
1013 : :
1014 : : /*
1015 : : * Advance oldestClogXid before truncating clog, so concurrent xact status
1016 : : * lookups can ensure they don't attempt to access truncated-away clog.
1017 : : *
1018 : : * It's only necessary to do this if we will actually truncate away clog
1019 : : * pages.
1020 : : */
2579 rhaas@postgresql.org 1021 :GBC 51 : AdvanceOldestClogXid(oldestXact);
1022 : :
1023 : : /*
1024 : : * Write XLOG record and flush XLOG to disk. We record the oldest xid
1025 : : * we're keeping information about here so we can ensure that it's always
1026 : : * ahead of clog truncation in case we crash, and so a standby finds out
1027 : : * the new valid xid before the next checkpoint.
1028 : : */
1029 : 51 : WriteTruncateXlogRec(cutoffPage, oldestXact, oldestxid_datoid);
1030 : :
1031 : : /* Now we can remove the old CLOG segment(s) */
1430 tgl@sss.pgh.pa.us 1032 : 51 : SimpleLruTruncate(XactCtl, cutoffPage);
1033 : : }
1034 : :
1035 : :
1036 : : /*
1037 : : * Decide whether a CLOG page number is "older" for truncation purposes.
1038 : : *
1039 : : * We need to use comparison of TransactionIds here in order to do the right
1040 : : * thing with wraparound XID arithmetic. However, TransactionIdPrecedes()
1041 : : * would get weird about permanent xact IDs. So, offset both such that xid1,
1042 : : * xid2, and xid2 + CLOG_XACTS_PER_PAGE - 1 are all normal XIDs; this offset
1043 : : * is relevant to page 0 and to the page preceding page 0.
1044 : : *
1045 : : * The page containing oldestXact-2^31 is the important edge case. The
1046 : : * portion of that page equaling or following oldestXact-2^31 is expendable,
1047 : : * but the portion preceding oldestXact-2^31 is not. When oldestXact-2^31 is
1048 : : * the first XID of a page and segment, the entire page and segment is
1049 : : * expendable, and we could truncate the segment. Recognizing that case would
1050 : : * require making oldestXact, not just the page containing oldestXact,
1051 : : * available to this callback. The benefit would be rare and small, so we
1052 : : * don't optimize that edge case.
1053 : : */
1054 : : static bool
137 akorotkov@postgresql 1055 :GNC 729676 : CLOGPagePrecedes(int64 page1, int64 page2)
1056 : : {
1057 : : TransactionId xid1;
1058 : : TransactionId xid2;
1059 : :
8207 tgl@sss.pgh.pa.us 1060 :CBC 729676 : xid1 = ((TransactionId) page1) * CLOG_XACTS_PER_PAGE;
1184 noah@leadboat.com 1061 : 729676 : xid1 += FirstNormalTransactionId + 1;
8207 tgl@sss.pgh.pa.us 1062 : 729676 : xid2 = ((TransactionId) page2) * CLOG_XACTS_PER_PAGE;
1184 noah@leadboat.com 1063 : 729676 : xid2 += FirstNormalTransactionId + 1;
1064 : :
1065 [ + + + + ]: 779865 : return (TransactionIdPrecedes(xid1, xid2) &&
1066 : 50189 : TransactionIdPrecedes(xid1, xid2 + CLOG_XACTS_PER_PAGE - 1));
1067 : : }
1068 : :
1069 : :
1070 : : /*
1071 : : * Write a ZEROPAGE xlog record
1072 : : */
1073 : : static void
137 akorotkov@postgresql 1074 :GNC 431691 : WriteZeroPageXlogRec(int64 pageno)
1075 : : {
3433 heikki.linnakangas@i 1076 :CBC 431691 : XLogBeginInsert();
137 akorotkov@postgresql 1077 :GNC 431691 : XLogRegisterData((char *) (&pageno), sizeof(pageno));
3433 heikki.linnakangas@i 1078 :CBC 431691 : (void) XLogInsert(RM_CLOG_ID, CLOG_ZEROPAGE);
8268 tgl@sss.pgh.pa.us 1079 : 431691 : }
1080 : :
1081 : : /*
1082 : : * Write a TRUNCATE xlog record
1083 : : *
1084 : : * We must flush the xlog record to disk before returning --- see notes
1085 : : * in TruncateCLOG().
1086 : : */
1087 : : static void
137 akorotkov@postgresql 1088 :GNC 51 : WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact, Oid oldestXactDb)
1089 : : {
1090 : : XLogRecPtr recptr;
1091 : : xl_clog_truncate xlrec;
1092 : :
2579 rhaas@postgresql.org 1093 :GBC 51 : xlrec.pageno = pageno;
1094 : 51 : xlrec.oldestXact = oldestXact;
1095 : 51 : xlrec.oldestXactDb = oldestXactDb;
1096 : :
3433 heikki.linnakangas@i 1097 : 51 : XLogBeginInsert();
2579 rhaas@postgresql.org 1098 : 51 : XLogRegisterData((char *) (&xlrec), sizeof(xl_clog_truncate));
3433 heikki.linnakangas@i 1099 : 51 : recptr = XLogInsert(RM_CLOG_ID, CLOG_TRUNCATE);
6370 tgl@sss.pgh.pa.us 1100 : 51 : XLogFlush(recptr);
1101 : 51 : }
1102 : :
1103 : : /*
1104 : : * CLOG resource manager's routines
1105 : : */
1106 : : void
3433 heikki.linnakangas@i 1107 :UBC 0 : clog_redo(XLogReaderState *record)
1108 : : {
1109 : 0 : uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1110 : :
1111 : : /* Backup blocks are not used in clog records */
1112 [ # # ]: 0 : Assert(!XLogRecHasAnyBlockRefs(record));
1113 : :
7174 tgl@sss.pgh.pa.us 1114 [ # # ]: 0 : if (info == CLOG_ZEROPAGE)
1115 : : {
1116 : : int64 pageno;
1117 : : int slotno;
1118 : : LWLock *lock;
1119 : :
137 akorotkov@postgresql 1120 :UNC 0 : memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1121 : :
46 alvherre@alvh.no-ip. 1122 : 0 : lock = SimpleLruGetBankLock(XactCtl, pageno);
1123 : 0 : LWLockAcquire(lock, LW_EXCLUSIVE);
1124 : :
7174 tgl@sss.pgh.pa.us 1125 :UBC 0 : slotno = ZeroCLOGPage(pageno, false);
1430 1126 : 0 : SimpleLruWritePage(XactCtl, slotno);
1127 [ # # ]: 0 : Assert(!XactCtl->shared->page_dirty[slotno]);
1128 : :
46 alvherre@alvh.no-ip. 1129 :UNC 0 : LWLockRelease(lock);
1130 : : }
6370 tgl@sss.pgh.pa.us 1131 [ # # ]:UBC 0 : else if (info == CLOG_TRUNCATE)
1132 : : {
1133 : : xl_clog_truncate xlrec;
1134 : :
2579 rhaas@postgresql.org 1135 : 0 : memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_clog_truncate));
1136 : :
1137 : 0 : AdvanceOldestClogXid(xlrec.oldestXact);
1138 : :
1430 tgl@sss.pgh.pa.us 1139 : 0 : SimpleLruTruncate(XactCtl, xlrec.pageno);
1140 : : }
1141 : : else
6370 1142 [ # # ]: 0 : elog(PANIC, "clog_redo: unknown op code %u", info);
7174 1143 : 0 : }
1144 : :
1145 : : /*
1146 : : * Entrypoint for sync.c to sync clog files.
1147 : : */
1148 : : int
1297 tmunro@postgresql.or 1149 : 0 : clogsyncfiletag(const FileTag *ftag, char *path)
1150 : : {
1151 : 0 : return SlruSyncFileTag(XactCtl, ftag, path);
1152 : : }
|