Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * xactdesc.c
4 : * rmgr descriptor routines for access/transam/xact.c
5 : *
6 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/access/rmgrdesc/xactdesc.c
12 : *
13 : *-------------------------------------------------------------------------
14 : */
15 : #include "postgres.h"
16 :
17 : #include "access/transam.h"
18 : #include "access/xact.h"
19 : #include "replication/origin.h"
20 : #include "storage/sinval.h"
21 : #include "storage/standbydefs.h"
22 : #include "utils/timestamp.h"
23 :
24 : /*
25 : * Parse the WAL format of an xact commit and abort records into an easier to
26 : * understand format.
27 : *
28 : * These routines are in xactdesc.c because they're accessed in backend (when
29 : * replaying WAL) and frontend (pg_waldump) code. This file is the only xact
30 : * specific one shared between both. They're complicated enough that
31 : * duplication would be bothersome.
32 : */
33 :
34 : void
35 49916 : ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
36 : {
37 49916 : char *data = ((char *) xlrec) + MinSizeOfXactCommit;
38 :
39 49916 : memset(parsed, 0, sizeof(*parsed));
40 :
41 49916 : parsed->xinfo = 0; /* default, if no XLOG_XACT_HAS_INFO is
42 : * present */
43 :
44 49916 : parsed->xact_time = xlrec->xact_time;
45 :
46 49916 : if (info & XLOG_XACT_HAS_INFO)
47 : {
48 35832 : xl_xact_xinfo *xl_xinfo = (xl_xact_xinfo *) data;
49 :
50 35832 : parsed->xinfo = xl_xinfo->xinfo;
51 :
52 35832 : data += sizeof(xl_xact_xinfo);
53 : }
54 :
55 49916 : if (parsed->xinfo & XACT_XINFO_HAS_DBINFO)
56 : {
57 35506 : xl_xact_dbinfo *xl_dbinfo = (xl_xact_dbinfo *) data;
58 :
59 35506 : parsed->dbId = xl_dbinfo->dbId;
60 35506 : parsed->tsId = xl_dbinfo->tsId;
61 :
62 35506 : data += sizeof(xl_xact_dbinfo);
63 : }
64 :
65 49916 : if (parsed->xinfo & XACT_XINFO_HAS_SUBXACTS)
66 : {
67 522 : xl_xact_subxacts *xl_subxacts = (xl_xact_subxacts *) data;
68 :
69 522 : parsed->nsubxacts = xl_subxacts->nsubxacts;
70 522 : parsed->subxacts = xl_subxacts->subxacts;
71 :
72 522 : data += MinSizeOfXactSubxacts;
73 522 : data += parsed->nsubxacts * sizeof(TransactionId);
74 : }
75 :
76 49916 : if (parsed->xinfo & XACT_XINFO_HAS_RELFILELOCATORS)
77 : {
78 4372 : xl_xact_relfilelocators *xl_rellocators = (xl_xact_relfilelocators *) data;
79 :
80 4372 : parsed->nrels = xl_rellocators->nrels;
81 4372 : parsed->xlocators = xl_rellocators->xlocators;
82 :
83 4372 : data += MinSizeOfXactRelfileLocators;
84 4372 : data += xl_rellocators->nrels * sizeof(RelFileLocator);
85 : }
86 :
87 49916 : if (parsed->xinfo & XACT_XINFO_HAS_DROPPED_STATS)
88 : {
89 5524 : xl_xact_stats_items *xl_drops = (xl_xact_stats_items *) data;
90 :
91 5524 : parsed->nstats = xl_drops->nitems;
92 5524 : parsed->stats = xl_drops->items;
93 :
94 5524 : data += MinSizeOfXactStatsItems;
95 5524 : data += xl_drops->nitems * sizeof(xl_xact_stats_item);
96 : }
97 :
98 49916 : if (parsed->xinfo & XACT_XINFO_HAS_INVALS)
99 : {
100 31924 : xl_xact_invals *xl_invals = (xl_xact_invals *) data;
101 :
102 31924 : parsed->nmsgs = xl_invals->nmsgs;
103 31924 : parsed->msgs = xl_invals->msgs;
104 :
105 31924 : data += MinSizeOfXactInvals;
106 31924 : data += xl_invals->nmsgs * sizeof(SharedInvalidationMessage);
107 : }
108 :
109 49916 : if (parsed->xinfo & XACT_XINFO_HAS_TWOPHASE)
110 : {
111 384 : xl_xact_twophase *xl_twophase = (xl_xact_twophase *) data;
112 :
113 384 : parsed->twophase_xid = xl_twophase->xid;
114 :
115 384 : data += sizeof(xl_xact_twophase);
116 :
117 384 : if (parsed->xinfo & XACT_XINFO_HAS_GID)
118 : {
119 216 : strlcpy(parsed->twophase_gid, data, sizeof(parsed->twophase_gid));
120 216 : data += strlen(data) + 1;
121 : }
122 : }
123 :
124 : /* Note: no alignment is guaranteed after this point */
125 :
126 49916 : if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
127 : {
128 : xl_xact_origin xl_origin;
129 :
130 : /* no alignment is guaranteed, so copy onto stack */
131 168 : memcpy(&xl_origin, data, sizeof(xl_origin));
132 :
133 168 : parsed->origin_lsn = xl_origin.origin_lsn;
134 168 : parsed->origin_timestamp = xl_origin.origin_timestamp;
135 :
136 168 : data += sizeof(xl_xact_origin);
137 : }
138 49916 : }
139 :
140 : void
141 3784 : ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed)
142 : {
143 3784 : char *data = ((char *) xlrec) + MinSizeOfXactAbort;
144 :
145 3784 : memset(parsed, 0, sizeof(*parsed));
146 :
147 3784 : parsed->xinfo = 0; /* default, if no XLOG_XACT_HAS_INFO is
148 : * present */
149 :
150 3784 : parsed->xact_time = xlrec->xact_time;
151 :
152 3784 : if (info & XLOG_XACT_HAS_INFO)
153 : {
154 2472 : xl_xact_xinfo *xl_xinfo = (xl_xact_xinfo *) data;
155 :
156 2472 : parsed->xinfo = xl_xinfo->xinfo;
157 :
158 2472 : data += sizeof(xl_xact_xinfo);
159 : }
160 :
161 3784 : if (parsed->xinfo & XACT_XINFO_HAS_DBINFO)
162 : {
163 88 : xl_xact_dbinfo *xl_dbinfo = (xl_xact_dbinfo *) data;
164 :
165 88 : parsed->dbId = xl_dbinfo->dbId;
166 88 : parsed->tsId = xl_dbinfo->tsId;
167 :
168 88 : data += sizeof(xl_xact_dbinfo);
169 : }
170 :
171 3784 : if (parsed->xinfo & XACT_XINFO_HAS_SUBXACTS)
172 : {
173 46 : xl_xact_subxacts *xl_subxacts = (xl_xact_subxacts *) data;
174 :
175 46 : parsed->nsubxacts = xl_subxacts->nsubxacts;
176 46 : parsed->subxacts = xl_subxacts->subxacts;
177 :
178 46 : data += MinSizeOfXactSubxacts;
179 46 : data += parsed->nsubxacts * sizeof(TransactionId);
180 : }
181 :
182 3784 : if (parsed->xinfo & XACT_XINFO_HAS_RELFILELOCATORS)
183 : {
184 656 : xl_xact_relfilelocators *xl_rellocator = (xl_xact_relfilelocators *) data;
185 :
186 656 : parsed->nrels = xl_rellocator->nrels;
187 656 : parsed->xlocators = xl_rellocator->xlocators;
188 :
189 656 : data += MinSizeOfXactRelfileLocators;
190 656 : data += xl_rellocator->nrels * sizeof(RelFileLocator);
191 : }
192 :
193 3784 : if (parsed->xinfo & XACT_XINFO_HAS_DROPPED_STATS)
194 : {
195 834 : xl_xact_stats_items *xl_drops = (xl_xact_stats_items *) data;
196 :
197 834 : parsed->nstats = xl_drops->nitems;
198 834 : parsed->stats = xl_drops->items;
199 :
200 834 : data += MinSizeOfXactStatsItems;
201 834 : data += xl_drops->nitems * sizeof(xl_xact_stats_item);
202 : }
203 :
204 3784 : if (parsed->xinfo & XACT_XINFO_HAS_TWOPHASE)
205 : {
206 164 : xl_xact_twophase *xl_twophase = (xl_xact_twophase *) data;
207 :
208 164 : parsed->twophase_xid = xl_twophase->xid;
209 :
210 164 : data += sizeof(xl_xact_twophase);
211 :
212 164 : if (parsed->xinfo & XACT_XINFO_HAS_GID)
213 : {
214 88 : strlcpy(parsed->twophase_gid, data, sizeof(parsed->twophase_gid));
215 88 : data += strlen(data) + 1;
216 : }
217 : }
218 :
219 : /* Note: no alignment is guaranteed after this point */
220 :
221 3784 : if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
222 : {
223 : xl_xact_origin xl_origin;
224 :
225 : /* no alignment is guaranteed, so copy onto stack */
226 18 : memcpy(&xl_origin, data, sizeof(xl_origin));
227 :
228 18 : parsed->origin_lsn = xl_origin.origin_lsn;
229 18 : parsed->origin_timestamp = xl_origin.origin_timestamp;
230 :
231 18 : data += sizeof(xl_xact_origin);
232 : }
233 3784 : }
234 :
235 : /*
236 : * ParsePrepareRecord
237 : */
238 : void
239 316 : ParsePrepareRecord(uint8 info, xl_xact_prepare *xlrec, xl_xact_parsed_prepare *parsed)
240 : {
241 : char *bufptr;
242 :
243 316 : bufptr = ((char *) xlrec) + MAXALIGN(sizeof(xl_xact_prepare));
244 :
245 316 : memset(parsed, 0, sizeof(*parsed));
246 :
247 316 : parsed->xact_time = xlrec->prepared_at;
248 316 : parsed->origin_lsn = xlrec->origin_lsn;
249 316 : parsed->origin_timestamp = xlrec->origin_timestamp;
250 316 : parsed->twophase_xid = xlrec->xid;
251 316 : parsed->dbId = xlrec->database;
252 316 : parsed->nsubxacts = xlrec->nsubxacts;
253 316 : parsed->nrels = xlrec->ncommitrels;
254 316 : parsed->nabortrels = xlrec->nabortrels;
255 316 : parsed->nmsgs = xlrec->ninvalmsgs;
256 :
257 316 : strncpy(parsed->twophase_gid, bufptr, xlrec->gidlen);
258 316 : bufptr += MAXALIGN(xlrec->gidlen);
259 :
260 316 : parsed->subxacts = (TransactionId *) bufptr;
261 316 : bufptr += MAXALIGN(xlrec->nsubxacts * sizeof(TransactionId));
262 :
263 316 : parsed->xlocators = (RelFileLocator *) bufptr;
264 316 : bufptr += MAXALIGN(xlrec->ncommitrels * sizeof(RelFileLocator));
265 :
266 316 : parsed->abortlocators = (RelFileLocator *) bufptr;
267 316 : bufptr += MAXALIGN(xlrec->nabortrels * sizeof(RelFileLocator));
268 :
269 316 : parsed->stats = (xl_xact_stats_item *) bufptr;
270 316 : bufptr += MAXALIGN(xlrec->ncommitstats * sizeof(xl_xact_stats_item));
271 :
272 316 : parsed->abortstats = (xl_xact_stats_item *) bufptr;
273 316 : bufptr += MAXALIGN(xlrec->nabortstats * sizeof(xl_xact_stats_item));
274 :
275 316 : parsed->msgs = (SharedInvalidationMessage *) bufptr;
276 316 : bufptr += MAXALIGN(xlrec->ninvalmsgs * sizeof(SharedInvalidationMessage));
277 316 : }
278 :
279 : static void
280 48 : xact_desc_relations(StringInfo buf, char *label, int nrels,
281 : RelFileLocator *xlocators)
282 : {
283 : int i;
284 :
285 48 : if (nrels > 0)
286 : {
287 6 : appendStringInfo(buf, "; %s:", label);
288 22 : for (i = 0; i < nrels; i++)
289 : {
290 16 : appendStringInfo(buf, " %s",
291 16 : relpathperm(xlocators[i], MAIN_FORKNUM).str);
292 : }
293 : }
294 48 : }
295 :
296 : static void
297 48 : xact_desc_subxacts(StringInfo buf, int nsubxacts, TransactionId *subxacts)
298 : {
299 : int i;
300 :
301 48 : if (nsubxacts > 0)
302 : {
303 0 : appendStringInfoString(buf, "; subxacts:");
304 0 : for (i = 0; i < nsubxacts; i++)
305 0 : appendStringInfo(buf, " %u", subxacts[i]);
306 : }
307 48 : }
308 :
309 : static void
310 48 : xact_desc_stats(StringInfo buf, const char *label,
311 : int ndropped, xl_xact_stats_item *dropped_stats)
312 : {
313 : int i;
314 :
315 48 : if (ndropped > 0)
316 : {
317 12 : appendStringInfo(buf, "; %sdropped stats:", label);
318 30 : for (i = 0; i < ndropped; i++)
319 : {
320 18 : uint64 objid =
321 18 : ((uint64) dropped_stats[i].objid_hi) << 32 | dropped_stats[i].objid_lo;
322 :
323 18 : appendStringInfo(buf, " %d/%u/%" PRIu64,
324 18 : dropped_stats[i].kind,
325 18 : dropped_stats[i].dboid,
326 : objid);
327 : }
328 : }
329 48 : }
330 :
331 : static void
332 48 : xact_desc_commit(StringInfo buf, uint8 info, xl_xact_commit *xlrec, RepOriginId origin_id)
333 : {
334 : xl_xact_parsed_commit parsed;
335 :
336 48 : ParseCommitRecord(info, xlrec, &parsed);
337 :
338 : /* If this is a prepared xact, show the xid of the original xact */
339 48 : if (TransactionIdIsValid(parsed.twophase_xid))
340 0 : appendStringInfo(buf, "%u: ", parsed.twophase_xid);
341 :
342 48 : appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
343 :
344 48 : xact_desc_relations(buf, "rels", parsed.nrels, parsed.xlocators);
345 48 : xact_desc_subxacts(buf, parsed.nsubxacts, parsed.subxacts);
346 48 : xact_desc_stats(buf, "", parsed.nstats, parsed.stats);
347 :
348 48 : standby_desc_invalidations(buf, parsed.nmsgs, parsed.msgs, parsed.dbId,
349 : parsed.tsId,
350 48 : XactCompletionRelcacheInitFileInval(parsed.xinfo));
351 :
352 48 : if (XactCompletionApplyFeedback(parsed.xinfo))
353 0 : appendStringInfoString(buf, "; apply_feedback");
354 :
355 48 : if (XactCompletionForceSyncCommit(parsed.xinfo))
356 22 : appendStringInfoString(buf, "; sync");
357 :
358 48 : if (parsed.xinfo & XACT_XINFO_HAS_ORIGIN)
359 : {
360 0 : appendStringInfo(buf, "; origin: node %u, lsn %X/%X, at %s",
361 : origin_id,
362 0 : LSN_FORMAT_ARGS(parsed.origin_lsn),
363 : timestamptz_to_str(parsed.origin_timestamp));
364 : }
365 48 : }
366 :
367 : static void
368 0 : xact_desc_abort(StringInfo buf, uint8 info, xl_xact_abort *xlrec, RepOriginId origin_id)
369 : {
370 : xl_xact_parsed_abort parsed;
371 :
372 0 : ParseAbortRecord(info, xlrec, &parsed);
373 :
374 : /* If this is a prepared xact, show the xid of the original xact */
375 0 : if (TransactionIdIsValid(parsed.twophase_xid))
376 0 : appendStringInfo(buf, "%u: ", parsed.twophase_xid);
377 :
378 0 : appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
379 :
380 0 : xact_desc_relations(buf, "rels", parsed.nrels, parsed.xlocators);
381 0 : xact_desc_subxacts(buf, parsed.nsubxacts, parsed.subxacts);
382 :
383 0 : if (parsed.xinfo & XACT_XINFO_HAS_ORIGIN)
384 : {
385 0 : appendStringInfo(buf, "; origin: node %u, lsn %X/%X, at %s",
386 : origin_id,
387 0 : LSN_FORMAT_ARGS(parsed.origin_lsn),
388 : timestamptz_to_str(parsed.origin_timestamp));
389 : }
390 :
391 0 : xact_desc_stats(buf, "", parsed.nstats, parsed.stats);
392 0 : }
393 :
394 : static void
395 0 : xact_desc_prepare(StringInfo buf, uint8 info, xl_xact_prepare *xlrec, RepOriginId origin_id)
396 : {
397 : xl_xact_parsed_prepare parsed;
398 :
399 0 : ParsePrepareRecord(info, xlrec, &parsed);
400 :
401 0 : appendStringInfo(buf, "gid %s: ", parsed.twophase_gid);
402 0 : appendStringInfoString(buf, timestamptz_to_str(parsed.xact_time));
403 :
404 0 : xact_desc_relations(buf, "rels(commit)", parsed.nrels, parsed.xlocators);
405 0 : xact_desc_relations(buf, "rels(abort)", parsed.nabortrels,
406 : parsed.abortlocators);
407 0 : xact_desc_stats(buf, "commit ", parsed.nstats, parsed.stats);
408 0 : xact_desc_stats(buf, "abort ", parsed.nabortstats, parsed.abortstats);
409 0 : xact_desc_subxacts(buf, parsed.nsubxacts, parsed.subxacts);
410 :
411 0 : standby_desc_invalidations(buf, parsed.nmsgs, parsed.msgs, parsed.dbId,
412 0 : parsed.tsId, xlrec->initfileinval);
413 :
414 : /*
415 : * Check if the replication origin has been set in this record in the same
416 : * way as PrepareRedoAdd().
417 : */
418 0 : if (origin_id != InvalidRepOriginId)
419 0 : appendStringInfo(buf, "; origin: node %u, lsn %X/%X, at %s",
420 : origin_id,
421 0 : LSN_FORMAT_ARGS(parsed.origin_lsn),
422 : timestamptz_to_str(parsed.origin_timestamp));
423 0 : }
424 :
425 : static void
426 0 : xact_desc_assignment(StringInfo buf, xl_xact_assignment *xlrec)
427 : {
428 : int i;
429 :
430 0 : appendStringInfoString(buf, "subxacts:");
431 :
432 0 : for (i = 0; i < xlrec->nsubxacts; i++)
433 0 : appendStringInfo(buf, " %u", xlrec->xsub[i]);
434 0 : }
435 :
436 : void
437 48 : xact_desc(StringInfo buf, XLogReaderState *record)
438 : {
439 48 : char *rec = XLogRecGetData(record);
440 48 : uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
441 :
442 48 : if (info == XLOG_XACT_COMMIT || info == XLOG_XACT_COMMIT_PREPARED)
443 48 : {
444 48 : xl_xact_commit *xlrec = (xl_xact_commit *) rec;
445 :
446 48 : xact_desc_commit(buf, XLogRecGetInfo(record), xlrec,
447 48 : XLogRecGetOrigin(record));
448 : }
449 0 : else if (info == XLOG_XACT_ABORT || info == XLOG_XACT_ABORT_PREPARED)
450 0 : {
451 0 : xl_xact_abort *xlrec = (xl_xact_abort *) rec;
452 :
453 0 : xact_desc_abort(buf, XLogRecGetInfo(record), xlrec,
454 0 : XLogRecGetOrigin(record));
455 : }
456 0 : else if (info == XLOG_XACT_PREPARE)
457 : {
458 0 : xl_xact_prepare *xlrec = (xl_xact_prepare *) rec;
459 :
460 0 : xact_desc_prepare(buf, XLogRecGetInfo(record), xlrec,
461 0 : XLogRecGetOrigin(record));
462 : }
463 0 : else if (info == XLOG_XACT_ASSIGNMENT)
464 : {
465 0 : xl_xact_assignment *xlrec = (xl_xact_assignment *) rec;
466 :
467 : /*
468 : * Note that we ignore the WAL record's xid, since we're more
469 : * interested in the top-level xid that issued the record and which
470 : * xids are being reported here.
471 : */
472 0 : appendStringInfo(buf, "xtop %u: ", xlrec->xtop);
473 0 : xact_desc_assignment(buf, xlrec);
474 : }
475 0 : else if (info == XLOG_XACT_INVALIDATIONS)
476 : {
477 0 : xl_xact_invals *xlrec = (xl_xact_invals *) rec;
478 :
479 0 : standby_desc_invalidations(buf, xlrec->nmsgs, xlrec->msgs, InvalidOid,
480 : InvalidOid, false);
481 : }
482 48 : }
483 :
484 : const char *
485 48 : xact_identify(uint8 info)
486 : {
487 48 : const char *id = NULL;
488 :
489 48 : switch (info & XLOG_XACT_OPMASK)
490 : {
491 48 : case XLOG_XACT_COMMIT:
492 48 : id = "COMMIT";
493 48 : break;
494 0 : case XLOG_XACT_PREPARE:
495 0 : id = "PREPARE";
496 0 : break;
497 0 : case XLOG_XACT_ABORT:
498 0 : id = "ABORT";
499 0 : break;
500 0 : case XLOG_XACT_COMMIT_PREPARED:
501 0 : id = "COMMIT_PREPARED";
502 0 : break;
503 0 : case XLOG_XACT_ABORT_PREPARED:
504 0 : id = "ABORT_PREPARED";
505 0 : break;
506 0 : case XLOG_XACT_ASSIGNMENT:
507 0 : id = "ASSIGNMENT";
508 0 : break;
509 0 : case XLOG_XACT_INVALIDATIONS:
510 0 : id = "INVALIDATION";
511 0 : break;
512 : }
513 :
514 48 : return id;
515 : }
|