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