PostgreSQL Source Code  git master
snapbuild.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <unistd.h>
#include "access/heapam_xlog.h"
#include "access/transam.h"
#include "access/xact.h"
#include "common/file_utils.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/logical.h"
#include "replication/reorderbuffer.h"
#include "replication/snapbuild.h"
#include "storage/fd.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/standby.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
#include "utils/snapshot.h"
Include dependency graph for snapbuild.c:

Go to the source code of this file.

Data Structures

struct  SnapBuild
 
struct  SnapBuildOnDisk
 

Macros

#define SnapBuildOnDiskConstantSize    offsetof(SnapBuildOnDisk, builder)
 
#define SnapBuildOnDiskNotChecksummedSize    offsetof(SnapBuildOnDisk, version)
 
#define SNAPBUILD_MAGIC   0x51A1E001
 
#define SNAPBUILD_VERSION   6
 

Typedefs

typedef struct SnapBuildOnDisk SnapBuildOnDisk
 

Functions

static void SnapBuildPurgeOlderTxn (SnapBuild *builder)
 
static Snapshot SnapBuildBuildSnapshot (SnapBuild *builder)
 
static void SnapBuildFreeSnapshot (Snapshot snap)
 
static void SnapBuildSnapIncRefcount (Snapshot snap)
 
static void SnapBuildDistributeNewCatalogSnapshot (SnapBuild *builder, XLogRecPtr lsn)
 
static bool SnapBuildXidHasCatalogChanges (SnapBuild *builder, TransactionId xid, uint32 xinfo)
 
static bool SnapBuildFindSnapshot (SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
 
static void SnapBuildWaitSnapshot (xl_running_xacts *running, TransactionId cutoff)
 
static void SnapBuildSerialize (SnapBuild *builder, XLogRecPtr lsn)
 
static bool SnapBuildRestore (SnapBuild *builder, XLogRecPtr lsn)
 
static void SnapBuildRestoreContents (int fd, char *dest, Size size, const char *path)
 
SnapBuildAllocateSnapshotBuilder (ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, bool in_slot_creation, XLogRecPtr two_phase_at)
 
void FreeSnapshotBuilder (SnapBuild *builder)
 
SnapBuildState SnapBuildCurrentState (SnapBuild *builder)
 
XLogRecPtr SnapBuildGetTwoPhaseAt (SnapBuild *builder)
 
void SnapBuildSetTwoPhaseAt (SnapBuild *builder, XLogRecPtr ptr)
 
bool SnapBuildXactNeedsSkip (SnapBuild *builder, XLogRecPtr ptr)
 
void SnapBuildSnapDecRefcount (Snapshot snap)
 
Snapshot SnapBuildInitialSnapshot (SnapBuild *builder)
 
const char * SnapBuildExportSnapshot (SnapBuild *builder)
 
Snapshot SnapBuildGetOrBuildSnapshot (SnapBuild *builder)
 
void SnapBuildClearExportedSnapshot (void)
 
void SnapBuildResetExportedSnapshotState (void)
 
bool SnapBuildProcessChange (SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
 
void SnapBuildProcessNewCid (SnapBuild *builder, TransactionId xid, XLogRecPtr lsn, xl_heap_new_cid *xlrec)
 
static void SnapBuildAddCommittedTxn (SnapBuild *builder, TransactionId xid)
 
void SnapBuildCommitTxn (SnapBuild *builder, XLogRecPtr lsn, TransactionId xid, int nsubxacts, TransactionId *subxacts, uint32 xinfo)
 
void SnapBuildProcessRunningXacts (SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
 
void SnapBuildSerializationPoint (SnapBuild *builder, XLogRecPtr lsn)
 
void CheckPointSnapBuild (void)
 
bool SnapBuildSnapshotExists (XLogRecPtr lsn)
 

Variables

static ResourceOwner SavedResourceOwnerDuringExport = NULL
 
static bool ExportInProgress = false
 

Macro Definition Documentation

◆ SNAPBUILD_MAGIC

#define SNAPBUILD_MAGIC   0x51A1E001

Definition at line 1599 of file snapbuild.c.

◆ SNAPBUILD_VERSION

#define SNAPBUILD_VERSION   6

Definition at line 1600 of file snapbuild.c.

◆ SnapBuildOnDiskConstantSize

#define SnapBuildOnDiskConstantSize    offsetof(SnapBuildOnDisk, builder)

Definition at line 1594 of file snapbuild.c.

◆ SnapBuildOnDiskNotChecksummedSize

#define SnapBuildOnDiskNotChecksummedSize    offsetof(SnapBuildOnDisk, version)

Definition at line 1596 of file snapbuild.c.

Typedef Documentation

◆ SnapBuildOnDisk

Function Documentation

◆ AllocateSnapshotBuilder()

SnapBuild* AllocateSnapshotBuilder ( ReorderBuffer reorder,
TransactionId  xmin_horizon,
XLogRecPtr  start_lsn,
bool  need_full_snapshot,
bool  in_slot_creation,
XLogRecPtr  two_phase_at 
)

Definition at line 324 of file snapbuild.c.

330 {
332  MemoryContext oldcontext;
333  SnapBuild *builder;
334 
335  /* allocate memory in own context, to have better accountability */
337  "snapshot builder context",
339  oldcontext = MemoryContextSwitchTo(context);
340 
341  builder = palloc0(sizeof(SnapBuild));
342 
343  builder->state = SNAPBUILD_START;
344  builder->context = context;
345  builder->reorder = reorder;
346  /* Other struct members initialized by zeroing via palloc0 above */
347 
348  builder->committed.xcnt = 0;
349  builder->committed.xcnt_space = 128; /* arbitrary number */
350  builder->committed.xip =
351  palloc0(builder->committed.xcnt_space * sizeof(TransactionId));
352  builder->committed.includes_all_transactions = true;
353 
354  builder->catchange.xcnt = 0;
355  builder->catchange.xip = NULL;
356 
357  builder->initial_xmin_horizon = xmin_horizon;
358  builder->start_decoding_at = start_lsn;
359  builder->in_slot_creation = in_slot_creation;
360  builder->building_full_snapshot = need_full_snapshot;
361  builder->two_phase_at = two_phase_at;
362 
363  MemoryContextSwitchTo(oldcontext);
364 
365  return builder;
366 }
uint32 TransactionId
Definition: c.h:652
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
tree context
Definition: radixtree.h:1835
MemoryContextSwitchTo(old_ctx)
@ SNAPBUILD_START
Definition: snapbuild.h:23
struct SnapBuild::@20 catchange
XLogRecPtr start_decoding_at
Definition: snapbuild.c:170
SnapBuildState state
Definition: snapbuild.c:155
TransactionId initial_xmin_horizon
Definition: snapbuild.c:187
TransactionId * xip
Definition: snapbuild.c:255
XLogRecPtr two_phase_at
Definition: snapbuild.c:181
bool building_full_snapshot
Definition: snapbuild.c:190
size_t xcnt
Definition: snapbuild.c:229
bool in_slot_creation
Definition: snapbuild.c:198
size_t xcnt_space
Definition: snapbuild.c:232
bool includes_all_transactions
Definition: snapbuild.c:239
MemoryContext context
Definition: snapbuild.c:158
ReorderBuffer * reorder
Definition: snapbuild.c:213
struct SnapBuild::@19 committed

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, SnapBuild::building_full_snapshot, SnapBuild::catchange, SnapBuild::committed, SnapBuild::context, context, CurrentMemoryContext, SnapBuild::in_slot_creation, SnapBuild::includes_all_transactions, SnapBuild::initial_xmin_horizon, MemoryContextSwitchTo(), palloc0(), SnapBuild::reorder, SNAPBUILD_START, SnapBuild::start_decoding_at, SnapBuild::state, SnapBuild::two_phase_at, SnapBuild::xcnt, SnapBuild::xcnt_space, and SnapBuild::xip.

Referenced by StartupDecodingContext().

◆ CheckPointSnapBuild()

void CheckPointSnapBuild ( void  )

Definition at line 2071 of file snapbuild.c.

2072 {
2073  XLogRecPtr cutoff;
2074  XLogRecPtr redo;
2075  DIR *snap_dir;
2076  struct dirent *snap_de;
2077  char path[MAXPGPATH + 21];
2078 
2079  /*
2080  * We start off with a minimum of the last redo pointer. No new
2081  * replication slot will start before that, so that's a safe upper bound
2082  * for removal.
2083  */
2084  redo = GetRedoRecPtr();
2085 
2086  /* now check for the restart ptrs from existing slots */
2088 
2089  /* don't start earlier than the restart lsn */
2090  if (redo < cutoff)
2091  cutoff = redo;
2092 
2093  snap_dir = AllocateDir("pg_logical/snapshots");
2094  while ((snap_de = ReadDir(snap_dir, "pg_logical/snapshots")) != NULL)
2095  {
2096  uint32 hi;
2097  uint32 lo;
2098  XLogRecPtr lsn;
2099  PGFileType de_type;
2100 
2101  if (strcmp(snap_de->d_name, ".") == 0 ||
2102  strcmp(snap_de->d_name, "..") == 0)
2103  continue;
2104 
2105  snprintf(path, sizeof(path), "pg_logical/snapshots/%s", snap_de->d_name);
2106  de_type = get_dirent_type(path, snap_de, false, DEBUG1);
2107 
2108  if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_REG)
2109  {
2110  elog(DEBUG1, "only regular files expected: %s", path);
2111  continue;
2112  }
2113 
2114  /*
2115  * temporary filenames from SnapBuildSerialize() include the LSN and
2116  * everything but are postfixed by .$pid.tmp. We can just remove them
2117  * the same as other files because there can be none that are
2118  * currently being written that are older than cutoff.
2119  *
2120  * We just log a message if a file doesn't fit the pattern, it's
2121  * probably some editors lock/state file or similar...
2122  */
2123  if (sscanf(snap_de->d_name, "%X-%X.snap", &hi, &lo) != 2)
2124  {
2125  ereport(LOG,
2126  (errmsg("could not parse file name \"%s\"", path)));
2127  continue;
2128  }
2129 
2130  lsn = ((uint64) hi) << 32 | lo;
2131 
2132  /* check whether we still need it */
2133  if (lsn < cutoff || cutoff == InvalidXLogRecPtr)
2134  {
2135  elog(DEBUG1, "removing snapbuild snapshot %s", path);
2136 
2137  /*
2138  * It's not particularly harmful, though strange, if we can't
2139  * remove the file here. Don't prevent the checkpoint from
2140  * completing, that'd be a cure worse than the disease.
2141  */
2142  if (unlink(path) < 0)
2143  {
2144  ereport(LOG,
2146  errmsg("could not remove file \"%s\": %m",
2147  path)));
2148  continue;
2149  }
2150  }
2151  }
2152  FreeDir(snap_dir);
2153 }
unsigned int uint32
Definition: c.h:506
int errcode_for_file_access(void)
Definition: elog.c:876
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2909
int FreeDir(DIR *dir)
Definition: fd.c:2961
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2843
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition: file_utils.c:525
PGFileType
Definition: file_utils.h:19
@ PGFILETYPE_REG
Definition: file_utils.h:22
@ PGFILETYPE_ERROR
Definition: file_utils.h:20
#define MAXPGPATH
#define snprintf
Definition: port.h:238
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:1175
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6393
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References AllocateDir(), dirent::d_name, DEBUG1, elog, ereport, errcode_for_file_access(), errmsg(), FreeDir(), get_dirent_type(), GetRedoRecPtr(), InvalidXLogRecPtr, LOG, MAXPGPATH, PGFILETYPE_ERROR, PGFILETYPE_REG, ReadDir(), ReplicationSlotsComputeLogicalRestartLSN(), and snprintf.

Referenced by CheckPointGuts().

◆ FreeSnapshotBuilder()

void FreeSnapshotBuilder ( SnapBuild builder)

Definition at line 372 of file snapbuild.c.

373 {
374  MemoryContext context = builder->context;
375 
376  /* free snapshot explicitly, that contains some error checking */
377  if (builder->snapshot != NULL)
378  {
380  builder->snapshot = NULL;
381  }
382 
383  /* other resources are deallocated via memory context reset */
385 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
void SnapBuildSnapDecRefcount(Snapshot snap)
Definition: snapbuild.c:467
Snapshot snapshot
Definition: snapbuild.c:203

References SnapBuild::context, context, MemoryContextDelete(), SnapBuildSnapDecRefcount(), and SnapBuild::snapshot.

Referenced by FreeDecodingContext().

◆ SnapBuildAddCommittedTxn()

static void SnapBuildAddCommittedTxn ( SnapBuild builder,
TransactionId  xid 
)
static

Definition at line 923 of file snapbuild.c.

924 {
926 
927  if (builder->committed.xcnt == builder->committed.xcnt_space)
928  {
929  builder->committed.xcnt_space = builder->committed.xcnt_space * 2 + 1;
930 
931  elog(DEBUG1, "increasing space for committed transactions to %u",
932  (uint32) builder->committed.xcnt_space);
933 
934  builder->committed.xip = repalloc(builder->committed.xip,
935  builder->committed.xcnt_space * sizeof(TransactionId));
936  }
937 
938  /*
939  * TODO: It might make sense to keep the array sorted here instead of
940  * doing it every time we build a new snapshot. On the other hand this
941  * gets called repeatedly when a transaction with subtransactions commits.
942  */
943  builder->committed.xip[builder->committed.xcnt++] = xid;
944 }
#define Assert(condition)
Definition: c.h:858
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1541
#define TransactionIdIsValid(xid)
Definition: transam.h:41

References Assert, SnapBuild::committed, DEBUG1, elog, repalloc(), TransactionIdIsValid, SnapBuild::xcnt, SnapBuild::xcnt_space, and SnapBuild::xip.

Referenced by SnapBuildCommitTxn().

◆ SnapBuildBuildSnapshot()

static Snapshot SnapBuildBuildSnapshot ( SnapBuild builder)
static

Definition at line 499 of file snapbuild.c.

500 {
501  Snapshot snapshot;
502  Size ssize;
503 
504  Assert(builder->state >= SNAPBUILD_FULL_SNAPSHOT);
505 
506  ssize = sizeof(SnapshotData)
507  + sizeof(TransactionId) * builder->committed.xcnt
508  + sizeof(TransactionId) * 1 /* toplevel xid */ ;
509 
510  snapshot = MemoryContextAllocZero(builder->context, ssize);
511 
513 
514  /*
515  * We misuse the original meaning of SnapshotData's xip and subxip fields
516  * to make the more fitting for our needs.
517  *
518  * In the 'xip' array we store transactions that have to be treated as
519  * committed. Since we will only ever look at tuples from transactions
520  * that have modified the catalog it's more efficient to store those few
521  * that exist between xmin and xmax (frequently there are none).
522  *
523  * Snapshots that are used in transactions that have modified the catalog
524  * also use the 'subxip' array to store their toplevel xid and all the
525  * subtransaction xids so we can recognize when we need to treat rows as
526  * visible that are not in xip but still need to be visible. Subxip only
527  * gets filled when the transaction is copied into the context of a
528  * catalog modifying transaction since we otherwise share a snapshot
529  * between transactions. As long as a txn hasn't modified the catalog it
530  * doesn't need to treat any uncommitted rows as visible, so there is no
531  * need for those xids.
532  *
533  * Both arrays are qsort'ed so that we can use bsearch() on them.
534  */
535  Assert(TransactionIdIsNormal(builder->xmin));
536  Assert(TransactionIdIsNormal(builder->xmax));
537 
538  snapshot->xmin = builder->xmin;
539  snapshot->xmax = builder->xmax;
540 
541  /* store all transactions to be treated as committed by this snapshot */
542  snapshot->xip =
543  (TransactionId *) ((char *) snapshot + sizeof(SnapshotData));
544  snapshot->xcnt = builder->committed.xcnt;
545  memcpy(snapshot->xip,
546  builder->committed.xip,
547  builder->committed.xcnt * sizeof(TransactionId));
548 
549  /* sort so we can bsearch() */
550  qsort(snapshot->xip, snapshot->xcnt, sizeof(TransactionId), xidComparator);
551 
552  /*
553  * Initially, subxip is empty, i.e. it's a snapshot to be used by
554  * transactions that don't modify the catalog. Will be filled by
555  * ReorderBufferCopySnap() if necessary.
556  */
557  snapshot->subxcnt = 0;
558  snapshot->subxip = NULL;
559 
560  snapshot->suboverflowed = false;
561  snapshot->takenDuringRecovery = false;
562  snapshot->copied = false;
563  snapshot->curcid = FirstCommandId;
564  snapshot->active_count = 0;
565  snapshot->regd_count = 0;
566  snapshot->snapXactCompletionCount = 0;
567 
568  return snapshot;
569 }
#define FirstCommandId
Definition: c.h:668
size_t Size
Definition: c.h:605
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1215
#define qsort(a, b, c, d)
Definition: port.h:453
@ SNAPBUILD_FULL_SNAPSHOT
Definition: snapbuild.h:39
struct SnapshotData SnapshotData
@ SNAPSHOT_HISTORIC_MVCC
Definition: snapshot.h:109
TransactionId xmin
Definition: snapbuild.c:161
TransactionId xmax
Definition: snapbuild.c:164
TransactionId xmin
Definition: snapshot.h:157
int32 subxcnt
Definition: snapshot.h:181
bool copied
Definition: snapshot.h:185
uint32 regd_count
Definition: snapshot.h:205
uint32 active_count
Definition: snapshot.h:204
CommandId curcid
Definition: snapshot.h:187
uint32 xcnt
Definition: snapshot.h:169
TransactionId * subxip
Definition: snapshot.h:180
uint64 snapXactCompletionCount
Definition: snapshot.h:216
TransactionId xmax
Definition: snapshot.h:158
SnapshotType snapshot_type
Definition: snapshot.h:144
TransactionId * xip
Definition: snapshot.h:168
bool suboverflowed
Definition: snapshot.h:182
bool takenDuringRecovery
Definition: snapshot.h:184
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:139

References SnapshotData::active_count, Assert, SnapBuild::committed, SnapBuild::context, SnapshotData::copied, SnapshotData::curcid, FirstCommandId, MemoryContextAllocZero(), qsort, SnapshotData::regd_count, SNAPBUILD_FULL_SNAPSHOT, SNAPSHOT_HISTORIC_MVCC, SnapshotData::snapshot_type, SnapshotData::snapXactCompletionCount, SnapBuild::state, SnapshotData::suboverflowed, SnapshotData::subxcnt, SnapshotData::subxip, SnapshotData::takenDuringRecovery, TransactionIdIsNormal, SnapBuild::xcnt, SnapshotData::xcnt, xidComparator(), SnapBuild::xip, SnapshotData::xip, SnapBuild::xmax, SnapshotData::xmax, SnapBuild::xmin, and SnapshotData::xmin.

Referenced by SnapBuildCommitTxn(), SnapBuildGetOrBuildSnapshot(), SnapBuildInitialSnapshot(), SnapBuildProcessChange(), and SnapBuildRestore().

◆ SnapBuildClearExportedSnapshot()

void SnapBuildClearExportedSnapshot ( void  )

Definition at line 739 of file snapbuild.c.

740 {
741  ResourceOwner tmpResOwner;
742 
743  /* nothing exported, that is the usual case */
744  if (!ExportInProgress)
745  return;
746 
747  if (!IsTransactionState())
748  elog(ERROR, "clearing exported snapshot in wrong transaction state");
749 
750  /*
751  * AbortCurrentTransaction() takes care of resetting the snapshot state,
752  * so remember SavedResourceOwnerDuringExport.
753  */
754  tmpResOwner = SavedResourceOwnerDuringExport;
755 
756  /* make sure nothing could have ever happened */
758 
759  CurrentResourceOwner = tmpResOwner;
760 }
#define ERROR
Definition: elog.h:39
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
static ResourceOwner SavedResourceOwnerDuringExport
Definition: snapbuild.c:290
static bool ExportInProgress
Definition: snapbuild.c:291
bool IsTransactionState(void)
Definition: xact.c:385
void AbortCurrentTransaction(void)
Definition: xact.c:3425

References AbortCurrentTransaction(), CurrentResourceOwner, elog, ERROR, ExportInProgress, IsTransactionState(), and SavedResourceOwnerDuringExport.

Referenced by exec_replication_command().

◆ SnapBuildCommitTxn()

void SnapBuildCommitTxn ( SnapBuild builder,
XLogRecPtr  lsn,
TransactionId  xid,
int  nsubxacts,
TransactionId subxacts,
uint32  xinfo 
)

Definition at line 1034 of file snapbuild.c.

1036 {
1037  int nxact;
1038 
1039  bool needs_snapshot = false;
1040  bool needs_timetravel = false;
1041  bool sub_needs_timetravel = false;
1042 
1043  TransactionId xmax = xid;
1044 
1045  /*
1046  * Transactions preceding BUILDING_SNAPSHOT will neither be decoded, nor
1047  * will they be part of a snapshot. So we don't need to record anything.
1048  */
1049  if (builder->state == SNAPBUILD_START ||
1050  (builder->state == SNAPBUILD_BUILDING_SNAPSHOT &&
1051  TransactionIdPrecedes(xid, builder->next_phase_at)))
1052  {
1053  /* ensure that only commits after this are getting replayed */
1054  if (builder->start_decoding_at <= lsn)
1055  builder->start_decoding_at = lsn + 1;
1056  return;
1057  }
1058 
1059  if (builder->state < SNAPBUILD_CONSISTENT)
1060  {
1061  /* ensure that only commits after this are getting replayed */
1062  if (builder->start_decoding_at <= lsn)
1063  builder->start_decoding_at = lsn + 1;
1064 
1065  /*
1066  * If building an exportable snapshot, force xid to be tracked, even
1067  * if the transaction didn't modify the catalog.
1068  */
1069  if (builder->building_full_snapshot)
1070  {
1071  needs_timetravel = true;
1072  }
1073  }
1074 
1075  for (nxact = 0; nxact < nsubxacts; nxact++)
1076  {
1077  TransactionId subxid = subxacts[nxact];
1078 
1079  /*
1080  * Add subtransaction to base snapshot if catalog modifying, we don't
1081  * distinguish to toplevel transactions there.
1082  */
1083  if (SnapBuildXidHasCatalogChanges(builder, subxid, xinfo))
1084  {
1085  sub_needs_timetravel = true;
1086  needs_snapshot = true;
1087 
1088  elog(DEBUG1, "found subtransaction %u:%u with catalog changes",
1089  xid, subxid);
1090 
1091  SnapBuildAddCommittedTxn(builder, subxid);
1092 
1093  if (NormalTransactionIdFollows(subxid, xmax))
1094  xmax = subxid;
1095  }
1096 
1097  /*
1098  * If we're forcing timetravel we also need visibility information
1099  * about subtransaction, so keep track of subtransaction's state, even
1100  * if not catalog modifying. Don't need to distribute a snapshot in
1101  * that case.
1102  */
1103  else if (needs_timetravel)
1104  {
1105  SnapBuildAddCommittedTxn(builder, subxid);
1106  if (NormalTransactionIdFollows(subxid, xmax))
1107  xmax = subxid;
1108  }
1109  }
1110 
1111  /* if top-level modified catalog, it'll need a snapshot */
1112  if (SnapBuildXidHasCatalogChanges(builder, xid, xinfo))
1113  {
1114  elog(DEBUG2, "found top level transaction %u, with catalog changes",
1115  xid);
1116  needs_snapshot = true;
1117  needs_timetravel = true;
1118  SnapBuildAddCommittedTxn(builder, xid);
1119  }
1120  else if (sub_needs_timetravel)
1121  {
1122  /* track toplevel txn as well, subxact alone isn't meaningful */
1123  elog(DEBUG2, "forced transaction %u to do timetravel due to one of its subtransactions",
1124  xid);
1125  needs_timetravel = true;
1126  SnapBuildAddCommittedTxn(builder, xid);
1127  }
1128  else if (needs_timetravel)
1129  {
1130  elog(DEBUG2, "forced transaction %u to do timetravel", xid);
1131 
1132  SnapBuildAddCommittedTxn(builder, xid);
1133  }
1134 
1135  if (!needs_timetravel)
1136  {
1137  /* record that we cannot export a general snapshot anymore */
1138  builder->committed.includes_all_transactions = false;
1139  }
1140 
1141  Assert(!needs_snapshot || needs_timetravel);
1142 
1143  /*
1144  * Adjust xmax of the snapshot builder, we only do that for committed,
1145  * catalog modifying, transactions, everything else isn't interesting for
1146  * us since we'll never look at the respective rows.
1147  */
1148  if (needs_timetravel &&
1149  (!TransactionIdIsValid(builder->xmax) ||
1150  TransactionIdFollowsOrEquals(xmax, builder->xmax)))
1151  {
1152  builder->xmax = xmax;
1153  TransactionIdAdvance(builder->xmax);
1154  }
1155 
1156  /* if there's any reason to build a historic snapshot, do so now */
1157  if (needs_snapshot)
1158  {
1159  /*
1160  * If we haven't built a complete snapshot yet there's no need to hand
1161  * it out, it wouldn't (and couldn't) be used anyway.
1162  */
1163  if (builder->state < SNAPBUILD_FULL_SNAPSHOT)
1164  return;
1165 
1166  /*
1167  * Decrease the snapshot builder's refcount of the old snapshot, note
1168  * that it still will be used if it has been handed out to the
1169  * reorderbuffer earlier.
1170  */
1171  if (builder->snapshot)
1173 
1174  builder->snapshot = SnapBuildBuildSnapshot(builder);
1175 
1176  /* we might need to execute invalidations, add snapshot */
1177  if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, xid))
1178  {
1180  ReorderBufferSetBaseSnapshot(builder->reorder, xid, lsn,
1181  builder->snapshot);
1182  }
1183 
1184  /* refcount of the snapshot builder for the new snapshot */
1186 
1187  /* add a new catalog snapshot to all currently running transactions */
1189  }
1190 }
#define DEBUG2
Definition: elog.h:29
void ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Snapshot snap)
bool ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
static void SnapBuildSnapIncRefcount(Snapshot snap)
Definition: snapbuild.c:455
static void SnapBuildAddCommittedTxn(SnapBuild *builder, TransactionId xid)
Definition: snapbuild.c:923
static bool SnapBuildXidHasCatalogChanges(SnapBuild *builder, TransactionId xid, uint32 xinfo)
Definition: snapbuild.c:1197
static Snapshot SnapBuildBuildSnapshot(SnapBuild *builder)
Definition: snapbuild.c:499
static void SnapBuildDistributeNewCatalogSnapshot(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:870
@ SNAPBUILD_BUILDING_SNAPSHOT
Definition: snapbuild.h:29
@ SNAPBUILD_CONSISTENT
Definition: snapbuild.h:46
TransactionId next_phase_at
Definition: snapbuild.c:220
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:329
#define NormalTransactionIdFollows(id1, id2)
Definition: transam.h:152
#define TransactionIdAdvance(dest)
Definition: transam.h:91

References Assert, SnapBuild::building_full_snapshot, SnapBuild::committed, DEBUG1, DEBUG2, elog, SnapBuild::includes_all_transactions, SnapBuild::next_phase_at, NormalTransactionIdFollows, SnapBuild::reorder, ReorderBufferSetBaseSnapshot(), ReorderBufferXidHasBaseSnapshot(), SNAPBUILD_BUILDING_SNAPSHOT, SNAPBUILD_CONSISTENT, SNAPBUILD_FULL_SNAPSHOT, SNAPBUILD_START, SnapBuildAddCommittedTxn(), SnapBuildBuildSnapshot(), SnapBuildDistributeNewCatalogSnapshot(), SnapBuildSnapDecRefcount(), SnapBuildSnapIncRefcount(), SnapBuildXidHasCatalogChanges(), SnapBuild::snapshot, SnapBuild::start_decoding_at, SnapBuild::state, TransactionIdAdvance, TransactionIdFollowsOrEquals(), TransactionIdIsValid, TransactionIdPrecedes(), and SnapBuild::xmax.

Referenced by DecodeCommit().

◆ SnapBuildCurrentState()

SnapBuildState SnapBuildCurrentState ( SnapBuild builder)

Definition at line 416 of file snapbuild.c.

417 {
418  return builder->state;
419 }

References SnapBuild::state.

Referenced by DecodePrepare(), DecodingContextReady(), heap2_decode(), heap_decode(), logicalmsg_decode(), ReorderBufferCanStartStreaming(), and xact_decode().

◆ SnapBuildDistributeNewCatalogSnapshot()

static void SnapBuildDistributeNewCatalogSnapshot ( SnapBuild builder,
XLogRecPtr  lsn 
)
static

Definition at line 870 of file snapbuild.c.

871 {
872  dlist_iter txn_i;
873  ReorderBufferTXN *txn;
874 
875  /*
876  * Iterate through all toplevel transactions. This can include
877  * subtransactions which we just don't yet know to be that, but that's
878  * fine, they will just get an unnecessary snapshot queued.
879  */
880  dlist_foreach(txn_i, &builder->reorder->toplevel_by_lsn)
881  {
882  txn = dlist_container(ReorderBufferTXN, node, txn_i.cur);
883 
885 
886  /*
887  * If we don't have a base snapshot yet, there are no changes in this
888  * transaction which in turn implies we don't yet need a snapshot at
889  * all. We'll add a snapshot when the first change gets queued.
890  *
891  * NB: This works correctly even for subtransactions because
892  * ReorderBufferAssignChild() takes care to transfer the base snapshot
893  * to the top-level transaction, and while iterating the changequeue
894  * we'll get the change from the subtxn.
895  */
896  if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, txn->xid))
897  continue;
898 
899  /*
900  * We don't need to add snapshot to prepared transactions as they
901  * should not see the new catalog contents.
902  */
903  if (rbtxn_prepared(txn) || rbtxn_skip_prepared(txn))
904  continue;
905 
906  elog(DEBUG2, "adding a new snapshot to %u at %X/%X",
907  txn->xid, LSN_FORMAT_ARGS(lsn));
908 
909  /*
910  * increase the snapshot's refcount for the transaction we are handing
911  * it out to
912  */
914  ReorderBufferAddSnapshot(builder->reorder, txn->xid, lsn,
915  builder->snapshot);
916  }
917 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:623
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
void ReorderBufferAddSnapshot(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Snapshot snap)
#define rbtxn_prepared(txn)
#define rbtxn_skip_prepared(txn)
TransactionId xid
dlist_head toplevel_by_lsn
dlist_node * cur
Definition: ilist.h:179
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43

References Assert, dlist_iter::cur, DEBUG2, dlist_container, dlist_foreach, elog, LSN_FORMAT_ARGS, rbtxn_prepared, rbtxn_skip_prepared, SnapBuild::reorder, ReorderBufferAddSnapshot(), ReorderBufferXidHasBaseSnapshot(), SnapBuildSnapIncRefcount(), SnapBuild::snapshot, ReorderBuffer::toplevel_by_lsn, TransactionIdIsValid, and ReorderBufferTXN::xid.

Referenced by SnapBuildCommitTxn().

◆ SnapBuildExportSnapshot()

const char* SnapBuildExportSnapshot ( SnapBuild builder)

Definition at line 678 of file snapbuild.c.

679 {
680  Snapshot snap;
681  char *snapname;
682 
684  elog(ERROR, "cannot export a snapshot from within a transaction");
685 
687  elog(ERROR, "can only export one snapshot at a time");
688 
690  ExportInProgress = true;
691 
693 
694  /* There doesn't seem to a nice API to set these */
696  XactReadOnly = true;
697 
698  snap = SnapBuildInitialSnapshot(builder);
699 
700  /*
701  * now that we've built a plain snapshot, make it active and use the
702  * normal mechanisms for exporting it
703  */
704  snapname = ExportSnapshot(snap);
705 
706  ereport(LOG,
707  (errmsg_plural("exported logical decoding snapshot: \"%s\" with %u transaction ID",
708  "exported logical decoding snapshot: \"%s\" with %u transaction IDs",
709  snap->xcnt,
710  snapname, snap->xcnt)));
711  return snapname;
712 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1180
Snapshot SnapBuildInitialSnapshot(SnapBuild *builder)
Definition: snapbuild.c:579
char * ExportSnapshot(Snapshot snapshot)
Definition: snapmgr.c:1095
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4976
bool XactReadOnly
Definition: xact.c:80
void StartTransactionCommand(void)
Definition: xact.c:3033
int XactIsoLevel
Definition: xact.c:77
#define XACT_REPEATABLE_READ
Definition: xact.h:38

References CurrentResourceOwner, elog, ereport, errmsg_plural(), ERROR, ExportInProgress, ExportSnapshot(), IsTransactionOrTransactionBlock(), LOG, SavedResourceOwnerDuringExport, SnapBuildInitialSnapshot(), StartTransactionCommand(), XACT_REPEATABLE_READ, XactIsoLevel, XactReadOnly, and SnapshotData::xcnt.

Referenced by CreateReplicationSlot().

◆ SnapBuildFindSnapshot()

static bool SnapBuildFindSnapshot ( SnapBuild builder,
XLogRecPtr  lsn,
xl_running_xacts running 
)
static

Definition at line 1329 of file snapbuild.c.

1330 {
1331  /* ---
1332  * Build catalog decoding snapshot incrementally using information about
1333  * the currently running transactions. There are several ways to do that:
1334  *
1335  * a) There were no running transactions when the xl_running_xacts record
1336  * was inserted, jump to CONSISTENT immediately. We might find such a
1337  * state while waiting on c)'s sub-states.
1338  *
1339  * b) This (in a previous run) or another decoding slot serialized a
1340  * snapshot to disk that we can use. Can't use this method while finding
1341  * the start point for decoding changes as the restart LSN would be an
1342  * arbitrary LSN but we need to find the start point to extract changes
1343  * where we won't see the data for partial transactions. Also, we cannot
1344  * use this method when a slot needs a full snapshot for export or direct
1345  * use, as that snapshot will only contain catalog modifying transactions.
1346  *
1347  * c) First incrementally build a snapshot for catalog tuples
1348  * (BUILDING_SNAPSHOT), that requires all, already in-progress,
1349  * transactions to finish. Every transaction starting after that
1350  * (FULL_SNAPSHOT state), has enough information to be decoded. But
1351  * for older running transactions no viable snapshot exists yet, so
1352  * CONSISTENT will only be reached once all of those have finished.
1353  * ---
1354  */
1355 
1356  /*
1357  * xl_running_xacts record is older than what we can use, we might not
1358  * have all necessary catalog rows anymore.
1359  */
1362  builder->initial_xmin_horizon))
1363  {
1364  ereport(DEBUG1,
1365  (errmsg_internal("skipping snapshot at %X/%X while building logical decoding snapshot, xmin horizon too low",
1366  LSN_FORMAT_ARGS(lsn)),
1367  errdetail_internal("initial xmin horizon of %u vs the snapshot's %u",
1368  builder->initial_xmin_horizon, running->oldestRunningXid)));
1369 
1370 
1371  SnapBuildWaitSnapshot(running, builder->initial_xmin_horizon);
1372 
1373  return true;
1374  }
1375 
1376  /*
1377  * a) No transaction were running, we can jump to consistent.
1378  *
1379  * This is not affected by races around xl_running_xacts, because we can
1380  * miss transaction commits, but currently not transactions starting.
1381  *
1382  * NB: We might have already started to incrementally assemble a snapshot,
1383  * so we need to be careful to deal with that.
1384  */
1385  if (running->oldestRunningXid == running->nextXid)
1386  {
1387  if (builder->start_decoding_at == InvalidXLogRecPtr ||
1388  builder->start_decoding_at <= lsn)
1389  /* can decode everything after this */
1390  builder->start_decoding_at = lsn + 1;
1391 
1392  /* As no transactions were running xmin/xmax can be trivially set. */
1393  builder->xmin = running->nextXid; /* < are finished */
1394  builder->xmax = running->nextXid; /* >= are running */
1395 
1396  /* so we can safely use the faster comparisons */
1397  Assert(TransactionIdIsNormal(builder->xmin));
1398  Assert(TransactionIdIsNormal(builder->xmax));
1399 
1400  builder->state = SNAPBUILD_CONSISTENT;
1402 
1403  ereport(LOG,
1404  (errmsg("logical decoding found consistent point at %X/%X",
1405  LSN_FORMAT_ARGS(lsn)),
1406  errdetail("There are no running transactions.")));
1407 
1408  return false;
1409  }
1410 
1411  /*
1412  * b) valid on disk state and while neither building full snapshot nor
1413  * creating a slot.
1414  */
1415  else if (!builder->building_full_snapshot &&
1416  !builder->in_slot_creation &&
1417  SnapBuildRestore(builder, lsn))
1418  {
1419  /* there won't be any state to cleanup */
1420  return false;
1421  }
1422 
1423  /*
1424  * c) transition from START to BUILDING_SNAPSHOT.
1425  *
1426  * In START state, and a xl_running_xacts record with running xacts is
1427  * encountered. In that case, switch to BUILDING_SNAPSHOT state, and
1428  * record xl_running_xacts->nextXid. Once all running xacts have finished
1429  * (i.e. they're all >= nextXid), we have a complete catalog snapshot. It
1430  * might look that we could use xl_running_xacts's ->xids information to
1431  * get there quicker, but that is problematic because transactions marked
1432  * as running, might already have inserted their commit record - it's
1433  * infeasible to change that with locking.
1434  */
1435  else if (builder->state == SNAPBUILD_START)
1436  {
1438  builder->next_phase_at = running->nextXid;
1439 
1440  /*
1441  * Start with an xmin/xmax that's correct for future, when all the
1442  * currently running transactions have finished. We'll update both
1443  * while waiting for the pending transactions to finish.
1444  */
1445  builder->xmin = running->nextXid; /* < are finished */
1446  builder->xmax = running->nextXid; /* >= are running */
1447 
1448  /* so we can safely use the faster comparisons */
1449  Assert(TransactionIdIsNormal(builder->xmin));
1450  Assert(TransactionIdIsNormal(builder->xmax));
1451 
1452  ereport(LOG,
1453  (errmsg("logical decoding found initial starting point at %X/%X",
1454  LSN_FORMAT_ARGS(lsn)),
1455  errdetail("Waiting for transactions (approximately %d) older than %u to end.",
1456  running->xcnt, running->nextXid)));
1457 
1458  SnapBuildWaitSnapshot(running, running->nextXid);
1459  }
1460 
1461  /*
1462  * c) transition from BUILDING_SNAPSHOT to FULL_SNAPSHOT.
1463  *
1464  * In BUILDING_SNAPSHOT state, and this xl_running_xacts' oldestRunningXid
1465  * is >= than nextXid from when we switched to BUILDING_SNAPSHOT. This
1466  * means all transactions starting afterwards have enough information to
1467  * be decoded. Switch to FULL_SNAPSHOT.
1468  */
1469  else if (builder->state == SNAPBUILD_BUILDING_SNAPSHOT &&
1471  running->oldestRunningXid))
1472  {
1473  builder->state = SNAPBUILD_FULL_SNAPSHOT;
1474  builder->next_phase_at = running->nextXid;
1475 
1476  ereport(LOG,
1477  (errmsg("logical decoding found initial consistent point at %X/%X",
1478  LSN_FORMAT_ARGS(lsn)),
1479  errdetail("Waiting for transactions (approximately %d) older than %u to end.",
1480  running->xcnt, running->nextXid)));
1481 
1482  SnapBuildWaitSnapshot(running, running->nextXid);
1483  }
1484 
1485  /*
1486  * c) transition from FULL_SNAPSHOT to CONSISTENT.
1487  *
1488  * In FULL_SNAPSHOT state, and this xl_running_xacts' oldestRunningXid is
1489  * >= than nextXid from when we switched to FULL_SNAPSHOT. This means all
1490  * transactions that are currently in progress have a catalog snapshot,
1491  * and all their changes have been collected. Switch to CONSISTENT.
1492  */
1493  else if (builder->state == SNAPBUILD_FULL_SNAPSHOT &&
1495  running->oldestRunningXid))
1496  {
1497  builder->state = SNAPBUILD_CONSISTENT;
1499 
1500  ereport(LOG,
1501  (errmsg("logical decoding found consistent point at %X/%X",
1502  LSN_FORMAT_ARGS(lsn)),
1503  errdetail("There are no old transactions anymore.")));
1504  }
1505 
1506  /*
1507  * We already started to track running xacts and need to wait for all
1508  * in-progress ones to finish. We fall through to the normal processing of
1509  * records so incremental cleanup can be performed.
1510  */
1511  return true;
1512 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1230
int errdetail(const char *fmt,...)
Definition: elog.c:1203
static void SnapBuildWaitSnapshot(xl_running_xacts *running, TransactionId cutoff)
Definition: snapbuild.c:1526
static bool SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:1862
TransactionId oldestRunningXid
Definition: standbydefs.h:53
TransactionId nextXid
Definition: standbydefs.h:52
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
#define InvalidTransactionId
Definition: transam.h:31
#define NormalTransactionIdPrecedes(id1, id2)
Definition: transam.h:147

References Assert, SnapBuild::building_full_snapshot, DEBUG1, ereport, errdetail(), errdetail_internal(), errmsg(), errmsg_internal(), SnapBuild::in_slot_creation, SnapBuild::initial_xmin_horizon, InvalidTransactionId, InvalidXLogRecPtr, LOG, LSN_FORMAT_ARGS, SnapBuild::next_phase_at, xl_running_xacts::nextXid, NormalTransactionIdPrecedes, xl_running_xacts::oldestRunningXid, SNAPBUILD_BUILDING_SNAPSHOT, SNAPBUILD_CONSISTENT, SNAPBUILD_FULL_SNAPSHOT, SNAPBUILD_START, SnapBuildRestore(), SnapBuildWaitSnapshot(), SnapBuild::start_decoding_at, SnapBuild::state, TransactionIdIsNormal, TransactionIdPrecedesOrEquals(), xl_running_xacts::xcnt, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by SnapBuildProcessRunningXacts().

◆ SnapBuildFreeSnapshot()

static void SnapBuildFreeSnapshot ( Snapshot  snap)
static

Definition at line 391 of file snapbuild.c.

392 {
393  /* make sure we don't get passed an external snapshot */
395 
396  /* make sure nobody modified our snapshot */
397  Assert(snap->curcid == FirstCommandId);
398  Assert(!snap->suboverflowed);
399  Assert(!snap->takenDuringRecovery);
400  Assert(snap->regd_count == 0);
401 
402  /* slightly more likely, so it's checked even without c-asserts */
403  if (snap->copied)
404  elog(ERROR, "cannot free a copied snapshot");
405 
406  if (snap->active_count)
407  elog(ERROR, "cannot free an active snapshot");
408 
409  pfree(snap);
410 }
void pfree(void *pointer)
Definition: mcxt.c:1521

References SnapshotData::active_count, Assert, SnapshotData::copied, SnapshotData::curcid, elog, ERROR, FirstCommandId, pfree(), SnapshotData::regd_count, SNAPSHOT_HISTORIC_MVCC, SnapshotData::snapshot_type, SnapshotData::suboverflowed, and SnapshotData::takenDuringRecovery.

Referenced by SnapBuildSnapDecRefcount().

◆ SnapBuildGetOrBuildSnapshot()

Snapshot SnapBuildGetOrBuildSnapshot ( SnapBuild builder)

Definition at line 718 of file snapbuild.c.

719 {
720  Assert(builder->state == SNAPBUILD_CONSISTENT);
721 
722  /* only build a new snapshot if we don't have a prebuilt one */
723  if (builder->snapshot == NULL)
724  {
725  builder->snapshot = SnapBuildBuildSnapshot(builder);
726  /* increase refcount for the snapshot builder */
728  }
729 
730  return builder->snapshot;
731 }

References Assert, SNAPBUILD_CONSISTENT, SnapBuildBuildSnapshot(), SnapBuildSnapIncRefcount(), SnapBuild::snapshot, and SnapBuild::state.

Referenced by logicalmsg_decode().

◆ SnapBuildGetTwoPhaseAt()

XLogRecPtr SnapBuildGetTwoPhaseAt ( SnapBuild builder)

Definition at line 425 of file snapbuild.c.

426 {
427  return builder->two_phase_at;
428 }

References SnapBuild::two_phase_at.

Referenced by DecodeCommit().

◆ SnapBuildInitialSnapshot()

Snapshot SnapBuildInitialSnapshot ( SnapBuild builder)

Definition at line 579 of file snapbuild.c.

580 {
581  Snapshot snap;
582  TransactionId xid;
583  TransactionId safeXid;
584  TransactionId *newxip;
585  int newxcnt = 0;
586 
588  Assert(builder->building_full_snapshot);
589 
590  /* don't allow older snapshots */
591  InvalidateCatalogSnapshot(); /* about to overwrite MyProc->xmin */
593  elog(ERROR, "cannot build an initial slot snapshot when snapshots exist");
595 
596  if (builder->state != SNAPBUILD_CONSISTENT)
597  elog(ERROR, "cannot build an initial slot snapshot before reaching a consistent state");
598 
599  if (!builder->committed.includes_all_transactions)
600  elog(ERROR, "cannot build an initial slot snapshot, not all transactions are monitored anymore");
601 
602  /* so we don't overwrite the existing value */
604  elog(ERROR, "cannot build an initial slot snapshot when MyProc->xmin already is valid");
605 
606  snap = SnapBuildBuildSnapshot(builder);
607 
608  /*
609  * We know that snap->xmin is alive, enforced by the logical xmin
610  * mechanism. Due to that we can do this without locks, we're only
611  * changing our own value.
612  *
613  * Building an initial snapshot is expensive and an unenforced xmin
614  * horizon would have bad consequences, therefore always double-check that
615  * the horizon is enforced.
616  */
617  LWLockAcquire(ProcArrayLock, LW_SHARED);
618  safeXid = GetOldestSafeDecodingTransactionId(false);
619  LWLockRelease(ProcArrayLock);
620 
621  if (TransactionIdFollows(safeXid, snap->xmin))
622  elog(ERROR, "cannot build an initial slot snapshot as oldest safe xid %u follows snapshot's xmin %u",
623  safeXid, snap->xmin);
624 
625  MyProc->xmin = snap->xmin;
626 
627  /* allocate in transaction context */
628  newxip = (TransactionId *)
630 
631  /*
632  * snapbuild.c builds transactions in an "inverted" manner, which means it
633  * stores committed transactions in ->xip, not ones in progress. Build a
634  * classical snapshot by marking all non-committed transactions as
635  * in-progress. This can be expensive.
636  */
637  for (xid = snap->xmin; NormalTransactionIdPrecedes(xid, snap->xmax);)
638  {
639  void *test;
640 
641  /*
642  * Check whether transaction committed using the decoding snapshot
643  * meaning of ->xip.
644  */
645  test = bsearch(&xid, snap->xip, snap->xcnt,
646  sizeof(TransactionId), xidComparator);
647 
648  if (test == NULL)
649  {
650  if (newxcnt >= GetMaxSnapshotXidCount())
651  ereport(ERROR,
653  errmsg("initial slot snapshot too large")));
654 
655  newxip[newxcnt++] = xid;
656  }
657 
659  }
660 
661  /* adjust remaining snapshot fields as needed */
663  snap->xcnt = newxcnt;
664  snap->xip = newxip;
665 
666  return snap;
667 }
int errcode(int sqlerrcode)
Definition: elog.c:853
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_SHARED
Definition: lwlock.h:115
void * palloc(Size size)
Definition: mcxt.c:1317
#define ERRCODE_T_R_SERIALIZATION_FAILURE
Definition: pgbench.c:76
static void test(void)
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2949
int GetMaxSnapshotXidCount(void)
Definition: procarray.c:2069
bool HistoricSnapshotActive(void)
Definition: snapmgr.c:1672
bool HaveRegisteredOrActiveSnapshot(void)
Definition: snapmgr.c:1624
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:422
@ SNAPSHOT_MVCC
Definition: snapshot.h:50
PGPROC * MyProc
Definition: proc.c:66
TransactionId xmin
Definition: proc.h:172
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:314

References Assert, SnapBuild::building_full_snapshot, SnapBuild::committed, elog, ereport, errcode(), ERRCODE_T_R_SERIALIZATION_FAILURE, errmsg(), ERROR, GetMaxSnapshotXidCount(), GetOldestSafeDecodingTransactionId(), HaveRegisteredOrActiveSnapshot(), HistoricSnapshotActive(), SnapBuild::includes_all_transactions, InvalidateCatalogSnapshot(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MyProc, NormalTransactionIdPrecedes, palloc(), SNAPBUILD_CONSISTENT, SnapBuildBuildSnapshot(), SNAPSHOT_MVCC, SnapshotData::snapshot_type, SnapBuild::state, test(), TransactionIdAdvance, TransactionIdFollows(), TransactionIdIsValid, XACT_REPEATABLE_READ, XactIsoLevel, SnapshotData::xcnt, xidComparator(), SnapshotData::xip, SnapshotData::xmax, PGPROC::xmin, and SnapshotData::xmin.

Referenced by CreateReplicationSlot(), and SnapBuildExportSnapshot().

◆ SnapBuildProcessChange()

bool SnapBuildProcessChange ( SnapBuild builder,
TransactionId  xid,
XLogRecPtr  lsn 
)

Definition at line 778 of file snapbuild.c.

779 {
780  /*
781  * We can't handle data in transactions if we haven't built a snapshot
782  * yet, so don't store them.
783  */
784  if (builder->state < SNAPBUILD_FULL_SNAPSHOT)
785  return false;
786 
787  /*
788  * No point in keeping track of changes in transactions that we don't have
789  * enough information about to decode. This means that they started before
790  * we got into the SNAPBUILD_FULL_SNAPSHOT state.
791  */
792  if (builder->state < SNAPBUILD_CONSISTENT &&
793  TransactionIdPrecedes(xid, builder->next_phase_at))
794  return false;
795 
796  /*
797  * If the reorderbuffer doesn't yet have a snapshot, add one now, it will
798  * be needed to decode the change we're currently processing.
799  */
800  if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, xid))
801  {
802  /* only build a new snapshot if we don't have a prebuilt one */
803  if (builder->snapshot == NULL)
804  {
805  builder->snapshot = SnapBuildBuildSnapshot(builder);
806  /* increase refcount for the snapshot builder */
808  }
809 
810  /*
811  * Increase refcount for the transaction we're handing the snapshot
812  * out to.
813  */
815  ReorderBufferSetBaseSnapshot(builder->reorder, xid, lsn,
816  builder->snapshot);
817  }
818 
819  return true;
820 }

References SnapBuild::next_phase_at, SnapBuild::reorder, ReorderBufferSetBaseSnapshot(), ReorderBufferXidHasBaseSnapshot(), SNAPBUILD_CONSISTENT, SNAPBUILD_FULL_SNAPSHOT, SnapBuildBuildSnapshot(), SnapBuildSnapIncRefcount(), SnapBuild::snapshot, SnapBuild::state, and TransactionIdPrecedes().

Referenced by heap2_decode(), heap_decode(), and logicalmsg_decode().

◆ SnapBuildProcessNewCid()

void SnapBuildProcessNewCid ( SnapBuild builder,
TransactionId  xid,
XLogRecPtr  lsn,
xl_heap_new_cid xlrec 
)

Definition at line 828 of file snapbuild.c.

830 {
831  CommandId cid;
832 
833  /*
834  * we only log new_cid's if a catalog tuple was modified, so mark the
835  * transaction as containing catalog modifications
836  */
837  ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
838 
839  ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn,
840  xlrec->target_locator, xlrec->target_tid,
841  xlrec->cmin, xlrec->cmax,
842  xlrec->combocid);
843 
844  /* figure out new command id */
845  if (xlrec->cmin != InvalidCommandId &&
846  xlrec->cmax != InvalidCommandId)
847  cid = Max(xlrec->cmin, xlrec->cmax);
848  else if (xlrec->cmax != InvalidCommandId)
849  cid = xlrec->cmax;
850  else if (xlrec->cmin != InvalidCommandId)
851  cid = xlrec->cmin;
852  else
853  {
854  cid = InvalidCommandId; /* silence compiler */
855  elog(ERROR, "xl_heap_new_cid record without a valid CommandId");
856  }
857 
858  ReorderBufferAddNewCommandId(builder->reorder, xid, lsn, cid + 1);
859 }
#define InvalidCommandId
Definition: c.h:669
#define Max(x, y)
Definition: c.h:998
uint32 CommandId
Definition: c.h:666
void ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
void ReorderBufferAddNewCommandId(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, CommandId cid)
void ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, RelFileLocator locator, ItemPointerData tid, CommandId cmin, CommandId cmax, CommandId combocid)
CommandId cmin
Definition: heapam_xlog.h:453
CommandId combocid
Definition: heapam_xlog.h:455
ItemPointerData target_tid
Definition: heapam_xlog.h:461
TransactionId top_xid
Definition: heapam_xlog.h:452
CommandId cmax
Definition: heapam_xlog.h:454
RelFileLocator target_locator
Definition: heapam_xlog.h:460

References xl_heap_new_cid::cmax, xl_heap_new_cid::cmin, xl_heap_new_cid::combocid, elog, ERROR, InvalidCommandId, Max, SnapBuild::reorder, ReorderBufferAddNewCommandId(), ReorderBufferAddNewTupleCids(), ReorderBufferXidSetCatalogChanges(), xl_heap_new_cid::target_locator, xl_heap_new_cid::target_tid, and xl_heap_new_cid::top_xid.

Referenced by heap2_decode().

◆ SnapBuildProcessRunningXacts()

void SnapBuildProcessRunningXacts ( SnapBuild builder,
XLogRecPtr  lsn,
xl_running_xacts running 
)

Definition at line 1227 of file snapbuild.c.

1228 {
1229  ReorderBufferTXN *txn;
1230  TransactionId xmin;
1231 
1232  /*
1233  * If we're not consistent yet, inspect the record to see whether it
1234  * allows to get closer to being consistent. If we are consistent, dump
1235  * our snapshot so others or we, after a restart, can use it.
1236  */
1237  if (builder->state < SNAPBUILD_CONSISTENT)
1238  {
1239  /* returns false if there's no point in performing cleanup just yet */
1240  if (!SnapBuildFindSnapshot(builder, lsn, running))
1241  return;
1242  }
1243  else
1244  SnapBuildSerialize(builder, lsn);
1245 
1246  /*
1247  * Update range of interesting xids based on the running xacts
1248  * information. We don't increase ->xmax using it, because once we are in
1249  * a consistent state we can do that ourselves and much more efficiently
1250  * so, because we only need to do it for catalog transactions since we
1251  * only ever look at those.
1252  *
1253  * NB: We only increase xmax when a catalog modifying transaction commits
1254  * (see SnapBuildCommitTxn). Because of this, xmax can be lower than
1255  * xmin, which looks odd but is correct and actually more efficient, since
1256  * we hit fast paths in heapam_visibility.c.
1257  */
1258  builder->xmin = running->oldestRunningXid;
1259 
1260  /* Remove transactions we don't need to keep track off anymore */
1261  SnapBuildPurgeOlderTxn(builder);
1262 
1263  /*
1264  * Advance the xmin limit for the current replication slot, to allow
1265  * vacuum to clean up the tuples this slot has been protecting.
1266  *
1267  * The reorderbuffer might have an xmin among the currently running
1268  * snapshots; use it if so. If not, we need only consider the snapshots
1269  * we'll produce later, which can't be less than the oldest running xid in
1270  * the record we're reading now.
1271  */
1272  xmin = ReorderBufferGetOldestXmin(builder->reorder);
1273  if (xmin == InvalidTransactionId)
1274  xmin = running->oldestRunningXid;
1275  elog(DEBUG3, "xmin: %u, xmax: %u, oldest running: %u, oldest xmin: %u",
1276  builder->xmin, builder->xmax, running->oldestRunningXid, xmin);
1277  LogicalIncreaseXminForSlot(lsn, xmin);
1278 
1279  /*
1280  * Also tell the slot where we can restart decoding from. We don't want to
1281  * do that after every commit because changing that implies an fsync of
1282  * the logical slot's state file, so we only do it every time we see a
1283  * running xacts record.
1284  *
1285  * Do so by looking for the oldest in progress transaction (determined by
1286  * the first LSN of any of its relevant records). Every transaction
1287  * remembers the last location we stored the snapshot to disk before its
1288  * beginning. That point is where we can restart from.
1289  */
1290 
1291  /*
1292  * Can't know about a serialized snapshot's location if we're not
1293  * consistent.
1294  */
1295  if (builder->state < SNAPBUILD_CONSISTENT)
1296  return;
1297 
1298  txn = ReorderBufferGetOldestTXN(builder->reorder);
1299 
1300  /*
1301  * oldest ongoing txn might have started when we didn't yet serialize
1302  * anything because we hadn't reached a consistent state yet.
1303  */
1304  if (txn != NULL && txn->restart_decoding_lsn != InvalidXLogRecPtr)
1306 
1307  /*
1308  * No in-progress transaction, can reuse the last serialized snapshot if
1309  * we have one.
1310  */
1311  else if (txn == NULL &&
1315  builder->last_serialized_snapshot);
1316 }
#define DEBUG3
Definition: elog.h:28
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition: logical.c:1761
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition: logical.c:1693
TransactionId ReorderBufferGetOldestXmin(ReorderBuffer *rb)
ReorderBufferTXN * ReorderBufferGetOldestTXN(ReorderBuffer *rb)
static void SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:1622
static bool SnapBuildFindSnapshot(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
Definition: snapbuild.c:1329
static void SnapBuildPurgeOlderTxn(SnapBuild *builder)
Definition: snapbuild.c:957
XLogRecPtr restart_decoding_lsn
XLogRecPtr current_restart_decoding_lsn
XLogRecPtr last_serialized_snapshot
Definition: snapbuild.c:208

References ReorderBuffer::current_restart_decoding_lsn, DEBUG3, elog, InvalidTransactionId, InvalidXLogRecPtr, SnapBuild::last_serialized_snapshot, LogicalIncreaseRestartDecodingForSlot(), LogicalIncreaseXminForSlot(), xl_running_xacts::oldestRunningXid, SnapBuild::reorder, ReorderBufferGetOldestTXN(), ReorderBufferGetOldestXmin(), ReorderBufferTXN::restart_decoding_lsn, SNAPBUILD_CONSISTENT, SnapBuildFindSnapshot(), SnapBuildPurgeOlderTxn(), SnapBuildSerialize(), SnapBuild::state, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by standby_decode().

◆ SnapBuildPurgeOlderTxn()

static void SnapBuildPurgeOlderTxn ( SnapBuild builder)
static

Definition at line 957 of file snapbuild.c.

958 {
959  int off;
960  TransactionId *workspace;
961  int surviving_xids = 0;
962 
963  /* not ready yet */
964  if (!TransactionIdIsNormal(builder->xmin))
965  return;
966 
967  /* TODO: Neater algorithm than just copying and iterating? */
968  workspace =
969  MemoryContextAlloc(builder->context,
970  builder->committed.xcnt * sizeof(TransactionId));
971 
972  /* copy xids that still are interesting to workspace */
973  for (off = 0; off < builder->committed.xcnt; off++)
974  {
975  if (NormalTransactionIdPrecedes(builder->committed.xip[off],
976  builder->xmin))
977  ; /* remove */
978  else
979  workspace[surviving_xids++] = builder->committed.xip[off];
980  }
981 
982  /* copy workspace back to persistent state */
983  memcpy(builder->committed.xip, workspace,
984  surviving_xids * sizeof(TransactionId));
985 
986  elog(DEBUG3, "purged committed transactions from %u to %u, xmin: %u, xmax: %u",
987  (uint32) builder->committed.xcnt, (uint32) surviving_xids,
988  builder->xmin, builder->xmax);
989  builder->committed.xcnt = surviving_xids;
990 
991  pfree(workspace);
992 
993  /*
994  * Purge xids in ->catchange as well. The purged array must also be sorted
995  * in xidComparator order.
996  */
997  if (builder->catchange.xcnt > 0)
998  {
999  /*
1000  * Since catchange.xip is sorted, we find the lower bound of xids that
1001  * are still interesting.
1002  */
1003  for (off = 0; off < builder->catchange.xcnt; off++)
1004  {
1005  if (TransactionIdFollowsOrEquals(builder->catchange.xip[off],
1006  builder->xmin))
1007  break;
1008  }
1009 
1010  surviving_xids = builder->catchange.xcnt - off;
1011 
1012  if (surviving_xids > 0)
1013  {
1014  memmove(builder->catchange.xip, &(builder->catchange.xip[off]),
1015  surviving_xids * sizeof(TransactionId));
1016  }
1017  else
1018  {
1019  pfree(builder->catchange.xip);
1020  builder->catchange.xip = NULL;
1021  }
1022 
1023  elog(DEBUG3, "purged catalog modifying transactions from %u to %u, xmin: %u, xmax: %u",
1024  (uint32) builder->catchange.xcnt, (uint32) surviving_xids,
1025  builder->xmin, builder->xmax);
1026  builder->catchange.xcnt = surviving_xids;
1027  }
1028 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181

References SnapBuild::catchange, SnapBuild::committed, SnapBuild::context, DEBUG3, elog, MemoryContextAlloc(), NormalTransactionIdPrecedes, pfree(), TransactionIdFollowsOrEquals(), TransactionIdIsNormal, SnapBuild::xcnt, SnapBuild::xip, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by SnapBuildProcessRunningXacts().

◆ SnapBuildResetExportedSnapshotState()

void SnapBuildResetExportedSnapshotState ( void  )

Definition at line 766 of file snapbuild.c.

767 {
769  ExportInProgress = false;
770 }

References ExportInProgress, and SavedResourceOwnerDuringExport.

Referenced by AbortTransaction().

◆ SnapBuildRestore()

static bool SnapBuildRestore ( SnapBuild builder,
XLogRecPtr  lsn 
)
static

Definition at line 1862 of file snapbuild.c.

1863 {
1864  SnapBuildOnDisk ondisk;
1865  int fd;
1866  char path[MAXPGPATH];
1867  Size sz;
1868  pg_crc32c checksum;
1869 
1870  /* no point in loading a snapshot if we're already there */
1871  if (builder->state == SNAPBUILD_CONSISTENT)
1872  return false;
1873 
1874  sprintf(path, "pg_logical/snapshots/%X-%X.snap",
1875  LSN_FORMAT_ARGS(lsn));
1876 
1877  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
1878 
1879  if (fd < 0 && errno == ENOENT)
1880  return false;
1881  else if (fd < 0)
1882  ereport(ERROR,
1884  errmsg("could not open file \"%s\": %m", path)));
1885 
1886  /* ----
1887  * Make sure the snapshot had been stored safely to disk, that's normally
1888  * cheap.
1889  * Note that we do not need PANIC here, nobody will be able to use the
1890  * slot without fsyncing, and saving it won't succeed without an fsync()
1891  * either...
1892  * ----
1893  */
1894  fsync_fname(path, false);
1895  fsync_fname("pg_logical/snapshots", true);
1896 
1897 
1898  /* read statically sized portion of snapshot */
1899  SnapBuildRestoreContents(fd, (char *) &ondisk, SnapBuildOnDiskConstantSize, path);
1900 
1901  if (ondisk.magic != SNAPBUILD_MAGIC)
1902  ereport(ERROR,
1904  errmsg("snapbuild state file \"%s\" has wrong magic number: %u instead of %u",
1905  path, ondisk.magic, SNAPBUILD_MAGIC)));
1906 
1907  if (ondisk.version != SNAPBUILD_VERSION)
1908  ereport(ERROR,
1910  errmsg("snapbuild state file \"%s\" has unsupported version: %u instead of %u",
1911  path, ondisk.version, SNAPBUILD_VERSION)));
1912 
1913  INIT_CRC32C(checksum);
1914  COMP_CRC32C(checksum,
1915  ((char *) &ondisk) + SnapBuildOnDiskNotChecksummedSize,
1917 
1918  /* read SnapBuild */
1919  SnapBuildRestoreContents(fd, (char *) &ondisk.builder, sizeof(SnapBuild), path);
1920  COMP_CRC32C(checksum, &ondisk.builder, sizeof(SnapBuild));
1921 
1922  /* restore committed xacts information */
1923  if (ondisk.builder.committed.xcnt > 0)
1924  {
1925  sz = sizeof(TransactionId) * ondisk.builder.committed.xcnt;
1926  ondisk.builder.committed.xip = MemoryContextAllocZero(builder->context, sz);
1927  SnapBuildRestoreContents(fd, (char *) ondisk.builder.committed.xip, sz, path);
1928  COMP_CRC32C(checksum, ondisk.builder.committed.xip, sz);
1929  }
1930 
1931  /* restore catalog modifying xacts information */
1932  if (ondisk.builder.catchange.xcnt > 0)
1933  {
1934  sz = sizeof(TransactionId) * ondisk.builder.catchange.xcnt;
1935  ondisk.builder.catchange.xip = MemoryContextAllocZero(builder->context, sz);
1936  SnapBuildRestoreContents(fd, (char *) ondisk.builder.catchange.xip, sz, path);
1937  COMP_CRC32C(checksum, ondisk.builder.catchange.xip, sz);
1938  }
1939 
1940  if (CloseTransientFile(fd) != 0)
1941  ereport(ERROR,
1943  errmsg("could not close file \"%s\": %m", path)));
1944 
1945  FIN_CRC32C(checksum);
1946 
1947  /* verify checksum of what we've read */
1948  if (!EQ_CRC32C(checksum, ondisk.checksum))
1949  ereport(ERROR,
1951  errmsg("checksum mismatch for snapbuild state file \"%s\": is %u, should be %u",
1952  path, checksum, ondisk.checksum)));
1953 
1954  /*
1955  * ok, we now have a sensible snapshot here, figure out if it has more
1956  * information than we have.
1957  */
1958 
1959  /*
1960  * We are only interested in consistent snapshots for now, comparing
1961  * whether one incomplete snapshot is more "advanced" seems to be
1962  * unnecessarily complex.
1963  */
1964  if (ondisk.builder.state < SNAPBUILD_CONSISTENT)
1965  goto snapshot_not_interesting;
1966 
1967  /*
1968  * Don't use a snapshot that requires an xmin that we cannot guarantee to
1969  * be available.
1970  */
1972  goto snapshot_not_interesting;
1973 
1974  /*
1975  * Consistent snapshots have no next phase. Reset next_phase_at as it is
1976  * possible that an old value may remain.
1977  */
1980 
1981  /* ok, we think the snapshot is sensible, copy over everything important */
1982  builder->xmin = ondisk.builder.xmin;
1983  builder->xmax = ondisk.builder.xmax;
1984  builder->state = ondisk.builder.state;
1985 
1986  builder->committed.xcnt = ondisk.builder.committed.xcnt;
1987  /* We only allocated/stored xcnt, not xcnt_space xids ! */
1988  /* don't overwrite preallocated xip, if we don't have anything here */
1989  if (builder->committed.xcnt > 0)
1990  {
1991  pfree(builder->committed.xip);
1992  builder->committed.xcnt_space = ondisk.builder.committed.xcnt;
1993  builder->committed.xip = ondisk.builder.committed.xip;
1994  }
1995  ondisk.builder.committed.xip = NULL;
1996 
1997  /* set catalog modifying transactions */
1998  if (builder->catchange.xip)
1999  pfree(builder->catchange.xip);
2000  builder->catchange.xcnt = ondisk.builder.catchange.xcnt;
2001  builder->catchange.xip = ondisk.builder.catchange.xip;
2002  ondisk.builder.catchange.xip = NULL;
2003 
2004  /* our snapshot is not interesting anymore, build a new one */
2005  if (builder->snapshot != NULL)
2006  {
2008  }
2009  builder->snapshot = SnapBuildBuildSnapshot(builder);
2011 
2012  ReorderBufferSetRestartPoint(builder->reorder, lsn);
2013 
2014  Assert(builder->state == SNAPBUILD_CONSISTENT);
2015 
2016  ereport(LOG,
2017  (errmsg("logical decoding found consistent point at %X/%X",
2018  LSN_FORMAT_ARGS(lsn)),
2019  errdetail("Logical decoding will begin using saved snapshot.")));
2020  return true;
2021 
2022 snapshot_not_interesting:
2023  if (ondisk.builder.committed.xip != NULL)
2024  pfree(ondisk.builder.committed.xip);
2025  if (ondisk.builder.catchange.xip != NULL)
2026  pfree(ondisk.builder.catchange.xip);
2027  return false;
2028 }
#define PG_BINARY
Definition: c.h:1273
int CloseTransientFile(int fd)
Definition: fd.c:2809
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:756
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2633
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:98
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:103
#define sprintf
Definition: port.h:240
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void ReorderBufferSetRestartPoint(ReorderBuffer *rb, XLogRecPtr ptr)
#define SNAPBUILD_VERSION
Definition: snapbuild.c:1600
#define SnapBuildOnDiskNotChecksummedSize
Definition: snapbuild.c:1596
#define SNAPBUILD_MAGIC
Definition: snapbuild.c:1599
#define SnapBuildOnDiskConstantSize
Definition: snapbuild.c:1594
static void SnapBuildRestoreContents(int fd, char *dest, Size size, const char *path)
Definition: snapbuild.c:2034
SnapBuild builder
Definition: snapbuild.c:1589
pg_crc32c checksum
Definition: snapbuild.c:1579

References Assert, SnapBuildOnDisk::builder, SnapBuild::catchange, SnapBuildOnDisk::checksum, CloseTransientFile(), SnapBuild::committed, COMP_CRC32C, SnapBuild::context, EQ_CRC32C, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errdetail(), errmsg(), ERROR, fd(), FIN_CRC32C, fsync_fname(), INIT_CRC32C, SnapBuild::initial_xmin_horizon, InvalidTransactionId, LOG, LSN_FORMAT_ARGS, SnapBuildOnDisk::magic, MAXPGPATH, MemoryContextAllocZero(), SnapBuild::next_phase_at, OpenTransientFile(), pfree(), PG_BINARY, SnapBuild::reorder, ReorderBufferSetRestartPoint(), SNAPBUILD_CONSISTENT, SNAPBUILD_MAGIC, SNAPBUILD_VERSION, SnapBuildBuildSnapshot(), SnapBuildOnDiskConstantSize, SnapBuildOnDiskNotChecksummedSize, SnapBuildRestoreContents(), SnapBuildSnapDecRefcount(), SnapBuildSnapIncRefcount(), SnapBuild::snapshot, sprintf, SnapBuild::state, TransactionIdPrecedes(), SnapBuildOnDisk::version, SnapBuild::xcnt, SnapBuild::xcnt_space, SnapBuild::xip, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by SnapBuildFindSnapshot(), and SnapBuildSerializationPoint().

◆ SnapBuildRestoreContents()

static void SnapBuildRestoreContents ( int  fd,
char *  dest,
Size  size,
const char *  path 
)
static

Definition at line 2034 of file snapbuild.c.

2035 {
2036  int readBytes;
2037 
2038  pgstat_report_wait_start(WAIT_EVENT_SNAPBUILD_READ);
2039  readBytes = read(fd, dest, size);
2041  if (readBytes != size)
2042  {
2043  int save_errno = errno;
2044 
2046 
2047  if (readBytes < 0)
2048  {
2049  errno = save_errno;
2050  ereport(ERROR,
2052  errmsg("could not read file \"%s\": %m", path)));
2053  }
2054  else
2055  ereport(ERROR,
2057  errmsg("could not read file \"%s\": read %d of %zu",
2058  path, readBytes, size)));
2059  }
2060 }
#define read(a, b, c)
Definition: win32.h:13
static pg_noinline void Size size
Definition: slab.c:607
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101

References CloseTransientFile(), generate_unaccent_rules::dest, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errmsg(), ERROR, fd(), pgstat_report_wait_end(), pgstat_report_wait_start(), read, and size.

Referenced by SnapBuildRestore().

◆ SnapBuildSerializationPoint()

void SnapBuildSerializationPoint ( SnapBuild builder,
XLogRecPtr  lsn 
)

Definition at line 1609 of file snapbuild.c.

1610 {
1611  if (builder->state < SNAPBUILD_CONSISTENT)
1612  SnapBuildRestore(builder, lsn);
1613  else
1614  SnapBuildSerialize(builder, lsn);
1615 }

References SNAPBUILD_CONSISTENT, SnapBuildRestore(), SnapBuildSerialize(), and SnapBuild::state.

Referenced by xlog_decode().

◆ SnapBuildSerialize()

static void SnapBuildSerialize ( SnapBuild builder,
XLogRecPtr  lsn 
)
static

Definition at line 1622 of file snapbuild.c.

1623 {
1624  Size needed_length;
1625  SnapBuildOnDisk *ondisk = NULL;
1626  TransactionId *catchange_xip = NULL;
1628  size_t catchange_xcnt;
1629  char *ondisk_c;
1630  int fd;
1631  char tmppath[MAXPGPATH];
1632  char path[MAXPGPATH];
1633  int ret;
1634  struct stat stat_buf;
1635  Size sz;
1636 
1637  Assert(lsn != InvalidXLogRecPtr);
1639  builder->last_serialized_snapshot <= lsn);
1640 
1641  /*
1642  * no point in serializing if we cannot continue to work immediately after
1643  * restoring the snapshot
1644  */
1645  if (builder->state < SNAPBUILD_CONSISTENT)
1646  return;
1647 
1648  /* consistent snapshots have no next phase */
1650 
1651  /*
1652  * We identify snapshots by the LSN they are valid for. We don't need to
1653  * include timelines in the name as each LSN maps to exactly one timeline
1654  * unless the user used pg_resetwal or similar. If a user did so, there's
1655  * no hope continuing to decode anyway.
1656  */
1657  sprintf(path, "pg_logical/snapshots/%X-%X.snap",
1658  LSN_FORMAT_ARGS(lsn));
1659 
1660  /*
1661  * first check whether some other backend already has written the snapshot
1662  * for this LSN. It's perfectly fine if there's none, so we accept ENOENT
1663  * as a valid state. Everything else is an unexpected error.
1664  */
1665  ret = stat(path, &stat_buf);
1666 
1667  if (ret != 0 && errno != ENOENT)
1668  ereport(ERROR,
1670  errmsg("could not stat file \"%s\": %m", path)));
1671 
1672  else if (ret == 0)
1673  {
1674  /*
1675  * somebody else has already serialized to this point, don't overwrite
1676  * but remember location, so we don't need to read old data again.
1677  *
1678  * To be sure it has been synced to disk after the rename() from the
1679  * tempfile filename to the real filename, we just repeat the fsync.
1680  * That ought to be cheap because in most scenarios it should already
1681  * be safely on disk.
1682  */
1683  fsync_fname(path, false);
1684  fsync_fname("pg_logical/snapshots", true);
1685 
1686  builder->last_serialized_snapshot = lsn;
1687  goto out;
1688  }
1689 
1690  /*
1691  * there is an obvious race condition here between the time we stat(2) the
1692  * file and us writing the file. But we rename the file into place
1693  * atomically and all files created need to contain the same data anyway,
1694  * so this is perfectly fine, although a bit of a resource waste. Locking
1695  * seems like pointless complication.
1696  */
1697  elog(DEBUG1, "serializing snapshot to %s", path);
1698 
1699  /* to make sure only we will write to this tempfile, include pid */
1700  sprintf(tmppath, "pg_logical/snapshots/%X-%X.snap.%d.tmp",
1701  LSN_FORMAT_ARGS(lsn), MyProcPid);
1702 
1703  /*
1704  * Unlink temporary file if it already exists, needs to have been before a
1705  * crash/error since we won't enter this function twice from within a
1706  * single decoding slot/backend and the temporary file contains the pid of
1707  * the current process.
1708  */
1709  if (unlink(tmppath) != 0 && errno != ENOENT)
1710  ereport(ERROR,
1712  errmsg("could not remove file \"%s\": %m", tmppath)));
1713 
1715 
1716  /* Get the catalog modifying transactions that are yet not committed */
1717  catchange_xip = ReorderBufferGetCatalogChangesXacts(builder->reorder);
1718  catchange_xcnt = dclist_count(&builder->reorder->catchange_txns);
1719 
1720  needed_length = sizeof(SnapBuildOnDisk) +
1721  sizeof(TransactionId) * (builder->committed.xcnt + catchange_xcnt);
1722 
1723  ondisk_c = palloc0(needed_length);
1724  ondisk = (SnapBuildOnDisk *) ondisk_c;
1725  ondisk->magic = SNAPBUILD_MAGIC;
1726  ondisk->version = SNAPBUILD_VERSION;
1727  ondisk->length = needed_length;
1728  INIT_CRC32C(ondisk->checksum);
1729  COMP_CRC32C(ondisk->checksum,
1730  ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1732  ondisk_c += sizeof(SnapBuildOnDisk);
1733 
1734  memcpy(&ondisk->builder, builder, sizeof(SnapBuild));
1735  /* NULL-ify memory-only data */
1736  ondisk->builder.context = NULL;
1737  ondisk->builder.snapshot = NULL;
1738  ondisk->builder.reorder = NULL;
1739  ondisk->builder.committed.xip = NULL;
1740  ondisk->builder.catchange.xip = NULL;
1741  /* update catchange only on disk data */
1742  ondisk->builder.catchange.xcnt = catchange_xcnt;
1743 
1744  COMP_CRC32C(ondisk->checksum,
1745  &ondisk->builder,
1746  sizeof(SnapBuild));
1747 
1748  /* copy committed xacts */
1749  if (builder->committed.xcnt > 0)
1750  {
1751  sz = sizeof(TransactionId) * builder->committed.xcnt;
1752  memcpy(ondisk_c, builder->committed.xip, sz);
1753  COMP_CRC32C(ondisk->checksum, ondisk_c, sz);
1754  ondisk_c += sz;
1755  }
1756 
1757  /* copy catalog modifying xacts */
1758  if (catchange_xcnt > 0)
1759  {
1760  sz = sizeof(TransactionId) * catchange_xcnt;
1761  memcpy(ondisk_c, catchange_xip, sz);
1762  COMP_CRC32C(ondisk->checksum, ondisk_c, sz);
1763  ondisk_c += sz;
1764  }
1765 
1766  FIN_CRC32C(ondisk->checksum);
1767 
1768  /* we have valid data now, open tempfile and write it there */
1769  fd = OpenTransientFile(tmppath,
1770  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
1771  if (fd < 0)
1772  ereport(ERROR,
1774  errmsg("could not open file \"%s\": %m", tmppath)));
1775 
1776  errno = 0;
1777  pgstat_report_wait_start(WAIT_EVENT_SNAPBUILD_WRITE);
1778  if ((write(fd, ondisk, needed_length)) != needed_length)
1779  {
1780  int save_errno = errno;
1781 
1783 
1784  /* if write didn't set errno, assume problem is no disk space */
1785  errno = save_errno ? save_errno : ENOSPC;
1786  ereport(ERROR,
1788  errmsg("could not write to file \"%s\": %m", tmppath)));
1789  }
1791 
1792  /*
1793  * fsync the file before renaming so that even if we crash after this we
1794  * have either a fully valid file or nothing.
1795  *
1796  * It's safe to just ERROR on fsync() here because we'll retry the whole
1797  * operation including the writes.
1798  *
1799  * TODO: Do the fsync() via checkpoints/restartpoints, doing it here has
1800  * some noticeable overhead since it's performed synchronously during
1801  * decoding?
1802  */
1803  pgstat_report_wait_start(WAIT_EVENT_SNAPBUILD_SYNC);
1804  if (pg_fsync(fd) != 0)
1805  {
1806  int save_errno = errno;
1807 
1809  errno = save_errno;
1810  ereport(ERROR,
1812  errmsg("could not fsync file \"%s\": %m", tmppath)));
1813  }
1815 
1816  if (CloseTransientFile(fd) != 0)
1817  ereport(ERROR,
1819  errmsg("could not close file \"%s\": %m", tmppath)));
1820 
1821  fsync_fname("pg_logical/snapshots", true);
1822 
1823  /*
1824  * We may overwrite the work from some other backend, but that's ok, our
1825  * snapshot is valid as well, we'll just have done some superfluous work.
1826  */
1827  if (rename(tmppath, path) != 0)
1828  {
1829  ereport(ERROR,
1831  errmsg("could not rename file \"%s\" to \"%s\": %m",
1832  tmppath, path)));
1833  }
1834 
1835  /* make sure we persist */
1836  fsync_fname(path, false);
1837  fsync_fname("pg_logical/snapshots", true);
1838 
1839  /*
1840  * Now there's no way we can lose the dumped state anymore, remember this
1841  * as a serialization point.
1842  */
1843  builder->last_serialized_snapshot = lsn;
1844 
1846 
1847 out:
1849  builder->last_serialized_snapshot);
1850  /* be tidy */
1851  if (ondisk)
1852  pfree(ondisk);
1853  if (catchange_xip)
1854  pfree(catchange_xip);
1855 }
int pg_fsync(int fd)
Definition: fd.c:386
int MyProcPid
Definition: globals.c:46
static uint32 dclist_count(const dclist_head *head)
Definition: ilist.h:932
#define write(a, b, c)
Definition: win32.h:14
RT_SCOPE RT_RADIX_TREE *MemoryContext old_ctx
Definition: radixtree.h:1824
TransactionId * ReorderBufferGetCatalogChangesXacts(ReorderBuffer *rb)
struct SnapBuildOnDisk SnapBuildOnDisk
dclist_head catchange_txns
#define stat
Definition: win32_port.h:284

References Assert, SnapBuildOnDisk::builder, SnapBuild::catchange, ReorderBuffer::catchange_txns, SnapBuildOnDisk::checksum, CloseTransientFile(), SnapBuild::committed, COMP_CRC32C, SnapBuild::context, dclist_count(), DEBUG1, elog, ereport, errcode_for_file_access(), errmsg(), ERROR, fd(), FIN_CRC32C, fsync_fname(), INIT_CRC32C, InvalidTransactionId, InvalidXLogRecPtr, SnapBuild::last_serialized_snapshot, SnapBuildOnDisk::length, LSN_FORMAT_ARGS, SnapBuildOnDisk::magic, MAXPGPATH, MemoryContextSwitchTo(), MyProcPid, SnapBuild::next_phase_at, old_ctx, OpenTransientFile(), palloc0(), pfree(), PG_BINARY, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), SnapBuild::reorder, ReorderBufferGetCatalogChangesXacts(), ReorderBufferSetRestartPoint(), SNAPBUILD_CONSISTENT, SNAPBUILD_MAGIC, SNAPBUILD_VERSION, SnapBuildOnDiskConstantSize, SnapBuildOnDiskNotChecksummedSize, SnapBuild::snapshot, sprintf, stat, SnapBuild::state, SnapBuildOnDisk::version, write, SnapBuild::xcnt, and SnapBuild::xip.

Referenced by SnapBuildProcessRunningXacts(), and SnapBuildSerializationPoint().

◆ SnapBuildSetTwoPhaseAt()

void SnapBuildSetTwoPhaseAt ( SnapBuild builder,
XLogRecPtr  ptr 
)

Definition at line 434 of file snapbuild.c.

435 {
436  builder->two_phase_at = ptr;
437 }

References SnapBuild::two_phase_at.

Referenced by CreateDecodingContext().

◆ SnapBuildSnapDecRefcount()

void SnapBuildSnapDecRefcount ( Snapshot  snap)

Definition at line 467 of file snapbuild.c.

468 {
469  /* make sure we don't get passed an external snapshot */
471 
472  /* make sure nobody modified our snapshot */
473  Assert(snap->curcid == FirstCommandId);
474  Assert(!snap->suboverflowed);
475  Assert(!snap->takenDuringRecovery);
476 
477  Assert(snap->regd_count == 0);
478 
479  Assert(snap->active_count > 0);
480 
481  /* slightly more likely, so it's checked even without casserts */
482  if (snap->copied)
483  elog(ERROR, "cannot free a copied snapshot");
484 
485  snap->active_count--;
486  if (snap->active_count == 0)
487  SnapBuildFreeSnapshot(snap);
488 }
static void SnapBuildFreeSnapshot(Snapshot snap)
Definition: snapbuild.c:391

References SnapshotData::active_count, Assert, SnapshotData::copied, SnapshotData::curcid, elog, ERROR, FirstCommandId, SnapshotData::regd_count, SnapBuildFreeSnapshot(), SNAPSHOT_HISTORIC_MVCC, SnapshotData::snapshot_type, SnapshotData::suboverflowed, and SnapshotData::takenDuringRecovery.

Referenced by FreeSnapshotBuilder(), ReorderBufferCleanupTXN(), ReorderBufferFreeSnap(), ReorderBufferTransferSnapToParent(), SnapBuildCommitTxn(), and SnapBuildRestore().

◆ SnapBuildSnapIncRefcount()

static void SnapBuildSnapIncRefcount ( Snapshot  snap)
static

◆ SnapBuildSnapshotExists()

bool SnapBuildSnapshotExists ( XLogRecPtr  lsn)

Definition at line 2159 of file snapbuild.c.

2160 {
2161  char path[MAXPGPATH];
2162  int ret;
2163  struct stat stat_buf;
2164 
2165  sprintf(path, "pg_logical/snapshots/%X-%X.snap",
2166  LSN_FORMAT_ARGS(lsn));
2167 
2168  ret = stat(path, &stat_buf);
2169 
2170  if (ret != 0 && errno != ENOENT)
2171  ereport(ERROR,
2173  errmsg("could not stat file \"%s\": %m", path)));
2174 
2175  return ret == 0;
2176 }

References ereport, errcode_for_file_access(), errmsg(), ERROR, LSN_FORMAT_ARGS, MAXPGPATH, sprintf, and stat.

Referenced by update_local_synced_slot().

◆ SnapBuildWaitSnapshot()

static void SnapBuildWaitSnapshot ( xl_running_xacts running,
TransactionId  cutoff 
)
static

Definition at line 1526 of file snapbuild.c.

1527 {
1528  int off;
1529 
1530  for (off = 0; off < running->xcnt; off++)
1531  {
1532  TransactionId xid = running->xids[off];
1533 
1534  /*
1535  * Upper layers should prevent that we ever need to wait on ourselves.
1536  * Check anyway, since failing to do so would either result in an
1537  * endless wait or an Assert() failure.
1538  */
1540  elog(ERROR, "waiting for ourselves");
1541 
1542  if (TransactionIdFollows(xid, cutoff))
1543  continue;
1544 
1545  XactLockTableWait(xid, NULL, NULL, XLTW_None);
1546  }
1547 
1548  /*
1549  * All transactions we needed to finish finished - try to ensure there is
1550  * another xl_running_xacts record in a timely manner, without having to
1551  * wait for bgwriter or checkpointer to log one. During recovery we can't
1552  * enforce that, so we'll have to wait.
1553  */
1554  if (!RecoveryInProgress())
1555  {
1557  }
1558 }
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:657
@ XLTW_None
Definition: lmgr.h:26
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1285
TransactionId xids[FLEXIBLE_ARRAY_MEMBER]
Definition: standbydefs.h:56
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:939
bool RecoveryInProgress(void)
Definition: xlog.c:6290

References elog, ERROR, LogStandbySnapshot(), RecoveryInProgress(), TransactionIdFollows(), TransactionIdIsCurrentTransactionId(), XactLockTableWait(), xl_running_xacts::xcnt, xl_running_xacts::xids, and XLTW_None.

Referenced by SnapBuildFindSnapshot().

◆ SnapBuildXactNeedsSkip()

bool SnapBuildXactNeedsSkip ( SnapBuild builder,
XLogRecPtr  ptr 
)

Definition at line 443 of file snapbuild.c.

444 {
445  return ptr < builder->start_decoding_at;
446 }

References SnapBuild::start_decoding_at.

Referenced by AssertTXNLsnOrder(), DecodeTXNNeedSkip(), logicalmsg_decode(), and ReorderBufferCanStartStreaming().

◆ SnapBuildXidHasCatalogChanges()

static bool SnapBuildXidHasCatalogChanges ( SnapBuild builder,
TransactionId  xid,
uint32  xinfo 
)
inlinestatic

Definition at line 1197 of file snapbuild.c.

1199 {
1200  if (ReorderBufferXidHasCatalogChanges(builder->reorder, xid))
1201  return true;
1202 
1203  /*
1204  * The transactions that have changed catalogs must have invalidation
1205  * info.
1206  */
1207  if (!(xinfo & XACT_XINFO_HAS_INVALS))
1208  return false;
1209 
1210  /* Check the catchange XID array */
1211  return ((builder->catchange.xcnt > 0) &&
1212  (bsearch(&xid, builder->catchange.xip, builder->catchange.xcnt,
1213  sizeof(TransactionId), xidComparator) != NULL));
1214 }
bool ReorderBufferXidHasCatalogChanges(ReorderBuffer *rb, TransactionId xid)
#define XACT_XINFO_HAS_INVALS
Definition: xact.h:191

References SnapBuild::catchange, SnapBuild::reorder, ReorderBufferXidHasCatalogChanges(), XACT_XINFO_HAS_INVALS, SnapBuild::xcnt, xidComparator(), and SnapBuild::xip.

Referenced by SnapBuildCommitTxn().

Variable Documentation

◆ ExportInProgress

bool ExportInProgress = false
static

◆ SavedResourceOwnerDuringExport

ResourceOwner SavedResourceOwnerDuringExport = NULL
static