Debian Codegraph @

src/backend/access/transam/twophase.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * twophase.c
4 * Two-phase commit support functions.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * IDENTIFICATION
10 * src/backend/access/transam/twophase.c
11 *
12 * NOTES
13 * Each global transaction is associated with a global transaction
14 * identifier (GID). The client assigns a GID to a postgres
15 * transaction with the PREPARE TRANSACTION command.
16 *
17 * We keep all active global transactions in a shared memory array.
18 * When the PREPARE TRANSACTION command is issued, the GID is
19 * reserved for the transaction in the array. This is done before
20 * a WAL entry is made, because the reservation checks for duplicate
21 * GIDs and aborts the transaction if there already is a global
22 * transaction in prepared state with the same GID.
23 *
24 * A global transaction (gxact) also has dummy PGPROC; this is what keeps
25 * the XID considered running by TransactionIdIsInProgress. It is also
26 * convenient as a PGPROC to hook the gxact's locks to.
27 *
28 * Information to recover prepared transactions in case of crash is
29 * now stored in WAL for the common case. In some cases there will be
30 * an extended period between preparing a GXACT and commit/abort, in
31 * which case we need to separately record prepared transaction data
32 * in permanent storage. This includes locking information, pending
33 * notifications etc. All that state information is written to the
34 * per-transaction state file in the pg_twophase directory.
35 * All prepared transactions will be written prior to shutdown.
36 *
37 * Life track of state data is following:
38 *
39 * * On PREPARE TRANSACTION backend writes state data only to the WAL and
40 * stores pointer to the start of the WAL record in
41 * gxact->prepare_start_lsn.
42 * * If COMMIT occurs before checkpoint then backend reads data from WAL
43 * using prepare_start_lsn.
44 * * On checkpoint state data copied to files in pg_twophase directory and
45 * fsynced
46 * * If COMMIT happens after checkpoint then backend reads state data from
47 * files
48 *
49 * During replay and replication, TwoPhaseState also holds information
50 * about active prepared transactions that haven't been moved to disk yet.
51 *
52 * Replay of twophase records happens by the following rules:
53 *
54 * * At the beginning of recovery, pg_twophase is scanned once, filling
55 * TwoPhaseState with entries marked with gxact->inredo and
56 * gxact->ondisk. Two-phase file data older than the XID horizon of
57 * the redo position are discarded.
58 * * On PREPARE redo, the transaction is added to TwoPhaseState->prepXacts.
59 * gxact->inredo is set to true for such entries.
60 * * On Checkpoint we iterate through TwoPhaseState->prepXacts entries
61 * that have gxact->inredo set and are behind the redo_horizon. We
62 * save them to disk and then switch gxact->ondisk to true.
63 * * On COMMIT/ABORT we delete the entry from TwoPhaseState->prepXacts.
64 * If gxact->ondisk is true, the corresponding entry from the disk
65 * is additionally deleted.
66 * * RecoverPreparedTransactions(), StandbyRecoverPreparedTransactions()
67 * and PrescanPreparedTransactions() have been modified to go through
68 * gxact->inredo entries that have not made it to disk.
69 *
70 *-------------------------------------------------------------------------
71 */
72 #include "postgres.h"
73
74 #include <fcntl.h>
75 #include <sys/stat.h>
76 #include <time.h>
77 #include <unistd.h>
78
79 #include "access/commit_ts.h"
80 #include "access/htup_details.h"
81 #include "access/subtrans.h"
82 #include "access/transam.h"
83 #include "access/twophase.h"
84 #include "access/twophase_rmgr.h"
85 #include "access/xact.h"
86 #include "access/xlog.h"
87 #include "access/xloginsert.h"
88 #include "access/xlogreader.h"
89 #include "access/xlogrecovery.h"
90 #include "access/xlogutils.h"
91 #include "catalog/pg_type.h"
92 #include "catalog/storage.h"
93 #include "funcapi.h"
94 #include "miscadmin.h"
95 #include "pg_trace.h"
96 #include "pgstat.h"
97 #include "replication/origin.h"
98 #include "replication/syncrep.h"
99 #include "storage/fd.h"
100 #include "storage/ipc.h"
101 #include "storage/md.h"
102 #include "storage/predicate.h"
103 #include "storage/proc.h"
104 #include "storage/procarray.h"
105 #include "utils/builtins.h"
106 #include "utils/memutils.h"
107 #include "utils/timestamp.h"
108
109 /*
110 * Directory where Two-phase commit files reside within PGDATA
111 */
112 #define TWOPHASE_DIR "pg_twophase"
113
114 /* GUC variable, can't be changed after startup */
115 int max_prepared_xacts = 0;
116
117 /*
118 * This struct describes one global transaction that is in prepared state
119 * or attempting to become prepared.
120 *
121 * The lifecycle of a global transaction is:
122 *
123 * 1. After checking that the requested GID is not in use, set up an entry in
124 * the TwoPhaseState->prepXacts array with the correct GID and valid = false,
125 * and mark it as locked by my backend.
126 *
127 * 2. After successfully completing prepare, set valid = true and enter the
128 * referenced PGPROC into the global ProcArray.
129 *
130 * 3. To begin COMMIT PREPARED or ROLLBACK PREPARED, check that the entry is
131 * valid and not locked, then mark the entry as locked by storing my current
132 * proc number into locking_backend. This prevents concurrent attempts to
133 * commit or rollback the same prepared xact.
134 *
135 * 4. On completion of COMMIT PREPARED or ROLLBACK PREPARED, remove the entry
136 * from the ProcArray and the TwoPhaseState->prepXacts array and return it to
137 * the freelist.
138 *
139 * Note that if the preparing transaction fails between steps 1 and 2, the
140 * entry must be removed so that the GID and the GlobalTransaction struct
141 * can be reused. See AtAbort_Twophase().
142 *
143 * typedef struct GlobalTransactionData *GlobalTransaction appears in
144 * twophase.h
145 */
146
147 typedef struct GlobalTransactionData
148 {
149 GlobalTransaction next; /* list link for free list */
150 int pgprocno; /* ID of associated dummy PGPROC */
151 TimestampTz prepared_at; /* time of preparation */
152
153 /*
154 * Note that we need to keep track of two LSNs for each GXACT. We keep
155 * track of the start LSN because this is the address we must use to read
156 * state data back from WAL when committing a prepared GXACT. We keep
157 * track of the end LSN because that is the LSN we need to wait for prior
158 * to commit.
159 */
160 XLogRecPtr prepare_start_lsn; /* XLOG offset of prepare record start */
161 XLogRecPtr prepare_end_lsn; /* XLOG offset of prepare record end */
162 TransactionId xid; /* The GXACT id */
163
164 Oid owner; /* ID of user that executed the xact */
165 ProcNumber locking_backend; /* backend currently working on the xact */
166 bool valid; /* true if PGPROC entry is in proc array */
167 bool ondisk; /* true if prepare state file is on disk */
168 bool inredo; /* true if entry was added via xlog_redo */
169 char gid[GIDSIZE]; /* The GID assigned to the prepared xact */
170 } GlobalTransactionData;
171
172 /*
173 * Two Phase Commit shared state. Access to this struct is protected
174 * by TwoPhaseStateLock.
175 */
176 typedef struct TwoPhaseStateData
177 {
178 /* Head of linked list of free GlobalTransactionData structs */
179 GlobalTransaction freeGXacts;
180
181 /* Number of valid prepXacts entries. */
182 int numPrepXacts;
183
184 /* There are max_prepared_xacts items in this array */
185 GlobalTransaction prepXacts[FLEXIBLE_ARRAY_MEMBER];
186 } TwoPhaseStateData;
187
188 static TwoPhaseStateData *TwoPhaseState;
189
190 /*
191 * Global transaction entry currently locked by us, if any. Note that any
192 * access to the entry pointed to by this variable must be protected by
193 * TwoPhaseStateLock, though obviously the pointer itself doesn't need to be
194 * (since it's just local memory).
195 */
196 static GlobalTransaction MyLockedGxact = NULL;
197
198 static bool twophaseExitRegistered = false;
199
200 static void RecordTransactionCommitPrepared(TransactionId xid,
201 int nchildren,
202 TransactionId *children,
203 int nrels,
204 RelFileLocator *rels,
205 int nstats,
206 xl_xact_stats_item *stats,
207 int ninvalmsgs,
208 SharedInvalidationMessage *invalmsgs,
209 bool initfileinval,
210 const char *gid);
211 static void RecordTransactionAbortPrepared(TransactionId xid,
212 int nchildren,
213 TransactionId *children,
214 int nrels,
215 RelFileLocator *rels,
216 int nstats,
217 xl_xact_stats_item *stats,
218 const char *gid);
219 static void ProcessRecords(char *bufptr, TransactionId xid,
220 const TwoPhaseCallback callbacks[]);
221 static void RemoveGXact(GlobalTransaction gxact);
222
223 static void XlogReadTwoPhaseData(XLogRecPtr lsn, char **buf, int *len);
224 static char *ProcessTwoPhaseBuffer(TransactionId xid,
225 XLogRecPtr prepare_start_lsn,
226 bool fromdisk, bool setParent, bool setNextXid);
227 static void MarkAsPreparingGuts(GlobalTransaction gxact, TransactionId xid,
228 const char *gid, TimestampTz prepared_at, Oid owner,
229 Oid databaseid);
230 static void RemoveTwoPhaseFile(TransactionId xid, bool giveWarning);
231 static void RecreateTwoPhaseFile(TransactionId xid, void *content, int len);
232
233 /*
234 * Initialization of shared memory
235 */
236 Size
237 TwoPhaseShmemSize(void)
238 {
239 Size size;
240
241 /* Need the fixed struct, the array of pointers, and the GTD structs */
242 size = offsetof(TwoPhaseStateData, prepXacts);
243 size = add_size(size, mul_size(max_prepared_xacts,
244 sizeof(GlobalTransaction)));
245 size = MAXALIGN(size);
246 size = add_size(size, mul_size(max_prepared_xacts,
247 sizeof(GlobalTransactionData)));
248
249 return size;
250 }
251
252 void
253 TwoPhaseShmemInit(void)
254 {
255 bool found;
256
257 TwoPhaseState = ShmemInitStruct("Prepared Transaction Table",
258 TwoPhaseShmemSize(),
259 &found);
260 if (!IsUnderPostmaster)
261 {
262 GlobalTransaction gxacts;
263 int i;
264
265 Assert(!found);
266 TwoPhaseState->freeGXacts = NULL;
267 TwoPhaseState->numPrepXacts = 0;
268
269 /*
270 * Initialize the linked list of free GlobalTransactionData structs
271 */
272 gxacts = (GlobalTransaction)
273 ((char *) TwoPhaseState +
274 MAXALIGN(offsetof(TwoPhaseStateData, prepXacts) +
275 sizeof(GlobalTransaction) * max_prepared_xacts));
276 for (i = 0; i < max_prepared_xacts; i++)
277 {
278 /* insert into linked list */
279 gxacts[i].next = TwoPhaseState->freeGXacts;
280 TwoPhaseState->freeGXacts = &gxacts[i];
281
282 /* associate it with a PGPROC assigned by InitProcGlobal */
283 gxacts[i].pgprocno = GetNumberFromPGProc(&PreparedXactProcs[i]);
284 }
285 }
286 else
287 Assert(found);
288 }
289
290 /*
291 * Exit hook to unlock the global transaction entry we're working on.
292 */
293 static void
294 AtProcExit_Twophase(int code, Datum arg)
295 {
296 /* same logic as abort */
297 AtAbort_Twophase();
298 }
299
300 /*
301 * Abort hook to unlock the global transaction entry we're working on.
302 */
303 void
304 AtAbort_Twophase(void)
305 {
306 if (MyLockedGxact == NULL)
307 return;
308
309 /*
310 * What to do with the locked global transaction entry? If we were in the
311 * process of preparing the transaction, but haven't written the WAL
312 * record and state file yet, the transaction must not be considered as
313 * prepared. Likewise, if we are in the process of finishing an
314 * already-prepared transaction, and fail after having already written the
315 * 2nd phase commit or rollback record to the WAL, the transaction should
316 * not be considered as prepared anymore. In those cases, just remove the
317 * entry from shared memory.
318 *
319 * Otherwise, the entry must be left in place so that the transaction can
320 * be finished later, so just unlock it.
321 *
322 * If we abort during prepare, after having written the WAL record, we
323 * might not have transferred all locks and other state to the prepared
324 * transaction yet. Likewise, if we abort during commit or rollback,
325 * after having written the WAL record, we might not have released all the
326 * resources held by the transaction yet. In those cases, the in-memory
327 * state can be wrong, but it's too late to back out.
328 */
329 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
330 if (!MyLockedGxact->valid)
331 RemoveGXact(MyLockedGxact);
332 else
333 MyLockedGxact->locking_backend = INVALID_PROC_NUMBER;
334 LWLockRelease(TwoPhaseStateLock);
335
336 MyLockedGxact = NULL;
337 }
338
339 /*
340 * This is called after we have finished transferring state to the prepared
341 * PGPROC entry.
342 */
343 void
344 PostPrepare_Twophase(void)
345 {
346 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
347 MyLockedGxact->locking_backend = INVALID_PROC_NUMBER;
348 LWLockRelease(TwoPhaseStateLock);
349
350 MyLockedGxact = NULL;
351 }
352
353
354 /*
355 * MarkAsPreparing
356 * Reserve the GID for the given transaction.
357 */
358 GlobalTransaction
359 MarkAsPreparing(TransactionId xid, const char *gid,
360 TimestampTz prepared_at, Oid owner, Oid databaseid)
361 {
362 GlobalTransaction gxact;
363 int i;
364
365 if (strlen(gid) >= GIDSIZE)
366 ereport(ERROR,
367 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
368 errmsg("transaction identifier \"%s\" is too long",
369 gid)));
370
371 /* fail immediately if feature is disabled */
372 if (max_prepared_xacts == 0)
373 ereport(ERROR,
374 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
375 errmsg("prepared transactions are disabled"),
376 errhint("Set \"max_prepared_transactions\" to a nonzero value.")));
377
378 /* on first call, register the exit hook */
379 if (!twophaseExitRegistered)
380 {
381 before_shmem_exit(AtProcExit_Twophase, 0);
382 twophaseExitRegistered = true;
383 }
384
385 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
386
387 /* Check for conflicting GID */
388 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
389 {
390 gxact = TwoPhaseState->prepXacts[i];
391 if (strcmp(gxact->gid, gid) == 0)
392 {
393 ereport(ERROR,
394 (errcode(ERRCODE_DUPLICATE_OBJECT),
395 errmsg("transaction identifier \"%s\" is already in use",
396 gid)));
397 }
398 }
399
400 /* Get a free gxact from the freelist */
401 if (TwoPhaseState->freeGXacts == NULL)
402 ereport(ERROR,
403 (errcode(ERRCODE_OUT_OF_MEMORY),
404 errmsg("maximum number of prepared transactions reached"),
405 errhint("Increase \"max_prepared_transactions\" (currently %d).",
406 max_prepared_xacts)));
407 gxact = TwoPhaseState->freeGXacts;
408 TwoPhaseState->freeGXacts = gxact->next;
409
410 MarkAsPreparingGuts(gxact, xid, gid, prepared_at, owner, databaseid);
411
412 gxact->ondisk = false;
413
414 /* And insert it into the active array */
415 Assert(TwoPhaseState->numPrepXacts < max_prepared_xacts);
416 TwoPhaseState->prepXacts[TwoPhaseState->numPrepXacts++] = gxact;
417
418 LWLockRelease(TwoPhaseStateLock);
419
420 return gxact;
421 }
422
423 /*
424 * MarkAsPreparingGuts
425 *
426 * This uses a gxact struct and puts it into the active array.
427 * NOTE: this is also used when reloading a gxact after a crash; so avoid
428 * assuming that we can use very much backend context.
429 *
430 * Note: This function should be called with appropriate locks held.
431 */
432 static void
433 MarkAsPreparingGuts(GlobalTransaction gxact, TransactionId xid, const char *gid,
434 TimestampTz prepared_at, Oid owner, Oid databaseid)
435 {
436 PGPROC *proc;
437 int i;
438
439 Assert(LWLockHeldByMeInMode(TwoPhaseStateLock, LW_EXCLUSIVE));
440
441 Assert(gxact != NULL);
442 proc = GetPGProcByNumber(gxact->pgprocno);
443
444 /* Initialize the PGPROC entry */
445 MemSet(proc, 0, sizeof(PGPROC));
446 dlist_node_init(&proc->links);
447 proc->waitStatus = PROC_WAIT_STATUS_OK;
448 if (LocalTransactionIdIsValid(MyProc->vxid.lxid))
449 {
450 /* clone VXID, for TwoPhaseGetXidByVirtualXID() to find */
451 proc->vxid.lxid = MyProc->vxid.lxid;
452 proc->vxid.procNumber = MyProcNumber;
453 }
454 else
455 {
456 Assert(AmStartupProcess() || !IsPostmasterEnvironment);
457 /* GetLockConflicts() uses this to specify a wait on the XID */
458 proc->vxid.lxid = xid;
459 proc->vxid.procNumber = INVALID_PROC_NUMBER;
460 }
461 proc->xid = xid;
462 Assert(proc->xmin == InvalidTransactionId);
463 proc->delayChkptFlags = 0;
464 proc->statusFlags = 0;
465 proc->pid = 0;
466 proc->databaseId = databaseid;
467 proc->roleId = owner;
468 proc->tempNamespaceId = InvalidOid;
469 proc->isRegularBackend = false;
470 proc->lwWaiting = LW_WS_NOT_WAITING;
471 proc->lwWaitMode = 0;
472 proc->waitLock = NULL;
473 proc->waitProcLock = NULL;
474 pg_atomic_init_u64(&proc->waitStart, 0);
475 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
476 dlist_init(&proc->myProcLocks[i]);
477 /* subxid data must be filled later by GXactLoadSubxactData */
478 proc->subxidStatus.overflowed = false;
479 proc->subxidStatus.count = 0;
480
481 gxact->prepared_at = prepared_at;
482 gxact->xid = xid;
483 gxact->owner = owner;
484 gxact->locking_backend = MyProcNumber;
485 gxact->valid = false;
486 gxact->inredo = false;
487 strcpy(gxact->gid, gid);
488
489 /*
490 * Remember that we have this GlobalTransaction entry locked for us. If we
491 * abort after this, we must release it.
492 */
493 MyLockedGxact = gxact;
494 }
495
496 /*
497 * GXactLoadSubxactData
498 *
499 * If the transaction being persisted had any subtransactions, this must
500 * be called before MarkAsPrepared() to load information into the dummy
501 * PGPROC.
502 */
503 static void
504 GXactLoadSubxactData(GlobalTransaction gxact, int nsubxacts,
505 TransactionId *children)
506 {
507 PGPROC *proc = GetPGProcByNumber(gxact->pgprocno);
508
509 /* We need no extra lock since the GXACT isn't valid yet */
510 if (nsubxacts > PGPROC_MAX_CACHED_SUBXIDS)
511 {
512 proc->subxidStatus.overflowed = true;
513 nsubxacts = PGPROC_MAX_CACHED_SUBXIDS;
514 }
515 if (nsubxacts > 0)
516 {
517 memcpy(proc->subxids.xids, children,
518 nsubxacts * sizeof(TransactionId));
519 proc->subxidStatus.count = nsubxacts;
520 }
521 }
522
523 /*
524 * MarkAsPrepared
525 * Mark the GXACT as fully valid, and enter it into the global ProcArray.
526 *
527 * lock_held indicates whether caller already holds TwoPhaseStateLock.
528 */
529 static void
530 MarkAsPrepared(GlobalTransaction gxact, bool lock_held)
531 {
532 /* Lock here may be overkill, but I'm not convinced of that ... */
533 if (!lock_held)
534 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
535 Assert(!gxact->valid);
536 gxact->valid = true;
537 if (!lock_held)
538 LWLockRelease(TwoPhaseStateLock);
539
540 /*
541 * Put it into the global ProcArray so TransactionIdIsInProgress considers
542 * the XID as still running.
543 */
544 ProcArrayAdd(GetPGProcByNumber(gxact->pgprocno));
545 }
546
547 /*
548 * LockGXact
549 * Locate the prepared transaction and mark it busy for COMMIT or PREPARE.
550 */
551 static GlobalTransaction
552 LockGXact(const char *gid, Oid user)
553 {
554 int i;
555
556 /* on first call, register the exit hook */
557 if (!twophaseExitRegistered)
558 {
559 before_shmem_exit(AtProcExit_Twophase, 0);
560 twophaseExitRegistered = true;
561 }
562
563 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
564
565 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
566 {
567 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
568 PGPROC *proc = GetPGProcByNumber(gxact->pgprocno);
569
570 /* Ignore not-yet-valid GIDs */
571 if (!gxact->valid)
572 continue;
573 if (strcmp(gxact->gid, gid) != 0)
574 continue;
575
576 /* Found it, but has someone else got it locked? */
577 if (gxact->locking_backend != INVALID_PROC_NUMBER)
578 ereport(ERROR,
579 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
580 errmsg("prepared transaction with identifier \"%s\" is busy",
581 gid)));
582
583 if (user != gxact->owner && !superuser_arg(user))
584 ereport(ERROR,
585 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
586 errmsg("permission denied to finish prepared transaction"),
587 errhint("Must be superuser or the user that prepared the transaction.")));
588
589 /*
590 * Note: it probably would be possible to allow committing from
591 * another database; but at the moment NOTIFY is known not to work and
592 * there may be some other issues as well. Hence disallow until
593 * someone gets motivated to make it work.
594 */
595 if (MyDatabaseId != proc->databaseId)
596 ereport(ERROR,
597 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
598 errmsg("prepared transaction belongs to another database"),
599 errhint("Connect to the database where the transaction was prepared to finish it.")));
600
601 /* OK for me to lock it */
602 gxact->locking_backend = MyProcNumber;
603 MyLockedGxact = gxact;
604
605 LWLockRelease(TwoPhaseStateLock);
606
607 return gxact;
608 }
609
610 LWLockRelease(TwoPhaseStateLock);
611
612 ereport(ERROR,
613 (errcode(ERRCODE_UNDEFINED_OBJECT),
614 errmsg("prepared transaction with identifier \"%s\" does not exist",
615 gid)));
616
617 /* NOTREACHED */
618 return NULL;
619 }
620
621 /*
622 * RemoveGXact
623 * Remove the prepared transaction from the shared memory array.
624 *
625 * NB: caller should have already removed it from ProcArray
626 */
627 static void
628 RemoveGXact(GlobalTransaction gxact)
629 {
630 int i;
631
632 Assert(LWLockHeldByMeInMode(TwoPhaseStateLock, LW_EXCLUSIVE));
633
634 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
635 {
636 if (gxact == TwoPhaseState->prepXacts[i])
637 {
638 /* remove from the active array */
639 TwoPhaseState->numPrepXacts--;
640 TwoPhaseState->prepXacts[i] = TwoPhaseState->prepXacts[TwoPhaseState->numPrepXacts];
641
642 /* and put it back in the freelist */
643 gxact->next = TwoPhaseState->freeGXacts;
644 TwoPhaseState->freeGXacts = gxact;
645
646 return;
647 }
648 }
649
650 elog(ERROR, "failed to find %p in GlobalTransaction array", gxact);
651 }
652
653 /*
654 * Returns an array of all prepared transactions for the user-level
655 * function pg_prepared_xact.
656 *
657 * The returned array and all its elements are copies of internal data
658 * structures, to minimize the time we need to hold the TwoPhaseStateLock.
659 *
660 * WARNING -- we return even those transactions that are not fully prepared
661 * yet. The caller should filter them out if he doesn't want them.
662 *
663 * The returned array is palloc'd.
664 */
665 static int
666 GetPreparedTransactionList(GlobalTransaction *gxacts)
667 {
668 GlobalTransaction array;
669 int num;
670 int i;
671
672 LWLockAcquire(TwoPhaseStateLock, LW_SHARED);
673
674 if (TwoPhaseState->numPrepXacts == 0)
675 {
676 LWLockRelease(TwoPhaseStateLock);
677
678 *gxacts = NULL;
679 return 0;
680 }
681
682 num = TwoPhaseState->numPrepXacts;
683 array = (GlobalTransaction) palloc(sizeof(GlobalTransactionData) * num);
684 *gxacts = array;
685 for (i = 0; i < num; i++)
686 memcpy(array + i, TwoPhaseState->prepXacts[i],
687 sizeof(GlobalTransactionData));
688
689 LWLockRelease(TwoPhaseStateLock);
690
691 return num;
692 }
693
694
695 /* Working status for pg_prepared_xact */
696 typedef struct
697 {
698 GlobalTransaction array;
699 int ngxacts;
700 int currIdx;
701 } Working_State;
702
703 /*
704 * pg_prepared_xact
705 * Produce a view with one row per prepared transaction.
706 *
707 * This function is here so we don't have to export the
708 * GlobalTransactionData struct definition.
709 */
710 Datum
711 pg_prepared_xact(PG_FUNCTION_ARGS)
712 {
713 FuncCallContext *funcctx;
714 Working_State *status;
715
716 if (SRF_IS_FIRSTCALL())
717 {
718 TupleDesc tupdesc;
719 MemoryContext oldcontext;
720
721 /* create a function context for cross-call persistence */
722 funcctx = SRF_FIRSTCALL_INIT();
723
724 /*
725 * Switch to memory context appropriate for multiple function calls
726 */
727 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
728
729 /* build tupdesc for result tuples */
730 /* this had better match pg_prepared_xacts view in system_views.sql */
731 tupdesc = CreateTemplateTupleDesc(5);
732 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "transaction",
733 XIDOID, -1, 0);
734 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "gid",
735 TEXTOID, -1, 0);
736 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "prepared",
737 TIMESTAMPTZOID, -1, 0);
738 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "ownerid",
739 OIDOID, -1, 0);
740 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "dbid",
741 OIDOID, -1, 0);
742
743 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
744
745 /*
746 * Collect all the 2PC status information that we will format and send
747 * out as a result set.
748 */
749 status = (Working_State *) palloc(sizeof(Working_State));
750 funcctx->user_fctx = status;
751
752 status->ngxacts = GetPreparedTransactionList(&status->array);
753 status->currIdx = 0;
754
755 MemoryContextSwitchTo(oldcontext);
756 }
757
758 funcctx = SRF_PERCALL_SETUP();
759 status = (Working_State *) funcctx->user_fctx;
760
761 while (status->array != NULL && status->currIdx < status->ngxacts)
762 {
763 GlobalTransaction gxact = &status->array[status->currIdx++];
764 PGPROC *proc = GetPGProcByNumber(gxact->pgprocno);
765 Datum values[5] = {0};
766 bool nulls[5] = {0};
767 HeapTuple tuple;
768 Datum result;
769
770 if (!gxact->valid)
771 continue;
772
773 /*
774 * Form tuple with appropriate data.
775 */
776
777 values[0] = TransactionIdGetDatum(proc->xid);
778 values[1] = CStringGetTextDatum(gxact->gid);
779 values[2] = TimestampTzGetDatum(gxact->prepared_at);
780 values[3] = ObjectIdGetDatum(gxact->owner);
781 values[4] = ObjectIdGetDatum(proc->databaseId);
782
783 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
784 result = HeapTupleGetDatum(tuple);
785 SRF_RETURN_NEXT(funcctx, result);
786 }
787
788 SRF_RETURN_DONE(funcctx);
789 }
790
791 /*
792 * TwoPhaseGetGXact
793 * Get the GlobalTransaction struct for a prepared transaction
794 * specified by XID
795 *
796 * If lock_held is set to true, TwoPhaseStateLock will not be taken, so the
797 * caller had better hold it.
798 */
799 static GlobalTransaction
800 TwoPhaseGetGXact(TransactionId xid, bool lock_held)
801 {
802 GlobalTransaction result = NULL;
803 int i;
804
805 static TransactionId cached_xid = InvalidTransactionId;
806 static GlobalTransaction cached_gxact = NULL;
807
808 Assert(!lock_held || LWLockHeldByMe(TwoPhaseStateLock));
809
810 /*
811 * During a recovery, COMMIT PREPARED, or ABORT PREPARED, we'll be called
812 * repeatedly for the same XID. We can save work with a simple cache.
813 */
814 if (xid == cached_xid)
815 return cached_gxact;
816
817 if (!lock_held)
818 LWLockAcquire(TwoPhaseStateLock, LW_SHARED);
819
820 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
821 {
822 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
823
824 if (gxact->xid == xid)
825 {
826 result = gxact;
827 break;
828 }
829 }
830
831 if (!lock_held)
832 LWLockRelease(TwoPhaseStateLock);
833
834 if (result == NULL) /* should not happen */
835 elog(ERROR, "failed to find GlobalTransaction for xid %u", xid);
836
837 cached_xid = xid;
838 cached_gxact = result;
839
840 return result;
841 }
842
843 /*
844 * TwoPhaseGetXidByVirtualXID
845 * Lookup VXID among xacts prepared since last startup.
846 *
847 * (This won't find recovered xacts.) If more than one matches, return any
848 * and set "have_more" to true. To witness multiple matches, a single
849 * proc number must consume 2^32 LXIDs, with no intervening database restart.
850 */
851 TransactionId
852 TwoPhaseGetXidByVirtualXID(VirtualTransactionId vxid,
853 bool *have_more)
854 {
855 int i;
856 TransactionId result = InvalidTransactionId;
857
858 Assert(VirtualTransactionIdIsValid(vxid));
859 LWLockAcquire(TwoPhaseStateLock, LW_SHARED);
860
861 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
862 {
863 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
864 PGPROC *proc;
865 VirtualTransactionId proc_vxid;
866
867 if (!gxact->valid)
868 continue;
869 proc = GetPGProcByNumber(gxact->pgprocno);
870 GET_VXID_FROM_PGPROC(proc_vxid, *proc);
871 if (VirtualTransactionIdEquals(vxid, proc_vxid))
872 {
873 /*
874 * Startup process sets proc->vxid.procNumber to
875 * INVALID_PROC_NUMBER.
876 */
877 Assert(!gxact->inredo);
878
879 if (result != InvalidTransactionId)
880 {
881 *have_more = true;
882 break;
883 }
884 result = gxact->xid;
885 }
886 }
887
888 LWLockRelease(TwoPhaseStateLock);
889
890 return result;
891 }
892
893 /*
894 * TwoPhaseGetDummyProcNumber
895 * Get the dummy proc number for prepared transaction specified by XID
896 *
897 * Dummy proc numbers are similar to proc numbers of real backends. They
898 * start at FIRST_PREPARED_XACT_PROC_NUMBER, and are unique across all
899 * currently active real backends and prepared transactions. If lock_held is
900 * set to true, TwoPhaseStateLock will not be taken, so the caller had better
901 * hold it.
902 */
903 ProcNumber
904 TwoPhaseGetDummyProcNumber(TransactionId xid, bool lock_held)
905 {
906 GlobalTransaction gxact = TwoPhaseGetGXact(xid, lock_held);
907
908 return gxact->pgprocno;
909 }
910
911 /*
912 * TwoPhaseGetDummyProc
913 * Get the PGPROC that represents a prepared transaction specified by XID
914 *
915 * If lock_held is set to true, TwoPhaseStateLock will not be taken, so the
916 * caller had better hold it.
917 */
918 PGPROC *
919 TwoPhaseGetDummyProc(TransactionId xid, bool lock_held)
920 {
921 GlobalTransaction gxact = TwoPhaseGetGXact(xid, lock_held);
922
923 return GetPGProcByNumber(gxact->pgprocno);
924 }
925
926 /************************************************************************/
927 /* State file support */
928 /************************************************************************/
929
930 /*
931 * Compute the FullTransactionId for the given TransactionId.
932 *
933 * This is safe if the xid has not yet reached COMMIT PREPARED or ROLLBACK
934 * PREPARED. After those commands, concurrent vac_truncate_clog() may make
935 * the xid cease to qualify as allowable. XXX Not all callers limit their
936 * calls accordingly.
937 */
938 static inline FullTransactionId
939 AdjustToFullTransactionId(TransactionId xid)
940 {
941 Assert(TransactionIdIsValid(xid));
942 return FullTransactionIdFromAllowableAt(ReadNextFullTransactionId(), xid);
943 }
944
945 static inline int
946 TwoPhaseFilePath(char *path, TransactionId xid)
947 {
948 FullTransactionId fxid = AdjustToFullTransactionId(xid);
949
950 return snprintf(path, MAXPGPATH, TWOPHASE_DIR "/%08X%08X",
951 EpochFromFullTransactionId(fxid),
952 XidFromFullTransactionId(fxid));
953 }
954
955 /*
956 * 2PC state file format:
957 *
958 * 1. TwoPhaseFileHeader
959 * 2. TransactionId[] (subtransactions)
960 * 3. RelFileLocator[] (files to be deleted at commit)
961 * 4. RelFileLocator[] (files to be deleted at abort)
962 * 5. SharedInvalidationMessage[] (inval messages to be sent at commit)
963 * 6. TwoPhaseRecordOnDisk
964 * 7. ...
965 * 8. TwoPhaseRecordOnDisk (end sentinel, rmid == TWOPHASE_RM_END_ID)
966 * 9. checksum (CRC-32C)
967 *
968 * Each segment except the final checksum is MAXALIGN'd.
969 */
970
971 /*
972 * Header for a 2PC state file
973 */
974 #define TWOPHASE_MAGIC 0x57F94534 /* format identifier */
975
976 typedef xl_xact_prepare TwoPhaseFileHeader;
977
978 /*
979 * Header for each record in a state file
980 *
981 * NOTE: len counts only the rmgr data, not the TwoPhaseRecordOnDisk header.
982 * The rmgr data will be stored starting on a MAXALIGN boundary.
983 */
984 typedef struct TwoPhaseRecordOnDisk
985 {
986 uint32 len; /* length of rmgr data */
987 TwoPhaseRmgrId rmid; /* resource manager for this record */
988 uint16 info; /* flag bits for use by rmgr */
989 } TwoPhaseRecordOnDisk;
990
991 /*
992 * During prepare, the state file is assembled in memory before writing it
993 * to WAL and the actual state file. We use a chain of StateFileChunk blocks
994 * for that.
995 */
996 typedef struct StateFileChunk
997 {
998 char *data;
999 uint32 len;
1000 struct StateFileChunk *next;
1001 } StateFileChunk;
1002
1003 static struct xllist
1004 {
1005 StateFileChunk *head; /* first data block in the chain */
1006 StateFileChunk *tail; /* last block in chain */
1007 uint32 num_chunks;
1008 uint32 bytes_free; /* free bytes left in tail block */
1009 uint32 total_len; /* total data bytes in chain */
1010 } records;
1011
1012
1013 /*
1014 * Append a block of data to records data structure.
1015 *
1016 * NB: each block is padded to a MAXALIGN multiple. This must be
1017 * accounted for when the file is later read!
1018 *
1019 * The data is copied, so the caller is free to modify it afterwards.
1020 */
1021 static void
1022 save_state_data(const void *data, uint32 len)
1023 {
1024 uint32 padlen = MAXALIGN(len);
1025
1026 if (padlen > records.bytes_free)
1027 {
1028 records.tail->next = palloc0(sizeof(StateFileChunk));
1029 records.tail = records.tail->next;
1030 records.tail->len = 0;
1031 records.tail->next = NULL;
1032 records.num_chunks++;
1033
1034 records.bytes_free = Max(padlen, 512);
1035 records.tail->data = palloc(records.bytes_free);
1036 }
1037
1038 memcpy(((char *) records.tail->data) + records.tail->len, data, len);
1039 records.tail->len += padlen;
1040 records.bytes_free -= padlen;
1041 records.total_len += padlen;
1042 }
1043
1044 /*
1045 * Start preparing a state file.
1046 *
1047 * Initializes data structure and inserts the 2PC file header record.
1048 */
1049 void
1050 StartPrepare(GlobalTransaction gxact)
1051 {
1052 PGPROC *proc = GetPGProcByNumber(gxact->pgprocno);
1053 TransactionId xid = gxact->xid;
1054 TwoPhaseFileHeader hdr;
1055 TransactionId *children;
1056 RelFileLocator *commitrels;
1057 RelFileLocator *abortrels;
1058 xl_xact_stats_item *abortstats = NULL;
1059 xl_xact_stats_item *commitstats = NULL;
1060 SharedInvalidationMessage *invalmsgs;
1061
1062 /* Initialize linked list */
1063 records.head = palloc0(sizeof(StateFileChunk));
1064 records.head->len = 0;
1065 records.head->next = NULL;
1066
1067 records.bytes_free = Max(sizeof(TwoPhaseFileHeader), 512);
1068 records.head->data = palloc(records.bytes_free);
1069
1070 records.tail = records.head;
1071 records.num_chunks = 1;
1072
1073 records.total_len = 0;
1074
1075 /* Create header */
1076 hdr.magic = TWOPHASE_MAGIC;
1077 hdr.total_len = 0; /* EndPrepare will fill this in */
1078 hdr.xid = xid;
1079 hdr.database = proc->databaseId;
1080 hdr.prepared_at = gxact->prepared_at;
1081 hdr.owner = gxact->owner;
1082 hdr.nsubxacts = xactGetCommittedChildren(&children);
1083 hdr.ncommitrels = smgrGetPendingDeletes(true, &commitrels);
1084 hdr.nabortrels = smgrGetPendingDeletes(false, &abortrels);
1085 hdr.ncommitstats =
1086 pgstat_get_transactional_drops(true, &commitstats);
1087 hdr.nabortstats =
1088 pgstat_get_transactional_drops(false, &abortstats);
1089 hdr.ninvalmsgs = xactGetCommittedInvalidationMessages(&invalmsgs,
1090 &hdr.initfileinval);
1091 hdr.gidlen = strlen(gxact->gid) + 1; /* Include '\0' */
1092 /* EndPrepare will fill the origin data, if necessary */
1093 hdr.origin_lsn = InvalidXLogRecPtr;
1094 hdr.origin_timestamp = 0;
1095
1096 save_state_data(&hdr, sizeof(TwoPhaseFileHeader));
1097 save_state_data(gxact->gid, hdr.gidlen);
1098
1099 /*
1100 * Add the additional info about subxacts, deletable files and cache
1101 * invalidation messages.
1102 */
1103 if (hdr.nsubxacts > 0)
1104 {
1105 save_state_data(children, hdr.nsubxacts * sizeof(TransactionId));
1106 /* While we have the child-xact data, stuff it in the gxact too */
1107 GXactLoadSubxactData(gxact, hdr.nsubxacts, children);
1108 }
1109 if (hdr.ncommitrels > 0)
1110 {
1111 save_state_data(commitrels, hdr.ncommitrels * sizeof(RelFileLocator));
1112 pfree(commitrels);
1113 }
1114 if (hdr.nabortrels > 0)
1115 {
1116 save_state_data(abortrels, hdr.nabortrels * sizeof(RelFileLocator));
1117 pfree(abortrels);
1118 }
1119 if (hdr.ncommitstats > 0)
1120 {
1121 save_state_data(commitstats,
1122 hdr.ncommitstats * sizeof(xl_xact_stats_item));
1123 pfree(commitstats);
1124 }
1125 if (hdr.nabortstats > 0)
1126 {
1127 save_state_data(abortstats,
1128 hdr.nabortstats * sizeof(xl_xact_stats_item));
1129 pfree(abortstats);
1130 }
1131 if (hdr.ninvalmsgs > 0)
1132 {
1133 save_state_data(invalmsgs,
1134 hdr.ninvalmsgs * sizeof(SharedInvalidationMessage));
1135 pfree(invalmsgs);
1136 }
1137 }
1138
1139 /*
1140 * Finish preparing state data and writing it to WAL.
1141 */
1142 void
1143 EndPrepare(GlobalTransaction gxact)
1144 {
1145 TwoPhaseFileHeader *hdr;
1146 StateFileChunk *record;
1147 bool replorigin;
1148
1149 /* Add the end sentinel to the list of 2PC records */
1150 RegisterTwoPhaseRecord(TWOPHASE_RM_END_ID, 0,
1151 NULL, 0);
1152
1153 /* Go back and fill in total_len in the file header record */
1154 hdr = (TwoPhaseFileHeader *) records.head->data;
1155 Assert(hdr->magic == TWOPHASE_MAGIC);
1156 hdr->total_len = records.total_len + sizeof(pg_crc32c);
1157
1158 replorigin = (replorigin_session_origin != InvalidRepOriginId &&
1159 replorigin_session_origin != DoNotReplicateId);
1160
1161 if (replorigin)
1162 {
1163 hdr->origin_lsn = replorigin_session_origin_lsn;
1164 hdr->origin_timestamp = replorigin_session_origin_timestamp;
1165 }
1166
1167 /*
1168 * If the data size exceeds MaxAllocSize, we won't be able to read it in
1169 * ReadTwoPhaseFile. Check for that now, rather than fail in the case
1170 * where we write data to file and then re-read at commit time.
1171 */
1172 if (hdr->total_len > MaxAllocSize)
1173 ereport(ERROR,
1174 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1175 errmsg("two-phase state file maximum length exceeded")));
1176
1177 /*
1178 * Now writing 2PC state data to WAL. We let the WAL's CRC protection
1179 * cover us, so no need to calculate a separate CRC.
1180 *
1181 * We have to set DELAY_CHKPT_START here, too; otherwise a checkpoint
1182 * starting immediately after the WAL record is inserted could complete
1183 * without fsync'ing our state file. (This is essentially the same kind
1184 * of race condition as the COMMIT-to-clog-write case that
1185 * RecordTransactionCommit uses DELAY_CHKPT_START for; see notes there.)
1186 *
1187 * We save the PREPARE record's location in the gxact for later use by
1188 * CheckPointTwoPhase.
1189 */
1190 XLogEnsureRecordSpace(0, records.num_chunks);
1191
1192 START_CRIT_SECTION();
1193
1194 Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0);
1195 MyProc->delayChkptFlags |= DELAY_CHKPT_START;
1196
1197 XLogBeginInsert();
1198 for (record = records.head; record != NULL; record = record->next)
1199 XLogRegisterData(record->data, record->len);
1200
1201 XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
1202
1203 gxact->prepare_end_lsn = XLogInsert(RM_XACT_ID, XLOG_XACT_PREPARE);
1204
1205 if (replorigin)
1206 {
1207 /* Move LSNs forward for this replication origin */
1208 replorigin_session_advance(replorigin_session_origin_lsn,
1209 gxact->prepare_end_lsn);
1210 }
1211
1212 XLogFlush(gxact->prepare_end_lsn);
1213
1214 /* If we crash now, we have prepared: WAL replay will fix things */
1215
1216 /* Store record's start location to read that later on Commit */
1217 gxact->prepare_start_lsn = ProcLastRecPtr;
1218
1219 /*
1220 * Mark the prepared transaction as valid. As soon as xact.c marks MyProc
1221 * as not running our XID (which it will do immediately after this
1222 * function returns), others can commit/rollback the xact.
1223 *
1224 * NB: a side effect of this is to make a dummy ProcArray entry for the
1225 * prepared XID. This must happen before we clear the XID from MyProc /
1226 * ProcGlobal->xids[], else there is a window where the XID is not running
1227 * according to TransactionIdIsInProgress, and onlookers would be entitled
1228 * to assume the xact crashed. Instead we have a window where the same
1229 * XID appears twice in ProcArray, which is OK.
1230 */
1231 MarkAsPrepared(gxact, false);
1232
1233 /*
1234 * Now we can mark ourselves as out of the commit critical section: a
1235 * checkpoint starting after this will certainly see the gxact as a
1236 * candidate for fsyncing.
1237 */
1238 MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
1239
1240 /*
1241 * Remember that we have this GlobalTransaction entry locked for us. If
1242 * we crash after this point, it's too late to abort, but we must unlock
1243 * it so that the prepared transaction can be committed or rolled back.
1244 */
1245 MyLockedGxact = gxact;
1246
1247 END_CRIT_SECTION();
1248
1249 /*
1250 * Wait for synchronous replication, if required.
1251 *
1252 * Note that at this stage we have marked the prepare, but still show as
1253 * running in the procarray (twice!) and continue to hold locks.
1254 */
1255 SyncRepWaitForLSN(gxact->prepare_end_lsn, false);
1256
1257 records.tail = records.head = NULL;
1258 records.num_chunks = 0;
1259 }
1260
1261 /*
1262 * Register a 2PC record to be written to state file.
1263 */
1264 void
1265 RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info,
1266 const void *data, uint32 len)
1267 {
1268 TwoPhaseRecordOnDisk record;
1269
1270 record.rmid = rmid;
1271 record.info = info;
1272 record.len = len;
1273 save_state_data(&record, sizeof(TwoPhaseRecordOnDisk));
1274 if (len > 0)
1275 save_state_data(data, len);
1276 }
1277
1278
1279 /*
1280 * Read and validate the state file for xid.
1281 *
1282 * If it looks OK (has a valid magic number and CRC), return the palloc'd
1283 * contents of the file, issuing an error when finding corrupted data. If
1284 * missing_ok is true, which indicates that missing files can be safely
1285 * ignored, then return NULL. This state can be reached when doing recovery.
1286 */
1287 static char *
1288 ReadTwoPhaseFile(TransactionId xid, bool missing_ok)
1289 {
1290 char path[MAXPGPATH];
1291 char *buf;
1292 TwoPhaseFileHeader *hdr;
1293 int fd;
1294 struct stat stat;
1295 uint32 crc_offset;
1296 pg_crc32c calc_crc,
1297 file_crc;
1298 int r;
1299
1300 TwoPhaseFilePath(path, xid);
1301
1302 fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
1303 if (fd < 0)
1304 {
1305 if (missing_ok && errno == ENOENT)
1306 return NULL;
1307
1308 ereport(ERROR,
1309 (errcode_for_file_access(),
1310 errmsg("could not open file \"%s\": %m", path)));
1311 }
1312
1313 /*
1314 * Check file length. We can determine a lower bound pretty easily. We
1315 * set an upper bound to avoid palloc() failure on a corrupt file, though
1316 * we can't guarantee that we won't get an out of memory error anyway,
1317 * even on a valid file.
1318 */
1319 if (fstat(fd, &stat))
1320 ereport(ERROR,
1321 (errcode_for_file_access(),
1322 errmsg("could not stat file \"%s\": %m", path)));
1323
1324 if (stat.st_size < (MAXALIGN(sizeof(TwoPhaseFileHeader)) +
1325 MAXALIGN(sizeof(TwoPhaseRecordOnDisk)) +
1326 sizeof(pg_crc32c)) ||
1327 stat.st_size > MaxAllocSize)
1328 ereport(ERROR,
1329 (errcode(ERRCODE_DATA_CORRUPTED),
1330 errmsg_plural("incorrect size of file \"%s\": %lld byte",
1331 "incorrect size of file \"%s\": %lld bytes",
1332 (long long int) stat.st_size, path,
1333 (long long int) stat.st_size)));
1334
1335 crc_offset = stat.st_size - sizeof(pg_crc32c);
1336 if (crc_offset != MAXALIGN(crc_offset))
1337 ereport(ERROR,
1338 (errcode(ERRCODE_DATA_CORRUPTED),
1339 errmsg("incorrect alignment of CRC offset for file \"%s\"",
1340 path)));
1341
1342 /*
1343 * OK, slurp in the file.
1344 */
1345 buf = (char *) palloc(stat.st_size);
1346
1347 pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_READ);
1348 r = read(fd, buf, stat.st_size);
1349 if (r != stat.st_size)
1350 {
1351 if (r < 0)
1352 ereport(ERROR,
1353 (errcode_for_file_access(),
1354 errmsg("could not read file \"%s\": %m", path)));
1355 else
1356 ereport(ERROR,
1357 (errmsg("could not read file \"%s\": read %d of %lld",
1358 path, r, (long long int) stat.st_size)));
1359 }
1360
1361 pgstat_report_wait_end();
1362
1363 if (CloseTransientFile(fd) != 0)
1364 ereport(ERROR,
1365 (errcode_for_file_access(),
1366 errmsg("could not close file \"%s\": %m", path)));
1367
1368 hdr = (TwoPhaseFileHeader *) buf;
1369 if (hdr->magic != TWOPHASE_MAGIC)
1370 ereport(ERROR,
1371 (errcode(ERRCODE_DATA_CORRUPTED),
1372 errmsg("invalid magic number stored in file \"%s\"",
1373 path)));
1374
1375 if (hdr->total_len != stat.st_size)
1376 ereport(ERROR,
1377 (errcode(ERRCODE_DATA_CORRUPTED),
1378 errmsg("invalid size stored in file \"%s\"",
1379 path)));
1380
1381 INIT_CRC32C(calc_crc);
1382 COMP_CRC32C(calc_crc, buf, crc_offset);
1383 FIN_CRC32C(calc_crc);
1384
1385 file_crc = *((pg_crc32c *) (buf + crc_offset));
1386
1387 if (!EQ_CRC32C(calc_crc, file_crc))
1388 ereport(ERROR,
1389 (errcode(ERRCODE_DATA_CORRUPTED),
1390 errmsg("calculated CRC checksum does not match value stored in file \"%s\"",
1391 path)));
1392
1393 return buf;
1394 }
1395
1396
1397 /*
1398 * Reads 2PC data from xlog. During checkpoint this data will be moved to
1399 * twophase files and ReadTwoPhaseFile should be used instead.
1400 *
1401 * Note clearly that this function can access WAL during normal operation,
1402 * similarly to the way WALSender or Logical Decoding would do.
1403 */
1404 static void
1405 XlogReadTwoPhaseData(XLogRecPtr lsn, char **buf, int *len)
1406 {
1407 XLogRecord *record;
1408 XLogReaderState *xlogreader;
1409 char *errormsg;
1410
1411 xlogreader = XLogReaderAllocate(wal_segment_size, NULL,
1412 XL_ROUTINE(.page_read = &read_local_xlog_page,
1413 .segment_open = &wal_segment_open,
1414 .segment_close = &wal_segment_close),
1415 NULL);
1416 if (!xlogreader)
1417 ereport(ERROR,
1418 (errcode(ERRCODE_OUT_OF_MEMORY),
1419 errmsg("out of memory"),
1420 errdetail("Failed while allocating a WAL reading processor.")));
1421
1422 XLogBeginRead(xlogreader, lsn);
1423 record = XLogReadRecord(xlogreader, &errormsg);
1424
1425 if (record == NULL)
1426 {
1427 if (errormsg)
1428 ereport(ERROR,
1429 (errcode_for_file_access(),
1430 errmsg("could not read two-phase state from WAL at %X/%X: %s",
1431 LSN_FORMAT_ARGS(lsn), errormsg)));
1432 else
1433 ereport(ERROR,
1434 (errcode_for_file_access(),
1435 errmsg("could not read two-phase state from WAL at %X/%X",
1436 LSN_FORMAT_ARGS(lsn))));
1437 }
1438
1439 if (XLogRecGetRmid(xlogreader) != RM_XACT_ID ||
1440 (XLogRecGetInfo(xlogreader) & XLOG_XACT_OPMASK) != XLOG_XACT_PREPARE)
1441 ereport(ERROR,
1442 (errcode_for_file_access(),
1443 errmsg("expected two-phase state data is not present in WAL at %X/%X",
1444 LSN_FORMAT_ARGS(lsn))));
1445
1446 if (len != NULL)
1447 *len = XLogRecGetDataLen(xlogreader);
1448
1449 *buf = palloc(sizeof(char) * XLogRecGetDataLen(xlogreader));
1450 memcpy(*buf, XLogRecGetData(xlogreader), sizeof(char) * XLogRecGetDataLen(xlogreader));
1451
1452 XLogReaderFree(xlogreader);
1453 }
1454
1455
1456 /*
1457 * Confirms an xid is prepared, during recovery
1458 */
1459 bool
1460 StandbyTransactionIdIsPrepared(TransactionId xid)
1461 {
1462 char *buf;
1463 TwoPhaseFileHeader *hdr;
1464 bool result;
1465
1466 Assert(TransactionIdIsValid(xid));
1467
1468 if (max_prepared_xacts <= 0)
1469 return false; /* nothing to do */
1470
1471 /* Read and validate file */
1472 buf = ReadTwoPhaseFile(xid, true);
1473 if (buf == NULL)
1474 return false;
1475
1476 /* Check header also */
1477 hdr = (TwoPhaseFileHeader *) buf;
1478 result = TransactionIdEquals(hdr->xid, xid);
1479 pfree(buf);
1480
1481 return result;
1482 }
1483
1484 /*
1485 * FinishPreparedTransaction: execute COMMIT PREPARED or ROLLBACK PREPARED
1486 */
1487 void
1488 FinishPreparedTransaction(const char *gid, bool isCommit)
1489 {
1490 GlobalTransaction gxact;
1491 PGPROC *proc;
1492 TransactionId xid;
1493 bool ondisk;
1494 char *buf;
1495 char *bufptr;
1496 TwoPhaseFileHeader *hdr;
1497 TransactionId latestXid;
1498 TransactionId *children;
1499 RelFileLocator *commitrels;
1500 RelFileLocator *abortrels;
1501 RelFileLocator *delrels;
1502 int ndelrels;
1503 xl_xact_stats_item *commitstats;
1504 xl_xact_stats_item *abortstats;
1505 SharedInvalidationMessage *invalmsgs;
1506
1507 /*
1508 * Validate the GID, and lock the GXACT to ensure that two backends do not
1509 * try to commit the same GID at once.
1510 */
1511 gxact = LockGXact(gid, GetUserId());
1512 proc = GetPGProcByNumber(gxact->pgprocno);
1513 xid = gxact->xid;
1514
1515 /*
1516 * Read and validate 2PC state data. State data will typically be stored
1517 * in WAL files if the LSN is after the last checkpoint record, or moved
1518 * to disk if for some reason they have lived for a long time.
1519 */
1520 if (gxact->ondisk)
1521 buf = ReadTwoPhaseFile(xid, false);
1522 else
1523 XlogReadTwoPhaseData(gxact->prepare_start_lsn, &buf, NULL);
1524
1525
1526 /*
1527 * Disassemble the header area
1528 */
1529 hdr = (TwoPhaseFileHeader *) buf;
1530 Assert(TransactionIdEquals(hdr->xid, xid));
1531 bufptr = buf + MAXALIGN(sizeof(TwoPhaseFileHeader));
1532 bufptr += MAXALIGN(hdr->gidlen);
1533 children = (TransactionId *) bufptr;
1534 bufptr += MAXALIGN(hdr->nsubxacts * sizeof(TransactionId));
1535 commitrels = (RelFileLocator *) bufptr;
1536 bufptr += MAXALIGN(hdr->ncommitrels * sizeof(RelFileLocator));
1537 abortrels = (RelFileLocator *) bufptr;
1538 bufptr += MAXALIGN(hdr->nabortrels * sizeof(RelFileLocator));
1539 commitstats = (xl_xact_stats_item *) bufptr;
1540 bufptr += MAXALIGN(hdr->ncommitstats * sizeof(xl_xact_stats_item));
1541 abortstats = (xl_xact_stats_item *) bufptr;
1542 bufptr += MAXALIGN(hdr->nabortstats * sizeof(xl_xact_stats_item));
1543 invalmsgs = (SharedInvalidationMessage *) bufptr;
1544 bufptr += MAXALIGN(hdr->ninvalmsgs * sizeof(SharedInvalidationMessage));
1545
1546 /* compute latestXid among all children */
1547 latestXid = TransactionIdLatest(xid, hdr->nsubxacts, children);
1548
1549 /* Prevent cancel/die interrupt while cleaning up */
1550 HOLD_INTERRUPTS();
1551
1552 /*
1553 * The order of operations here is critical: make the XLOG entry for
1554 * commit or abort, then mark the transaction committed or aborted in
1555 * pg_xact, then remove its PGPROC from the global ProcArray (which means
1556 * TransactionIdIsInProgress will stop saying the prepared xact is in
1557 * progress), then run the post-commit or post-abort callbacks. The
1558 * callbacks will release the locks the transaction held.
1559 */
1560 if (isCommit)
1561 RecordTransactionCommitPrepared(xid,
1562 hdr->nsubxacts, children,
1563 hdr->ncommitrels, commitrels,
1564 hdr->ncommitstats,
1565 commitstats,
1566 hdr->ninvalmsgs, invalmsgs,
1567 hdr->initfileinval, gid);
1568 else
1569 RecordTransactionAbortPrepared(xid,
1570 hdr->nsubxacts, children,
1571 hdr->nabortrels, abortrels,
1572 hdr->nabortstats,
1573 abortstats,
1574 gid);
1575
1576 ProcArrayRemove(proc, latestXid);
1577
1578 /*
1579 * In case we fail while running the callbacks, mark the gxact invalid so
1580 * no one else will try to commit/rollback, and so it will be recycled if
1581 * we fail after this point. It is still locked by our backend so it
1582 * won't go away yet.
1583 *
1584 * (We assume it's safe to do this without taking TwoPhaseStateLock.)
1585 */
1586 gxact->valid = false;
1587
1588 /*
1589 * We have to remove any files that were supposed to be dropped. For
1590 * consistency with the regular xact.c code paths, must do this before
1591 * releasing locks, so do it before running the callbacks.
1592 *
1593 * NB: this code knows that we couldn't be dropping any temp rels ...
1594 */
1595 if (isCommit)
1596 {
1597 delrels = commitrels;
1598 ndelrels = hdr->ncommitrels;
1599 }
1600 else
1601 {
1602 delrels = abortrels;
1603 ndelrels = hdr->nabortrels;
1604 }
1605
1606 /* Make sure files supposed to be dropped are dropped */
1607 DropRelationFiles(delrels, ndelrels, false);
1608
1609 if (isCommit)
1610 pgstat_execute_transactional_drops(hdr->ncommitstats, commitstats, false);
1611 else
1612 pgstat_execute_transactional_drops(hdr->nabortstats, abortstats, false);
1613
1614 /*
1615 * Handle cache invalidation messages.
1616 *
1617 * Relcache init file invalidation requires processing both before and
1618 * after we send the SI messages, only when committing. See
1619 * AtEOXact_Inval().
1620 */
1621 if (isCommit)
1622 {
1623 if (hdr->initfileinval)
1624 RelationCacheInitFilePreInvalidate();
1625 SendSharedInvalidMessages(invalmsgs, hdr->ninvalmsgs);
1626 if (hdr->initfileinval)
1627 RelationCacheInitFilePostInvalidate();
1628 }
1629
1630 /*
1631 * Acquire the two-phase lock. We want to work on the two-phase callbacks
1632 * while holding it to avoid potential conflicts with other transactions
1633 * attempting to use the same GID, so the lock is released once the shared
1634 * memory state is cleared.
1635 */
1636 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
1637
1638 /* And now do the callbacks */
1639 if (isCommit)
1640 ProcessRecords(bufptr, xid, twophase_postcommit_callbacks);
1641 else
1642 ProcessRecords(bufptr, xid, twophase_postabort_callbacks);
1643
1644 PredicateLockTwoPhaseFinish(xid, isCommit);
1645
1646 /*
1647 * Read this value while holding the two-phase lock, as the on-disk 2PC
1648 * file is physically removed after the lock is released.
1649 */
1650 ondisk = gxact->ondisk;
1651
1652 /* Clear shared memory state */
1653 RemoveGXact(gxact);
1654
1655 /*
1656 * Release the lock as all callbacks are called and shared memory cleanup
1657 * is done.
1658 */
1659 LWLockRelease(TwoPhaseStateLock);
1660
1661 /* Count the prepared xact as committed or aborted */
1662 AtEOXact_PgStat(isCommit, false);
1663
1664 /*
1665 * And now we can clean up any files we may have left.
1666 */
1667 if (ondisk)
1668 RemoveTwoPhaseFile(xid, true);
1669
1670 MyLockedGxact = NULL;
1671
1672 RESUME_INTERRUPTS();
1673
1674 pfree(buf);
1675 }
1676
1677 /*
1678 * Scan 2PC state data in memory and call the indicated callbacks for each 2PC record.
1679 */
1680 static void
1681 ProcessRecords(char *bufptr, TransactionId xid,
1682 const TwoPhaseCallback callbacks[])
1683 {
1684 for (;;)
1685 {
1686 TwoPhaseRecordOnDisk *record = (TwoPhaseRecordOnDisk *) bufptr;
1687
1688 Assert(record->rmid <= TWOPHASE_RM_MAX_ID);
1689 if (record->rmid == TWOPHASE_RM_END_ID)
1690 break;
1691
1692 bufptr += MAXALIGN(sizeof(TwoPhaseRecordOnDisk));
1693
1694 if (callbacks[record->rmid] != NULL)
1695 callbacks[record->rmid] (xid, record->info, bufptr, record->len);
1696
1697 bufptr += MAXALIGN(record->len);
1698 }
1699 }
1700
1701 /*
1702 * Remove the 2PC file for the specified XID.
1703 *
1704 * If giveWarning is false, do not complain about file-not-present;
1705 * this is an expected case during WAL replay.
1706 */
1707 static void
1708 RemoveTwoPhaseFile(TransactionId xid, bool giveWarning)
1709 {
1710 char path[MAXPGPATH];
1711
1712 TwoPhaseFilePath(path, xid);
1713 if (unlink(path))
1714 if (errno != ENOENT || giveWarning)
1715 ereport(WARNING,
1716 (errcode_for_file_access(),
1717 errmsg("could not remove file \"%s\": %m", path)));
1718 }
1719
1720 /*
1721 * Recreates a state file. This is used in WAL replay and during
1722 * checkpoint creation.
1723 *
1724 * Note: content and len don't include CRC.
1725 */
1726 static void
1727 RecreateTwoPhaseFile(TransactionId xid, void *content, int len)
1728 {
1729 char path[MAXPGPATH];
1730 pg_crc32c statefile_crc;
1731 int fd;
1732
1733 /* Recompute CRC */
1734 INIT_CRC32C(statefile_crc);
1735 COMP_CRC32C(statefile_crc, content, len);
1736 FIN_CRC32C(statefile_crc);
1737
1738 TwoPhaseFilePath(path, xid);
1739
1740 fd = OpenTransientFile(path,
1741 O_CREAT | O_TRUNC | O_WRONLY | PG_BINARY);
1742 if (fd < 0)
1743 ereport(ERROR,
1744 (errcode_for_file_access(),
1745 errmsg("could not recreate file \"%s\": %m", path)));
1746
1747 /* Write content and CRC */
1748 errno = 0;
1749 pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_WRITE);
1750 if (write(fd, content, len) != len)
1751 {
1752 /* if write didn't set errno, assume problem is no disk space */
1753 if (errno == 0)
1754 errno = ENOSPC;
1755 ereport(ERROR,
1756 (errcode_for_file_access(),
1757 errmsg("could not write file \"%s\": %m", path)));
1758 }
1759 if (write(fd, &statefile_crc, sizeof(pg_crc32c)) != sizeof(pg_crc32c))
1760 {
1761 /* if write didn't set errno, assume problem is no disk space */
1762 if (errno == 0)
1763 errno = ENOSPC;
1764 ereport(ERROR,
1765 (errcode_for_file_access(),
1766 errmsg("could not write file \"%s\": %m", path)));
1767 }
1768 pgstat_report_wait_end();
1769
1770 /*
1771 * We must fsync the file because the end-of-replay checkpoint will not do
1772 * so, there being no GXACT in shared memory yet to tell it to.
1773 */
1774 pgstat_report_wait_start(WAIT_EVENT_TWOPHASE_FILE_SYNC);
1775 if (pg_fsync(fd) != 0)
1776 ereport(ERROR,
1777 (errcode_for_file_access(),
1778 errmsg("could not fsync file \"%s\": %m", path)));
1779 pgstat_report_wait_end();
1780
1781 if (CloseTransientFile(fd) != 0)
1782 ereport(ERROR,
1783 (errcode_for_file_access(),
1784 errmsg("could not close file \"%s\": %m", path)));
1785 }
1786
1787 /*
1788 * CheckPointTwoPhase -- handle 2PC component of checkpointing.
1789 *
1790 * We must fsync the state file of any GXACT that is valid or has been
1791 * generated during redo and has a PREPARE LSN <= the checkpoint's redo
1792 * horizon. (If the gxact isn't valid yet, has not been generated in
1793 * redo, or has a later LSN, this checkpoint is not responsible for
1794 * fsyncing it.)
1795 *
1796 * This is deliberately run as late as possible in the checkpoint sequence,
1797 * because GXACTs ordinarily have short lifespans, and so it is quite
1798 * possible that GXACTs that were valid at checkpoint start will no longer
1799 * exist if we wait a little bit. With typical checkpoint settings this
1800 * will be about 3 minutes for an online checkpoint, so as a result we
1801 * expect that there will be no GXACTs that need to be copied to disk.
1802 *
1803 * If a GXACT remains valid across multiple checkpoints, it will already
1804 * be on disk so we don't bother to repeat that write.
1805 */
1806 void
1807 CheckPointTwoPhase(XLogRecPtr redo_horizon)
1808 {
1809 int i;
1810 int serialized_xacts = 0;
1811
1812 if (max_prepared_xacts <= 0)
1813 return; /* nothing to do */
1814
1815 TRACE_POSTGRESQL_TWOPHASE_CHECKPOINT_START();
1816
1817 /*
1818 * We are expecting there to be zero GXACTs that need to be copied to
1819 * disk, so we perform all I/O while holding TwoPhaseStateLock for
1820 * simplicity. This prevents any new xacts from preparing while this
1821 * occurs, which shouldn't be a problem since the presence of long-lived
1822 * prepared xacts indicates the transaction manager isn't active.
1823 *
1824 * It's also possible to move I/O out of the lock, but on every error we
1825 * should check whether somebody committed our transaction in different
1826 * backend. Let's leave this optimization for future, if somebody will
1827 * spot that this place cause bottleneck.
1828 *
1829 * Note that it isn't possible for there to be a GXACT with a
1830 * prepare_end_lsn set prior to the last checkpoint yet is marked invalid,
1831 * because of the efforts with delayChkptFlags.
1832 */
1833 LWLockAcquire(TwoPhaseStateLock, LW_SHARED);
1834 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
1835 {
1836 /*
1837 * Note that we are using gxact not PGPROC so this works in recovery
1838 * also
1839 */
1840 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
1841
1842 if ((gxact->valid || gxact->inredo) &&
1843 !gxact->ondisk &&
1844 gxact->prepare_end_lsn <= redo_horizon)
1845 {
1846 char *buf;
1847 int len;
1848
1849 XlogReadTwoPhaseData(gxact->prepare_start_lsn, &buf, &len);
1850 RecreateTwoPhaseFile(gxact->xid, buf, len);
1851 gxact->ondisk = true;
1852 gxact->prepare_start_lsn = InvalidXLogRecPtr;
1853 gxact->prepare_end_lsn = InvalidXLogRecPtr;
1854 pfree(buf);
1855 serialized_xacts++;
1856 }
1857 }
1858 LWLockRelease(TwoPhaseStateLock);
1859
1860 /*
1861 * Flush unconditionally the parent directory to make any information
1862 * durable on disk. Two-phase files could have been removed and those
1863 * removals need to be made persistent as well as any files newly created
1864 * previously since the last checkpoint.
1865 */
1866 fsync_fname(TWOPHASE_DIR, true);
1867
1868 TRACE_POSTGRESQL_TWOPHASE_CHECKPOINT_DONE();
1869
1870 if (log_checkpoints && serialized_xacts > 0)
1871 ereport(LOG,
1872 (errmsg_plural("%u two-phase state file was written "
1873 "for a long-running prepared transaction",
1874 "%u two-phase state files were written "
1875 "for long-running prepared transactions",
1876 serialized_xacts,
1877 serialized_xacts)));
1878 }
1879
1880 /*
1881 * restoreTwoPhaseData
1882 *
1883 * Scan pg_twophase and fill TwoPhaseState depending on the on-disk data.
1884 * This is called once at the beginning of recovery, saving any extra
1885 * lookups in the future. Two-phase files that are newer than the
1886 * minimum XID horizon are discarded on the way.
1887 */
1888 void
1889 restoreTwoPhaseData(void)
1890 {
1891 DIR *cldir;
1892 struct dirent *clde;
1893
1894 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
1895 cldir = AllocateDir(TWOPHASE_DIR);
1896 while ((clde = ReadDir(cldir, TWOPHASE_DIR)) != NULL)
1897 {
1898 if (strlen(clde->d_name) == 16 &&
1899 strspn(clde->d_name, "0123456789ABCDEF") == 16)
1900 {
1901 TransactionId xid;
1902 FullTransactionId fxid;
1903 char *buf;
1904
1905 fxid = FullTransactionIdFromU64(strtou64(clde->d_name, NULL, 16));
1906 xid = XidFromFullTransactionId(fxid);
1907
1908 buf = ProcessTwoPhaseBuffer(xid, InvalidXLogRecPtr,
1909 true, false, false);
1910 if (buf == NULL)
1911 continue;
1912
1913 PrepareRedoAdd(buf, InvalidXLogRecPtr,
1914 InvalidXLogRecPtr, InvalidRepOriginId);
1915 }
1916 }
1917 LWLockRelease(TwoPhaseStateLock);
1918 FreeDir(cldir);
1919 }
1920
1921 /*
1922 * PrescanPreparedTransactions
1923 *
1924 * Scan the shared memory entries of TwoPhaseState and determine the range
1925 * of valid XIDs present. This is run during database startup, after we
1926 * have completed reading WAL. TransamVariables->nextXid has been set to
1927 * one more than the highest XID for which evidence exists in WAL.
1928 *
1929 * We throw away any prepared xacts with main XID beyond nextXid --- if any
1930 * are present, it suggests that the DBA has done a PITR recovery to an
1931 * earlier point in time without cleaning out pg_twophase. We dare not
1932 * try to recover such prepared xacts since they likely depend on database
1933 * state that doesn't exist now.
1934 *
1935 * However, we will advance nextXid beyond any subxact XIDs belonging to
1936 * valid prepared xacts. We need to do this since subxact commit doesn't
1937 * write a WAL entry, and so there might be no evidence in WAL of those
1938 * subxact XIDs.
1939 *
1940 * On corrupted two-phase files, fail immediately. Keeping around broken
1941 * entries and let replay continue causes harm on the system, and a new
1942 * backup should be rolled in.
1943 *
1944 * Our other responsibility is to determine and return the oldest valid XID
1945 * among the prepared xacts (if none, return TransamVariables->nextXid).
1946 * This is needed to synchronize pg_subtrans startup properly.
1947 *
1948 * If xids_p and nxids_p are not NULL, pointer to a palloc'd array of all
1949 * top-level xids is stored in *xids_p. The number of entries in the array
1950 * is returned in *nxids_p.
1951 */
1952 TransactionId
1953 PrescanPreparedTransactions(TransactionId **xids_p, int *nxids_p)
1954 {
1955 FullTransactionId nextXid = TransamVariables->nextXid;
1956 TransactionId origNextXid = XidFromFullTransactionId(nextXid);
1957 TransactionId result = origNextXid;
1958 TransactionId *xids = NULL;
1959 int nxids = 0;
1960 int allocsize = 0;
1961 int i;
1962
1963 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
1964 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
1965 {
1966 TransactionId xid;
1967 char *buf;
1968 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
1969
1970 Assert(gxact->inredo);
1971
1972 xid = gxact->xid;
1973
1974 buf = ProcessTwoPhaseBuffer(xid,
1975 gxact->prepare_start_lsn,
1976 gxact->ondisk, false, true);
1977
1978 if (buf == NULL)
1979 continue;
1980
1981 /*
1982 * OK, we think this file is valid. Incorporate xid into the
1983 * running-minimum result.
1984 */
1985 if (TransactionIdPrecedes(xid, result))
1986 result = xid;
1987
1988 if (xids_p)
1989 {
1990 if (nxids == allocsize)
1991 {
1992 if (nxids == 0)
1993 {
1994 allocsize = 10;
1995 xids = palloc(allocsize * sizeof(TransactionId));
1996 }
1997 else
1998 {
1999 allocsize = allocsize * 2;
2000 xids = repalloc(xids, allocsize * sizeof(TransactionId));
2001 }
2002 }
2003 xids[nxids++] = xid;
2004 }
2005
2006 pfree(buf);
2007 }
2008 LWLockRelease(TwoPhaseStateLock);
2009
2010 if (xids_p)
2011 {
2012 *xids_p = xids;
2013 *nxids_p = nxids;
2014 }
2015
2016 return result;
2017 }
2018
2019 /*
2020 * StandbyRecoverPreparedTransactions
2021 *
2022 * Scan the shared memory entries of TwoPhaseState and setup all the required
2023 * information to allow standby queries to treat prepared transactions as still
2024 * active.
2025 *
2026 * This is never called at the end of recovery - we use
2027 * RecoverPreparedTransactions() at that point.
2028 *
2029 * This updates pg_subtrans, so that any subtransactions will be correctly
2030 * seen as in-progress in snapshots taken during recovery.
2031 */
2032 void
2033 StandbyRecoverPreparedTransactions(void)
2034 {
2035 int i;
2036
2037 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
2038 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
2039 {
2040 TransactionId xid;
2041 char *buf;
2042 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
2043
2044 Assert(gxact->inredo);
2045
2046 xid = gxact->xid;
2047
2048 buf = ProcessTwoPhaseBuffer(xid,
2049 gxact->prepare_start_lsn,
2050 gxact->ondisk, true, false);
2051 if (buf != NULL)
2052 pfree(buf);
2053 }
2054 LWLockRelease(TwoPhaseStateLock);
2055 }
2056
2057 /*
2058 * RecoverPreparedTransactions
2059 *
2060 * Scan the shared memory entries of TwoPhaseState and reload the state for
2061 * each prepared transaction (reacquire locks, etc).
2062 *
2063 * This is run at the end of recovery, but before we allow backends to write
2064 * WAL.
2065 *
2066 * At the end of recovery the way we take snapshots will change. We now need
2067 * to mark all running transactions with their full SubTransSetParent() info
2068 * to allow normal snapshots to work correctly if snapshots overflow.
2069 * We do this here because by definition prepared transactions are the only
2070 * type of write transaction still running, so this is necessary and
2071 * complete.
2072 */
2073 void
2074 RecoverPreparedTransactions(void)
2075 {
2076 int i;
2077
2078 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
2079 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
2080 {
2081 TransactionId xid;
2082 char *buf;
2083 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
2084 char *bufptr;
2085 TwoPhaseFileHeader *hdr;
2086 TransactionId *subxids;
2087 const char *gid;
2088
2089 xid = gxact->xid;
2090
2091 /*
2092 * Reconstruct subtrans state for the transaction --- needed because
2093 * pg_subtrans is not preserved over a restart. Note that we are
2094 * linking all the subtransactions directly to the top-level XID;
2095 * there may originally have been a more complex hierarchy, but
2096 * there's no need to restore that exactly. It's possible that
2097 * SubTransSetParent has been set before, if the prepared transaction
2098 * generated xid assignment records.
2099 */
2100 buf = ProcessTwoPhaseBuffer(xid,
2101 gxact->prepare_start_lsn,
2102 gxact->ondisk, true, false);
2103 if (buf == NULL)
2104 continue;
2105
2106 ereport(LOG,
2107 (errmsg("recovering prepared transaction %u from shared memory", xid)));
2108
2109 hdr = (TwoPhaseFileHeader *) buf;
2110 Assert(TransactionIdEquals(hdr->xid, xid));
2111 bufptr = buf + MAXALIGN(sizeof(TwoPhaseFileHeader));
2112 gid = (const char *) bufptr;
2113 bufptr += MAXALIGN(hdr->gidlen);
2114 subxids = (TransactionId *) bufptr;
2115 bufptr += MAXALIGN(hdr->nsubxacts * sizeof(TransactionId));
2116 bufptr += MAXALIGN(hdr->ncommitrels * sizeof(RelFileLocator));
2117 bufptr += MAXALIGN(hdr->nabortrels * sizeof(RelFileLocator));
2118 bufptr += MAXALIGN(hdr->ncommitstats * sizeof(xl_xact_stats_item));
2119 bufptr += MAXALIGN(hdr->nabortstats * sizeof(xl_xact_stats_item));
2120 bufptr += MAXALIGN(hdr->ninvalmsgs * sizeof(SharedInvalidationMessage));
2121
2122 /*
2123 * Recreate its GXACT and dummy PGPROC. But, check whether it was
2124 * added in redo and already has a shmem entry for it.
2125 */
2126 MarkAsPreparingGuts(gxact, xid, gid,
2127 hdr->prepared_at,
2128 hdr->owner, hdr->database);
2129
2130 /* recovered, so reset the flag for entries generated by redo */
2131 gxact->inredo = false;
2132
2133 GXactLoadSubxactData(gxact, hdr->nsubxacts, subxids);
2134 MarkAsPrepared(gxact, true);
2135
2136 LWLockRelease(TwoPhaseStateLock);
2137
2138 /*
2139 * Recover other state (notably locks) using resource managers.
2140 */
2141 ProcessRecords(bufptr, xid, twophase_recover_callbacks);
2142
2143 /*
2144 * Release locks held by the standby process after we process each
2145 * prepared transaction. As a result, we don't need too many
2146 * additional locks at any one time.
2147 */
2148 if (InHotStandby)
2149 StandbyReleaseLockTree(xid, hdr->nsubxacts, subxids);
2150
2151 /*
2152 * We're done with recovering this transaction. Clear MyLockedGxact,
2153 * like we do in PrepareTransaction() during normal operation.
2154 */
2155 PostPrepare_Twophase();
2156
2157 pfree(buf);
2158
2159 LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
2160 }
2161
2162 LWLockRelease(TwoPhaseStateLock);
2163 }
2164
2165 /*
2166 * ProcessTwoPhaseBuffer
2167 *
2168 * Given a transaction id, read it either from disk or read it directly
2169 * via shmem xlog record pointer using the provided "prepare_start_lsn".
2170 *
2171 * If setParent is true, set up subtransaction parent linkages.
2172 *
2173 * If setNextXid is true, set TransamVariables->nextXid to the newest
2174 * value scanned.
2175 */
2176 static char *
2177 ProcessTwoPhaseBuffer(TransactionId xid,
2178 XLogRecPtr prepare_start_lsn,
2179 bool fromdisk,
2180 bool setParent, bool setNextXid)
2181 {
2182 FullTransactionId nextXid = TransamVariables->nextXid;
2183 TransactionId origNextXid = XidFromFullTransactionId(nextXid);
2184 TransactionId *subxids;
2185 char *buf;
2186 TwoPhaseFileHeader *hdr;
2187 int i;
2188
2189 Assert(LWLockHeldByMeInMode(TwoPhaseStateLock, LW_EXCLUSIVE));
2190
2191 if (!fromdisk)
2192 Assert(prepare_start_lsn != InvalidXLogRecPtr);
2193
2194 /* Already processed? */
2195 if (TransactionIdDidCommit(xid) || TransactionIdDidAbort(xid))
2196 {
2197 if (fromdisk)
2198 {
2199 ereport(WARNING,
2200 (errmsg("removing stale two-phase state file for transaction %u",
2201 xid)));
2202 RemoveTwoPhaseFile(xid, true);
2203 }
2204 else
2205 {
2206 ereport(WARNING,
2207 (errmsg("removing stale two-phase state from memory for transaction %u",
2208 xid)));
2209 PrepareRedoRemove(xid, true);
2210 }
2211 return NULL;
2212 }
2213
2214 /* Reject XID if too new */
2215 if (TransactionIdFollowsOrEquals(xid, origNextXid))
2216 {
2217 if (fromdisk)
2218 {
2219 ereport(WARNING,
2220 (errmsg("removing future two-phase state file for transaction %u",
2221 xid)));
2222 RemoveTwoPhaseFile(xid, true);
2223 }
2224 else
2225 {
2226 ereport(WARNING,
2227 (errmsg("removing future two-phase state from memory for transaction %u",
2228 xid)));
2229 PrepareRedoRemove(xid, true);
2230 }
2231 return NULL;
2232 }
2233
2234 if (fromdisk)
2235 {
2236 /* Read and validate file */
2237 buf = ReadTwoPhaseFile(xid, false);
2238 }
2239 else
2240 {
2241 /* Read xlog data */
2242 XlogReadTwoPhaseData(prepare_start_lsn, &buf, NULL);
2243 }
2244
2245 /* Deconstruct header */
2246 hdr = (TwoPhaseFileHeader *) buf;
2247 if (!TransactionIdEquals(hdr->xid, xid))
2248 {
2249 if (fromdisk)
2250 ereport(ERROR,
2251 (errcode(ERRCODE_DATA_CORRUPTED),
2252 errmsg("corrupted two-phase state file for transaction %u",
2253 xid)));
2254 else
2255 ereport(ERROR,
2256 (errcode(ERRCODE_DATA_CORRUPTED),
2257 errmsg("corrupted two-phase state in memory for transaction %u",
2258 xid)));
2259 }
2260
2261 /*
2262 * Examine subtransaction XIDs ... they should all follow main XID, and
2263 * they may force us to advance nextXid.
2264 */
2265 subxids = (TransactionId *) (buf +
2266 MAXALIGN(sizeof(TwoPhaseFileHeader)) +
2267 MAXALIGN(hdr->gidlen));
2268 for (i = 0; i < hdr->nsubxacts; i++)
2269 {
2270 TransactionId subxid = subxids[i];
2271
2272 Assert(TransactionIdFollows(subxid, xid));
2273
2274 /* update nextXid if needed */
2275 if (setNextXid)
2276 AdvanceNextFullTransactionIdPastXid(subxid);
2277
2278 if (setParent)
2279 SubTransSetParent(subxid, xid);
2280 }
2281
2282 return buf;
2283 }
2284
2285
2286 /*
2287 * RecordTransactionCommitPrepared
2288 *
2289 * This is basically the same as RecordTransactionCommit (q.v. if you change
2290 * this function): in particular, we must set DELAY_CHKPT_START to avoid a
2291 * race condition.
2292 *
2293 * We know the transaction made at least one XLOG entry (its PREPARE),
2294 * so it is never possible to optimize out the commit record.
2295 */
2296 static void
2297 RecordTransactionCommitPrepared(TransactionId xid,
2298 int nchildren,
2299 TransactionId *children,
2300 int nrels,
2301 RelFileLocator *rels,
2302 int nstats,
2303 xl_xact_stats_item *stats,
2304 int ninvalmsgs,
2305 SharedInvalidationMessage *invalmsgs,
2306 bool initfileinval,
2307 const char *gid)
2308 {
2309 XLogRecPtr recptr;
2310 TimestampTz committs = GetCurrentTimestamp();
2311 bool replorigin;
2312
2313 /*
2314 * Are we using the replication origins feature? Or, in other words, are
2315 * we replaying remote actions?
2316 */
2317 replorigin = (replorigin_session_origin != InvalidRepOriginId &&
2318 replorigin_session_origin != DoNotReplicateId);
2319
2320 START_CRIT_SECTION();
2321
2322 /* See notes in RecordTransactionCommit */
2323 Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0);
2324 MyProc->delayChkptFlags |= DELAY_CHKPT_START;
2325
2326 /*
2327 * Emit the XLOG commit record. Note that we mark 2PC commits as
2328 * potentially having AccessExclusiveLocks since we don't know whether or
2329 * not they do.
2330 */
2331 recptr = XactLogCommitRecord(committs,
2332 nchildren, children, nrels, rels,
2333 nstats, stats,
2334 ninvalmsgs, invalmsgs,
2335 initfileinval,
2336 MyXactFlags | XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK,
2337 xid, gid);
2338
2339
2340 if (replorigin)
2341 /* Move LSNs forward for this replication origin */
2342 replorigin_session_advance(replorigin_session_origin_lsn,
2343 XactLastRecEnd);
2344
2345 /*
2346 * Record commit timestamp. The value comes from plain commit timestamp
2347 * if replorigin is not enabled, or replorigin already set a value for us
2348 * in replorigin_session_origin_timestamp otherwise.
2349 *
2350 * We don't need to WAL-log anything here, as the commit record written
2351 * above already contains the data.
2352 */
2353 if (!replorigin || replorigin_session_origin_timestamp == 0)
2354 replorigin_session_origin_timestamp = committs;
2355
2356 TransactionTreeSetCommitTsData(xid, nchildren, children,
2357 replorigin_session_origin_timestamp,
2358 replorigin_session_origin);
2359
2360 /*
2361 * We don't currently try to sleep before flush here ... nor is there any
2362 * support for async commit of a prepared xact (the very idea is probably
2363 * a contradiction)
2364 */
2365
2366 /* Flush XLOG to disk */
2367 XLogFlush(recptr);
2368
2369 /* Mark the transaction committed in pg_xact */
2370 TransactionIdCommitTree(xid, nchildren, children);
2371
2372 /* Checkpoint can proceed now */
2373 MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
2374
2375 END_CRIT_SECTION();
2376
2377 /*
2378 * Wait for synchronous replication, if required.
2379 *
2380 * Note that at this stage we have marked clog, but still show as running
2381 * in the procarray and continue to hold locks.
2382 */
2383 SyncRepWaitForLSN(recptr, true);
2384 }
2385
2386 /*
2387 * RecordTransactionAbortPrepared
2388 *
2389 * This is basically the same as RecordTransactionAbort.
2390 *
2391 * We know the transaction made at least one XLOG entry (its PREPARE),
2392 * so it is never possible to optimize out the abort record.
2393 */
2394 static void
2395 RecordTransactionAbortPrepared(TransactionId xid,
2396 int nchildren,
2397 TransactionId *children,
2398 int nrels,
2399 RelFileLocator *rels,
2400 int nstats,
2401 xl_xact_stats_item *stats,
2402 const char *gid)
2403 {
2404 XLogRecPtr recptr;
2405 bool replorigin;
2406
2407 /*
2408 * Are we using the replication origins feature? Or, in other words, are
2409 * we replaying remote actions?
2410 */
2411 replorigin = (replorigin_session_origin != InvalidRepOriginId &&
2412 replorigin_session_origin != DoNotReplicateId);
2413
2414 /*
2415 * Catch the scenario where we aborted partway through
2416 * RecordTransactionCommitPrepared ...
2417 */
2418 if (TransactionIdDidCommit(xid))
2419 elog(PANIC, "cannot abort transaction %u, it was already committed",
2420 xid);
2421
2422 START_CRIT_SECTION();
2423
2424 /*
2425 * Emit the XLOG commit record. Note that we mark 2PC aborts as
2426 * potentially having AccessExclusiveLocks since we don't know whether or
2427 * not they do.
2428 */
2429 recptr = XactLogAbortRecord(GetCurrentTimestamp(),
2430 nchildren, children,
2431 nrels, rels,
2432 nstats, stats,
2433 MyXactFlags | XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK,
2434 xid, gid);
2435
2436 if (replorigin)
2437 /* Move LSNs forward for this replication origin */
2438 replorigin_session_advance(replorigin_session_origin_lsn,
2439 XactLastRecEnd);
2440
2441 /* Always flush, since we're about to remove the 2PC state file */
2442 XLogFlush(recptr);
2443
2444 /*
2445 * Mark the transaction aborted in clog. This is not absolutely necessary
2446 * but we may as well do it while we are here.
2447 */
2448 TransactionIdAbortTree(xid, nchildren, children);
2449
2450 END_CRIT_SECTION();
2451
2452 /*
2453 * Wait for synchronous replication, if required.
2454 *
2455 * Note that at this stage we have marked clog, but still show as running
2456 * in the procarray and continue to hold locks.
2457 */
2458 SyncRepWaitForLSN(recptr, false);
2459 }
2460
2461 /*
2462 * PrepareRedoAdd
2463 *
2464 * Store pointers to the start/end of the WAL record along with the xid in
2465 * a gxact entry in shared memory TwoPhaseState structure. If caller
2466 * specifies InvalidXLogRecPtr as WAL location to fetch the two-phase
2467 * data, the entry is marked as located on disk.
2468 */
2469 void
2470 PrepareRedoAdd(char *buf, XLogRecPtr start_lsn,
2471 XLogRecPtr end_lsn, RepOriginId origin_id)
2472 {
2473 TwoPhaseFileHeader *hdr = (TwoPhaseFileHeader *) buf;
2474 char *bufptr;
2475 const char *gid;
2476 GlobalTransaction gxact;
2477
2478 Assert(LWLockHeldByMeInMode(TwoPhaseStateLock, LW_EXCLUSIVE));
2479 Assert(RecoveryInProgress());
2480
2481 bufptr = buf + MAXALIGN(sizeof(TwoPhaseFileHeader));
2482 gid = (const char *) bufptr;
2483
2484 /*
2485 * Reserve the GID for the given transaction in the redo code path.
2486 *
2487 * This creates a gxact struct and puts it into the active array.
2488 *
2489 * In redo, this struct is mainly used to track PREPARE/COMMIT entries in
2490 * shared memory. Hence, we only fill up the bare minimum contents here.
2491 * The gxact also gets marked with gxact->inredo set to true to indicate
2492 * that it got added in the redo phase
2493 */
2494
2495 /*
2496 * In the event of a crash while a checkpoint was running, it may be
2497 * possible that some two-phase data found its way to disk while its
2498 * corresponding record needs to be replayed in the follow-up recovery. As
2499 * the 2PC data was on disk, it has already been restored at the beginning
2500 * of recovery with restoreTwoPhaseData(), so skip this record to avoid
2501 * duplicates in TwoPhaseState. If a consistent state has been reached,
2502 * the record is added to TwoPhaseState and it should have no
2503 * corresponding file in pg_twophase.
2504 */
2505 if (!XLogRecPtrIsInvalid(start_lsn))
2506 {
2507 char path[MAXPGPATH];
2508
2509 TwoPhaseFilePath(path, hdr->xid);
2510
2511 if (access(path, F_OK) == 0)
2512 {
2513 ereport(reachedConsistency ? ERROR : WARNING,
2514 (errmsg("could not recover two-phase state file for transaction %u",
2515 hdr->xid),
2516 errdetail("Two-phase state file has been found in WAL record %X/%X, but this transaction has already been restored from disk.",
2517 LSN_FORMAT_ARGS(start_lsn))));
2518 return;
2519 }
2520
2521 if (errno != ENOENT)
2522 ereport(ERROR,
2523 (errcode_for_file_access(),
2524 errmsg("could not access file \"%s\": %m", path)));
2525 }
2526
2527 /* Get a free gxact from the freelist */
2528 if (TwoPhaseState->freeGXacts == NULL)
2529 ereport(ERROR,
2530 (errcode(ERRCODE_OUT_OF_MEMORY),
2531 errmsg("maximum number of prepared transactions reached"),
2532 errhint("Increase \"max_prepared_transactions\" (currently %d).",
2533 max_prepared_xacts)));
2534 gxact = TwoPhaseState->freeGXacts;
2535 TwoPhaseState->freeGXacts = gxact->next;
2536
2537 gxact->prepared_at = hdr->prepared_at;
2538 gxact->prepare_start_lsn = start_lsn;
2539 gxact->prepare_end_lsn = end_lsn;
2540 gxact->xid = hdr->xid;
2541 gxact->owner = hdr->owner;
2542 gxact->locking_backend = INVALID_PROC_NUMBER;
2543 gxact->valid = false;
2544 gxact->ondisk = XLogRecPtrIsInvalid(start_lsn);
2545 gxact->inredo = true; /* yes, added in redo */
2546 strcpy(gxact->gid, gid);
2547
2548 /* And insert it into the active array */
2549 Assert(TwoPhaseState->numPrepXacts < max_prepared_xacts);
2550 TwoPhaseState->prepXacts[TwoPhaseState->numPrepXacts++] = gxact;
2551
2552 if (origin_id != InvalidRepOriginId)
2553 {
2554 /* recover apply progress */
2555 replorigin_advance(origin_id, hdr->origin_lsn, end_lsn,
2556 false /* backward */ , false /* WAL */ );
2557 }
2558
2559 elog(DEBUG2, "added 2PC data in shared memory for transaction %u", gxact->xid);
2560 }
2561
2562 /*
2563 * PrepareRedoRemove
2564 *
2565 * Remove the corresponding gxact entry from TwoPhaseState. Also remove
2566 * the 2PC file if a prepared transaction was saved via an earlier checkpoint.
2567 *
2568 * Caller must hold TwoPhaseStateLock in exclusive mode, because TwoPhaseState
2569 * is updated.
2570 */
2571 void
2572 PrepareRedoRemove(TransactionId xid, bool giveWarning)
2573 {
2574 GlobalTransaction gxact = NULL;
2575 int i;
2576 bool found = false;
2577
2578 Assert(LWLockHeldByMeInMode(TwoPhaseStateLock, LW_EXCLUSIVE));
2579 Assert(RecoveryInProgress());
2580
2581 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
2582 {
2583 gxact = TwoPhaseState->prepXacts[i];
2584
2585 if (gxact->xid == xid)
2586 {
2587 Assert(gxact->inredo);
2588 found = true;
2589 break;
2590 }
2591 }
2592
2593 /*
2594 * Just leave if there is nothing, this is expected during WAL replay.
2595 */
2596 if (!found)
2597 return;
2598
2599 /*
2600 * And now we can clean up any files we may have left.
2601 */
2602 elog(DEBUG2, "removing 2PC data for transaction %u", xid);
2603 if (gxact->ondisk)
2604 RemoveTwoPhaseFile(xid, giveWarning);
2605 RemoveGXact(gxact);
2606 }
2607
2608 /*
2609 * LookupGXact
2610 * Check if the prepared transaction with the given GID, lsn and timestamp
2611 * exists.
2612 *
2613 * Note that we always compare with the LSN where prepare ends because that is
2614 * what is stored as origin_lsn in the 2PC file.
2615 *
2616 * This function is primarily used to check if the prepared transaction
2617 * received from the upstream (remote node) already exists. Checking only GID
2618 * is not sufficient because a different prepared xact with the same GID can
2619 * exist on the same node. So, we are ensuring to match origin_lsn and
2620 * origin_timestamp of prepared xact to avoid the possibility of a match of
2621 * prepared xact from two different nodes.
2622 */
2623 bool
2624 LookupGXact(const char *gid, XLogRecPtr prepare_end_lsn,
2625 TimestampTz origin_prepare_timestamp)
2626 {
2627 int i;
2628 bool found = false;
2629
2630 LWLockAcquire(TwoPhaseStateLock, LW_SHARED);
2631 for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
2632 {
2633 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
2634
2635 /* Ignore not-yet-valid GIDs. */
2636 if (gxact->valid && strcmp(gxact->gid, gid) == 0)
2637 {
2638 char *buf;
2639 TwoPhaseFileHeader *hdr;
2640
2641 /*
2642 * We are not expecting collisions of GXACTs (same gid) between
2643 * publisher and subscribers, so we perform all I/O while holding
2644 * TwoPhaseStateLock for simplicity.
2645 *
2646 * To move the I/O out of the lock, we need to ensure that no
2647 * other backend commits the prepared xact in the meantime. We can
2648 * do this optimization if we encounter many collisions in GID
2649 * between publisher and subscriber.
2650 */
2651 if (gxact->ondisk)
2652 buf = ReadTwoPhaseFile(gxact->xid, false);
2653 else
2654 {
2655 Assert(gxact->prepare_start_lsn);
2656 XlogReadTwoPhaseData(gxact->prepare_start_lsn, &buf, NULL);
2657 }
2658
2659 hdr = (TwoPhaseFileHeader *) buf;
2660
2661 if (hdr->origin_lsn == prepare_end_lsn &&
2662 hdr->origin_timestamp == origin_prepare_timestamp)
2663 {
2664 found = true;
2665 pfree(buf);
2666 break;
2667 }
2668
2669 pfree(buf);
2670 }
2671 }
2672 LWLockRelease(TwoPhaseStateLock);
2673 return found;
2674 }
2675
2676 /*
2677 * TwoPhaseTransactionGid
2678 * Form the prepared transaction GID for two_phase transactions.
2679 *
2680 * Return the GID in the supplied buffer.
2681 */
2682 void
2683 TwoPhaseTransactionGid(Oid subid, TransactionId xid, char *gid_res, int szgid)
2684 {
2685 Assert(OidIsValid(subid));
2686
2687 if (!TransactionIdIsValid(xid))
2688 ereport(ERROR,
2689 (errcode(ERRCODE_PROTOCOL_VIOLATION),
2690 errmsg_internal("invalid two-phase transaction ID")));
2691
2692 snprintf(gid_res, szgid, "pg_gid_%u_%u", subid, xid);
2693 }
2694
2695 /*
2696 * IsTwoPhaseTransactionGidForSubid
2697 * Check whether the given GID (as formed by TwoPhaseTransactionGid) is
2698 * for the specified 'subid'.
2699 */
2700 static bool
2701 IsTwoPhaseTransactionGidForSubid(Oid subid, char *gid)
2702 {
2703 int ret;
2704 Oid subid_from_gid;
2705 TransactionId xid_from_gid;
2706 char gid_tmp[GIDSIZE];
2707
2708 /* Extract the subid and xid from the given GID */
2709 ret = sscanf(gid, "pg_gid_%u_%u", &subid_from_gid, &xid_from_gid);
2710
2711 /*
2712 * Check that the given GID has expected format, and at least the subid
2713 * matches.
2714 */
2715 if (ret != 2 || subid != subid_from_gid)
2716 return false;
2717
2718 /*
2719 * Reconstruct a temporary GID based on the subid and xid extracted from
2720 * the given GID and check whether the temporary GID and the given GID
2721 * match.
2722 */
2723 TwoPhaseTransactionGid(subid, xid_from_gid, gid_tmp, sizeof(gid_tmp));
2724
2725 return strcmp(gid, gid_tmp) == 0;
2726 }
2727
2728 /*
2729 * LookupGXactBySubid
2730 * Check if the prepared transaction done by apply worker exists.
2731 */
2732 bool
2733 LookupGXactBySubid(Oid subid)
2734 {
2735 bool found = false;
2736
2737 LWLockAcquire(TwoPhaseStateLock, LW_SHARED);
2738 for (int i = 0; i < TwoPhaseState->numPrepXacts; i++)
2739 {
2740 GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
2741
2742 /* Ignore not-yet-valid GIDs. */
2743 if (gxact->valid &&
2744 IsTwoPhaseTransactionGidForSubid(subid, gxact->gid))
2745 {
2746 found = true;
2747 break;
2748 }
2749 }
2750 LWLockRelease(TwoPhaseStateLock);
2751
2752 return found;
2753 }