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