Age Owner Branch data TLA 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 : :
1094 msawada@postgresql.o 22 :UBC 0 : 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 : 0 : PG_FUNCTION_INFO_V1(consume_xids);
33 : : Datum
34 : 0 : consume_xids(PG_FUNCTION_ARGS)
35 : : {
36 : 0 : int64 nxids = PG_GETARG_INT64(0);
37 : : FullTransactionId lastxid;
38 : :
10 fujii@postgresql.org 39 [ # # ]: 0 : if (nxids <= 0)
609 peter@eisentraut.org 40 [ # # ]: 0 : elog(ERROR, "invalid nxids argument: %" PRId64, nxids);
41 : :
10 fujii@postgresql.org 42 : 0 : lastxid = consume_xids_common(InvalidFullTransactionId, (uint64) nxids);
43 : :
1094 msawada@postgresql.o 44 : 0 : 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 : 0 : PG_FUNCTION_INFO_V1(consume_xids_until);
56 : : Datum
57 : 0 : consume_xids_until(PG_FUNCTION_ARGS)
58 : : {
59 : 0 : FullTransactionId targetxid = PG_GETARG_FULLTRANSACTIONID(0);
60 : : FullTransactionId lastxid;
61 : :
62 [ # # ]: 0 : if (!FullTransactionIdIsNormal(targetxid))
609 peter@eisentraut.org 63 [ # # ]: 0 : elog(ERROR, "targetxid %" PRIu64 " is not normal",
64 : : U64FromFullTransactionId(targetxid));
65 : :
1094 msawada@postgresql.o 66 : 0 : lastxid = consume_xids_common(targetxid, 0);
67 : :
68 : 0 : 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 : 0 : consume_xids_common(FullTransactionId untilxid, uint64 nxids)
93 : : {
94 : : FullTransactionId lastxid;
95 : 0 : uint64 last_reported_at = 0;
96 : 0 : 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 : 0 : 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 : : */
10 fujii@postgresql.org 113 [ # # ]: 0 : if (!FullTransactionIdIsValid(GetTopFullTransactionIdIfAny()))
114 : : {
115 : 0 : lastxid = GetTopFullTransactionId();
116 : 0 : consumed++;
117 : : }
118 : :
119 : : for (;;)
1094 msawada@postgresql.o 120 : 0 : {
121 : : uint64 xids_left;
122 : :
123 [ # # ]: 0 : CHECK_FOR_INTERRUPTS();
124 : :
125 : : /* How many XIDs do we have left to consume? */
126 [ # # ]: 0 : if (nxids > 0)
127 : : {
128 [ # # ]: 0 : if (consumed >= nxids)
129 : 0 : break;
130 : 0 : xids_left = nxids - consumed;
131 : : }
132 : : else
133 : : {
134 [ # # ]: 0 : if (FullTransactionIdFollowsOrEquals(lastxid, untilxid))
135 : 0 : break;
136 : 0 : 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 : : */
10 fujii@postgresql.org 143 [ # # ]: 0 : if (xids_left > XID_SHORTCUT_THRESHOLD &&
1094 msawada@postgresql.o 144 [ # # ]: 0 : consumed - last_reported_at < REPORT_INTERVAL &&
145 [ # # ]: 0 : MyProc->subxidStatus.overflowed)
146 : : {
147 : 0 : int64 consumed_by_shortcut = consume_xids_shortcut();
148 : :
149 [ # # ]: 0 : if (consumed_by_shortcut > 0)
150 : : {
151 : 0 : consumed += consumed_by_shortcut;
152 : 0 : continue;
153 : : }
154 : : }
155 : :
156 : : /* Slow path: Call GetNewTransactionId to allocate a new XID. */
157 : 0 : lastxid = GetNewTransactionId(true);
158 : 0 : consumed++;
159 : :
160 : : /* Report progress */
161 [ # # ]: 0 : if (consumed - last_reported_at >= REPORT_INTERVAL)
162 : : {
163 [ # # ]: 0 : if (nxids > 0)
609 peter@eisentraut.org 164 [ # # ]: 0 : elog(NOTICE, "consumed %" PRIu64 " / %" PRIu64 " XIDs, latest %u:%u",
165 : : consumed, nxids,
166 : : EpochFromFullTransactionId(lastxid),
167 : : XidFromFullTransactionId(lastxid));
168 : : else
1094 msawada@postgresql.o 169 [ # # ]: 0 : elog(NOTICE, "consumed up to %u:%u / %u:%u",
170 : : EpochFromFullTransactionId(lastxid),
171 : : XidFromFullTransactionId(lastxid),
172 : : EpochFromFullTransactionId(untilxid),
173 : : XidFromFullTransactionId(untilxid));
174 : 0 : last_reported_at = consumed;
175 : : }
176 : : }
177 : :
178 : 0 : 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 : 0 : XidSkip(FullTransactionId fullxid)
189 : : {
190 : 0 : uint32 low = XidFromFullTransactionId(fullxid);
191 : : uint32 rem;
192 : : uint32 distance;
193 : :
194 [ # # # # ]: 0 : if (low < 5 || low >= UINT32_MAX - 5)
195 : 0 : return 0;
196 : 0 : distance = UINT32_MAX - 5 - low;
197 : :
198 : 0 : rem = low % COMMIT_TS_XACTS_PER_PAGE;
199 [ # # ]: 0 : if (rem == 0)
200 : 0 : return 0;
201 : 0 : distance = Min(distance, COMMIT_TS_XACTS_PER_PAGE - rem);
202 : :
203 : 0 : rem = low % SUBTRANS_XACTS_PER_PAGE;
204 [ # # ]: 0 : if (rem == 0)
205 : 0 : return 0;
206 : 0 : distance = Min(distance, SUBTRANS_XACTS_PER_PAGE - rem);
207 : :
208 : 0 : rem = low % CLOG_XACTS_PER_PAGE;
209 [ # # ]: 0 : if (rem == 0)
210 : 0 : return 0;
211 : 0 : distance = Min(distance, CLOG_XACTS_PER_PAGE - rem);
212 : :
213 : : /* The shortcut threshold in consume_xids_common() relies on this */
10 fujii@postgresql.org 214 [ # # ]: 0 : Assert(distance <= XID_SHORTCUT_THRESHOLD);
215 : :
1094 msawada@postgresql.o 216 : 0 : return distance;
217 : : }
218 : :
219 : : static int64
220 : 0 : consume_xids_shortcut(void)
221 : : {
222 : : FullTransactionId nextXid;
223 : : uint32 consumed;
224 : :
225 : 0 : LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
1086 heikki.linnakangas@i 226 : 0 : 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 : : */
1094 msawada@postgresql.o 232 : 0 : consumed = XidSkip(nextXid);
233 [ # # ]: 0 : if (consumed > 0)
1086 heikki.linnakangas@i 234 : 0 : TransamVariables->nextXid.value += (uint64) consumed;
235 : :
1094 msawada@postgresql.o 236 : 0 : LWLockRelease(XidGenLock);
237 : :
238 : 0 : return consumed;
239 : : }
|