Branch data Line data Source code
1 : : /*--------------------------------------------------------------------------
2 : : *
3 : : * xid_wraparound.c
4 : : * Utilities for testing XID wraparound
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * IDENTIFICATION
11 : : * src/test/modules/xid_wraparound/xid_wraparound.c
12 : : *
13 : : * -------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/xact.h"
18 : : #include "miscadmin.h"
19 : : #include "storage/proc.h"
20 : : #include "utils/xid8.h"
21 : :
22 : 114 : PG_MODULE_MAGIC;
23 : :
24 : : static int64 consume_xids_shortcut(void);
25 : : static FullTransactionId consume_xids_common(FullTransactionId untilxid, uint64 nxids);
26 : :
27 : : /*
28 : : * Consume the specified number of XIDs.
29 : : *
30 : : * Returns the last XID assigned by this function.
31 : : */
32 : 113 : PG_FUNCTION_INFO_V1(consume_xids);
33 : : Datum
34 : 109 : consume_xids(PG_FUNCTION_ARGS)
35 : : {
36 : 109 : int64 nxids = PG_GETARG_INT64(0);
37 : : FullTransactionId lastxid;
38 : :
39 [ - + ]: 109 : if (nxids <= 0)
40 [ # # ]: 0 : elog(ERROR, "invalid nxids argument: %" PRId64, nxids);
41 : :
42 : 109 : lastxid = consume_xids_common(InvalidFullTransactionId, (uint64) nxids);
43 : :
44 : 108 : PG_RETURN_FULLTRANSACTIONID(lastxid);
45 : : }
46 : :
47 : : /*
48 : : * Advance XID allocation so that the next XID is at least the target.
49 : : * This may consume XIDs beyond the target.
50 : : *
51 : : * Ensures that the current transaction has a top-level XID.
52 : : * Returns the last XID assigned by this call, or the next XID observed
53 : : * if no XID was assigned.
54 : : */
55 : 5 : PG_FUNCTION_INFO_V1(consume_xids_until);
56 : : Datum
57 : 1 : consume_xids_until(PG_FUNCTION_ARGS)
58 : : {
59 : 1 : FullTransactionId targetxid = PG_GETARG_FULLTRANSACTIONID(0);
60 : : FullTransactionId lastxid;
61 : :
62 [ - + ]: 1 : if (!FullTransactionIdIsNormal(targetxid))
63 [ # # ]: 0 : elog(ERROR, "targetxid %" PRIu64 " is not normal",
64 : : U64FromFullTransactionId(targetxid));
65 : :
66 : 1 : lastxid = consume_xids_common(targetxid, 0);
67 : :
68 : 1 : PG_RETURN_FULLTRANSACTIONID(lastxid);
69 : : }
70 : :
71 : : /*
72 : : * These constants copied from .c files, because they're private.
73 : : */
74 : : #define COMMIT_TS_XACTS_PER_PAGE (BLCKSZ / 10)
75 : : #define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
76 : : #define CLOG_XACTS_PER_BYTE 4
77 : : #define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE)
78 : :
79 : : /*
80 : : * Take the shortcut in consume_xids_common() only if we have more than this
81 : : * many XIDs left to consume. XidSkip() never skips more than
82 : : * COMMIT_TS_XACTS_PER_PAGE XIDs, which is 3276 with the largest supported
83 : : * block size (32kB), so this is enough to make sure that the shortcut never
84 : : * consumes more XIDs than requested.
85 : : */
86 : : #define XID_SHORTCUT_THRESHOLD 4000
87 : :
88 : : /*
89 : : * Common functionality between the two public functions.
90 : : */
91 : : static FullTransactionId
92 : 110 : consume_xids_common(FullTransactionId untilxid, uint64 nxids)
93 : : {
94 : : FullTransactionId lastxid;
95 : 110 : uint64 last_reported_at = 0;
96 : 110 : uint64 consumed = 0;
97 : :
98 : : /* Print a NOTICE every REPORT_INTERVAL xids */
99 : : #define REPORT_INTERVAL (10 * 1000000)
100 : :
101 : : /* initialize 'lastxid' with the system's current next XID */
102 : 110 : lastxid = ReadNextFullTransactionId();
103 : :
104 : : /*
105 : : * We consume XIDs by calling GetNewTransactionId(true), which marks the
106 : : * consumed XIDs as subtransactions of the current top-level transaction.
107 : : * For that to work, this transaction must have a top-level XID.
108 : : *
109 : : * GetNewTransactionId registers them in the subxid cache in PGPROC, until
110 : : * the cache overflows, but beyond that, we don't keep track of the
111 : : * consumed XIDs.
112 : : */
113 [ + - ]: 110 : if (!FullTransactionIdIsValid(GetTopFullTransactionIdIfAny()))
114 : : {
115 : 110 : lastxid = GetTopFullTransactionId();
116 : 110 : consumed++;
117 : : }
118 : :
119 : : for (;;)
120 : 48754690 : {
121 : : uint64 xids_left;
122 : :
123 [ - + ]: 48754800 : CHECK_FOR_INTERRUPTS();
124 : :
125 : : /* How many XIDs do we have left to consume? */
126 [ + + ]: 48754800 : if (nxids > 0)
127 : : {
128 [ + + ]: 41918749 : if (consumed >= nxids)
129 : 108 : break;
130 : 41918641 : xids_left = nxids - consumed;
131 : : }
132 : : else
133 : : {
134 [ + + ]: 6836051 : if (FullTransactionIdFollowsOrEquals(lastxid, untilxid))
135 : 1 : break;
136 : 6836050 : xids_left = U64FromFullTransactionId(untilxid) - U64FromFullTransactionId(lastxid);
137 : : }
138 : :
139 : : /*
140 : : * If we still have plenty of XIDs to consume, try to take a shortcut
141 : : * and bump up the nextXid counter directly.
142 : : */
143 [ + + ]: 48754691 : if (xids_left > XID_SHORTCUT_THRESHOLD &&
144 [ + + ]: 48360041 : consumed - last_reported_at < REPORT_INTERVAL &&
145 [ + + ]: 48358738 : MyProc->subxidStatus.overflowed)
146 : : {
147 : 48351588 : int64 consumed_by_shortcut = consume_xids_shortcut();
148 : :
149 [ + + ]: 48351588 : if (consumed_by_shortcut > 0)
150 : : {
151 : 24168057 : consumed += consumed_by_shortcut;
152 : 24168057 : continue;
153 : : }
154 : : }
155 : :
156 : : /* Slow path: Call GetNewTransactionId to allocate a new XID. */
157 : 24586634 : lastxid = GetNewTransactionId(true);
158 : 24586633 : consumed++;
159 : :
160 : : /* Report progress */
161 [ + + ]: 24586633 : if (consumed - last_reported_at >= REPORT_INTERVAL)
162 : : {
163 [ + + ]: 1312 : if (nxids > 0)
164 [ + - ]: 1113 : elog(NOTICE, "consumed %" PRIu64 " / %" PRIu64 " XIDs, latest %u:%u",
165 : : consumed, nxids,
166 : : EpochFromFullTransactionId(lastxid),
167 : : XidFromFullTransactionId(lastxid));
168 : : else
169 [ + - ]: 199 : elog(NOTICE, "consumed up to %u:%u / %u:%u",
170 : : EpochFromFullTransactionId(lastxid),
171 : : XidFromFullTransactionId(lastxid),
172 : : EpochFromFullTransactionId(untilxid),
173 : : XidFromFullTransactionId(untilxid));
174 : 1312 : last_reported_at = consumed;
175 : : }
176 : : }
177 : :
178 : 109 : return lastxid;
179 : : }
180 : :
181 : : /*
182 : : * All the interesting action in GetNewTransactionId happens when we extend
183 : : * the SLRUs, or at the uint32 wraparound. If the nextXid counter is not close
184 : : * to any of those interesting values, take a shortcut and bump nextXID
185 : : * directly, close to the next "interesting" value.
186 : : */
187 : : static inline uint32
188 : 48351588 : XidSkip(FullTransactionId fullxid)
189 : : {
190 : 48351588 : uint32 low = XidFromFullTransactionId(fullxid);
191 : : uint32 rem;
192 : : uint32 distance;
193 : :
194 [ + + + + ]: 48351588 : if (low < 5 || low >= UINT32_MAX - 5)
195 : 16 : return 0;
196 : 48351572 : distance = UINT32_MAX - 5 - low;
197 : :
198 : 48351572 : rem = low % COMMIT_TS_XACTS_PER_PAGE;
199 [ + + ]: 48351572 : if (rem == 0)
200 : 17281422 : return 0;
201 : 31070150 : distance = Min(distance, COMMIT_TS_XACTS_PER_PAGE - rem);
202 : :
203 : 31070150 : rem = low % SUBTRANS_XACTS_PER_PAGE;
204 [ + + ]: 31070150 : if (rem == 0)
205 : 6902093 : return 0;
206 : 24168057 : distance = Min(distance, SUBTRANS_XACTS_PER_PAGE - rem);
207 : :
208 : 24168057 : rem = low % CLOG_XACTS_PER_PAGE;
209 [ - + ]: 24168057 : if (rem == 0)
210 : 0 : return 0;
211 : 24168057 : distance = Min(distance, CLOG_XACTS_PER_PAGE - rem);
212 : :
213 : : /* The shortcut threshold in consume_xids_common() relies on this */
214 : : Assert(distance <= XID_SHORTCUT_THRESHOLD);
215 : :
216 : 24168057 : return distance;
217 : : }
218 : :
219 : : static int64
220 : 48351588 : consume_xids_shortcut(void)
221 : : {
222 : : FullTransactionId nextXid;
223 : : uint32 consumed;
224 : :
225 : 48351588 : LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
226 : 48351588 : nextXid = TransamVariables->nextXid;
227 : :
228 : : /*
229 : : * Go slow near the "interesting values". The interesting zones include 5
230 : : * transactions before and after SLRU page switches.
231 : : */
232 : 48351588 : consumed = XidSkip(nextXid);
233 [ + + ]: 48351588 : if (consumed > 0)
234 : 24168057 : TransamVariables->nextXid.value += (uint64) consumed;
235 : :
236 : 48351588 : LWLockRelease(XidGenLock);
237 : :
238 : 48351588 : return consumed;
239 : : }
|