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