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/block.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   5
 

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, 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)
 

Variables

static ResourceOwner SavedResourceOwnerDuringExport = NULL
 
static bool ExportInProgress = false
 

Macro Definition Documentation

◆ SNAPBUILD_MAGIC

#define SNAPBUILD_MAGIC   0x51A1E001

Definition at line 1583 of file snapbuild.c.

◆ SNAPBUILD_VERSION

#define SNAPBUILD_VERSION   5

Definition at line 1584 of file snapbuild.c.

◆ SnapBuildOnDiskConstantSize

#define SnapBuildOnDiskConstantSize    offsetof(SnapBuildOnDisk, builder)

Definition at line 1578 of file snapbuild.c.

◆ SnapBuildOnDiskNotChecksummedSize

#define SnapBuildOnDiskNotChecksummedSize    offsetof(SnapBuildOnDisk, version)

Definition at line 1580 of file snapbuild.c.

Typedef Documentation

◆ SnapBuildOnDisk

Function Documentation

◆ AllocateSnapshotBuilder()

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

Definition at line 317 of file snapbuild.c.

322 {
323  MemoryContext context;
324  MemoryContext oldcontext;
325  SnapBuild *builder;
326 
327  /* allocate memory in own context, to have better accountability */
329  "snapshot builder context",
331  oldcontext = MemoryContextSwitchTo(context);
332 
333  builder = palloc0(sizeof(SnapBuild));
334 
335  builder->state = SNAPBUILD_START;
336  builder->context = context;
337  builder->reorder = reorder;
338  /* Other struct members initialized by zeroing via palloc0 above */
339 
340  builder->committed.xcnt = 0;
341  builder->committed.xcnt_space = 128; /* arbitrary number */
342  builder->committed.xip =
343  palloc0(builder->committed.xcnt_space * sizeof(TransactionId));
344  builder->committed.includes_all_transactions = true;
345 
346  builder->catchange.xcnt = 0;
347  builder->catchange.xip = NULL;
348 
349  builder->initial_xmin_horizon = xmin_horizon;
350  builder->start_decoding_at = start_lsn;
351  builder->building_full_snapshot = need_full_snapshot;
352  builder->two_phase_at = two_phase_at;
353 
354  MemoryContextSwitchTo(oldcontext);
355 
356  return builder;
357 }
uint32 TransactionId
Definition: c.h:641
void * palloc0(Size size)
Definition: mcxt.c:1257
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
@ SNAPBUILD_START
Definition: snapbuild.h:23
XLogRecPtr start_decoding_at
Definition: snapbuild.c:171
SnapBuildState state
Definition: snapbuild.c:156
TransactionId initial_xmin_horizon
Definition: snapbuild.c:188
struct SnapBuild::@16 committed
TransactionId * xip
Definition: snapbuild.c:248
XLogRecPtr two_phase_at
Definition: snapbuild.c:182
bool building_full_snapshot
Definition: snapbuild.c:191
struct SnapBuild::@17 catchange
size_t xcnt
Definition: snapbuild.c:222
size_t xcnt_space
Definition: snapbuild.c:225
bool includes_all_transactions
Definition: snapbuild.c:232
MemoryContext context
Definition: snapbuild.c:159
ReorderBuffer * reorder
Definition: snapbuild.c:206

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, SnapBuild::building_full_snapshot, SnapBuild::catchange, SnapBuild::committed, SnapBuild::context, CurrentMemoryContext, 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 2055 of file snapbuild.c.

2056 {
2057  XLogRecPtr cutoff;
2058  XLogRecPtr redo;
2059  DIR *snap_dir;
2060  struct dirent *snap_de;
2061  char path[MAXPGPATH + 21];
2062 
2063  /*
2064  * We start off with a minimum of the last redo pointer. No new
2065  * replication slot will start before that, so that's a safe upper bound
2066  * for removal.
2067  */
2068  redo = GetRedoRecPtr();
2069 
2070  /* now check for the restart ptrs from existing slots */
2072 
2073  /* don't start earlier than the restart lsn */
2074  if (redo < cutoff)
2075  cutoff = redo;
2076 
2077  snap_dir = AllocateDir("pg_logical/snapshots");
2078  while ((snap_de = ReadDir(snap_dir, "pg_logical/snapshots")) != NULL)
2079  {
2080  uint32 hi;
2081  uint32 lo;
2082  XLogRecPtr lsn;
2083  PGFileType de_type;
2084 
2085  if (strcmp(snap_de->d_name, ".") == 0 ||
2086  strcmp(snap_de->d_name, "..") == 0)
2087  continue;
2088 
2089  snprintf(path, sizeof(path), "pg_logical/snapshots/%s", snap_de->d_name);
2090  de_type = get_dirent_type(path, snap_de, false, DEBUG1);
2091 
2092  if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_REG)
2093  {
2094  elog(DEBUG1, "only regular files expected: %s", path);
2095  continue;
2096  }
2097 
2098  /*
2099  * temporary filenames from SnapBuildSerialize() include the LSN and
2100  * everything but are postfixed by .$pid.tmp. We can just remove them
2101  * the same as other files because there can be none that are
2102  * currently being written that are older than cutoff.
2103  *
2104  * We just log a message if a file doesn't fit the pattern, it's
2105  * probably some editors lock/state file or similar...
2106  */
2107  if (sscanf(snap_de->d_name, "%X-%X.snap", &hi, &lo) != 2)
2108  {
2109  ereport(LOG,
2110  (errmsg("could not parse file name \"%s\"", path)));
2111  continue;
2112  }
2113 
2114  lsn = ((uint64) hi) << 32 | lo;
2115 
2116  /* check whether we still need it */
2117  if (lsn < cutoff || cutoff == InvalidXLogRecPtr)
2118  {
2119  elog(DEBUG1, "removing snapbuild snapshot %s", path);
2120 
2121  /*
2122  * It's not particularly harmful, though strange, if we can't
2123  * remove the file here. Don't prevent the checkpoint from
2124  * completing, that'd be a cure worse than the disease.
2125  */
2126  if (unlink(path) < 0)
2127  {
2128  ereport(LOG,
2130  errmsg("could not remove file \"%s\": %m",
2131  path)));
2132  continue;
2133  }
2134  }
2135  }
2136  FreeDir(snap_dir);
2137 }
unsigned int uint32
Definition: c.h:495
int errcode_for_file_access(void)
Definition: elog.c:881
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:149
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2854
int FreeDir(DIR *dir)
Definition: fd.c:2906
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2788
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:942
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6051
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 363 of file snapbuild.c.

364 {
365  MemoryContext context = builder->context;
366 
367  /* free snapshot explicitly, that contains some error checking */
368  if (builder->snapshot != NULL)
369  {
371  builder->snapshot = NULL;
372  }
373 
374  /* other resources are deallocated via memory context reset */
375  MemoryContextDelete(context);
376 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
void SnapBuildSnapDecRefcount(Snapshot snap)
Definition: snapbuild.c:458
Snapshot snapshot
Definition: snapbuild.c:196

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

Referenced by FreeDecodingContext().

◆ SnapBuildAddCommittedTxn()

static void SnapBuildAddCommittedTxn ( SnapBuild builder,
TransactionId  xid 
)
static

Definition at line 914 of file snapbuild.c.

915 {
917 
918  if (builder->committed.xcnt == builder->committed.xcnt_space)
919  {
920  builder->committed.xcnt_space = builder->committed.xcnt_space * 2 + 1;
921 
922  elog(DEBUG1, "increasing space for committed transactions to %u",
923  (uint32) builder->committed.xcnt_space);
924 
925  builder->committed.xip = repalloc(builder->committed.xip,
926  builder->committed.xcnt_space * sizeof(TransactionId));
927  }
928 
929  /*
930  * TODO: It might make sense to keep the array sorted here instead of
931  * doing it every time we build a new snapshot. On the other hand this
932  * gets called repeatedly when a transaction with subtransactions commits.
933  */
934  builder->committed.xip[builder->committed.xcnt++] = xid;
935 }
Assert(fmt[strlen(fmt) - 1] !='\n')
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1476
#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 490 of file snapbuild.c.

491 {
492  Snapshot snapshot;
493  Size ssize;
494 
495  Assert(builder->state >= SNAPBUILD_FULL_SNAPSHOT);
496 
497  ssize = sizeof(SnapshotData)
498  + sizeof(TransactionId) * builder->committed.xcnt
499  + sizeof(TransactionId) * 1 /* toplevel xid */ ;
500 
501  snapshot = MemoryContextAllocZero(builder->context, ssize);
502 
504 
505  /*
506  * We misuse the original meaning of SnapshotData's xip and subxip fields
507  * to make the more fitting for our needs.
508  *
509  * In the 'xip' array we store transactions that have to be treated as
510  * committed. Since we will only ever look at tuples from transactions
511  * that have modified the catalog it's more efficient to store those few
512  * that exist between xmin and xmax (frequently there are none).
513  *
514  * Snapshots that are used in transactions that have modified the catalog
515  * also use the 'subxip' array to store their toplevel xid and all the
516  * subtransaction xids so we can recognize when we need to treat rows as
517  * visible that are not in xip but still need to be visible. Subxip only
518  * gets filled when the transaction is copied into the context of a
519  * catalog modifying transaction since we otherwise share a snapshot
520  * between transactions. As long as a txn hasn't modified the catalog it
521  * doesn't need to treat any uncommitted rows as visible, so there is no
522  * need for those xids.
523  *
524  * Both arrays are qsort'ed so that we can use bsearch() on them.
525  */
526  Assert(TransactionIdIsNormal(builder->xmin));
527  Assert(TransactionIdIsNormal(builder->xmax));
528 
529  snapshot->xmin = builder->xmin;
530  snapshot->xmax = builder->xmax;
531 
532  /* store all transactions to be treated as committed by this snapshot */
533  snapshot->xip =
534  (TransactionId *) ((char *) snapshot + sizeof(SnapshotData));
535  snapshot->xcnt = builder->committed.xcnt;
536  memcpy(snapshot->xip,
537  builder->committed.xip,
538  builder->committed.xcnt * sizeof(TransactionId));
539 
540  /* sort so we can bsearch() */
541  qsort(snapshot->xip, snapshot->xcnt, sizeof(TransactionId), xidComparator);
542 
543  /*
544  * Initially, subxip is empty, i.e. it's a snapshot to be used by
545  * transactions that don't modify the catalog. Will be filled by
546  * ReorderBufferCopySnap() if necessary.
547  */
548  snapshot->subxcnt = 0;
549  snapshot->subxip = NULL;
550 
551  snapshot->suboverflowed = false;
552  snapshot->takenDuringRecovery = false;
553  snapshot->copied = false;
554  snapshot->curcid = FirstCommandId;
555  snapshot->active_count = 0;
556  snapshot->regd_count = 0;
557  snapshot->snapXactCompletionCount = 0;
558 
559  return snapshot;
560 }
#define FirstCommandId
Definition: c.h:657
size_t Size
Definition: c.h:594
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1064
#define qsort(a, b, c, d)
Definition: port.h:445
@ SNAPBUILD_FULL_SNAPSHOT
Definition: snapbuild.h:39
struct SnapshotData SnapshotData
@ SNAPSHOT_HISTORIC_MVCC
Definition: snapshot.h:109
TransactionId xmin
Definition: snapbuild.c:162
TransactionId xmax
Definition: snapbuild.c:165
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:138

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 730 of file snapbuild.c.

731 {
732  ResourceOwner tmpResOwner;
733 
734  /* nothing exported, that is the usual case */
735  if (!ExportInProgress)
736  return;
737 
738  if (!IsTransactionState())
739  elog(ERROR, "clearing exported snapshot in wrong transaction state");
740 
741  /*
742  * AbortCurrentTransaction() takes care of resetting the snapshot state,
743  * so remember SavedResourceOwnerDuringExport.
744  */
745  tmpResOwner = SavedResourceOwnerDuringExport;
746 
747  /* make sure nothing could have ever happened */
749 
750  CurrentResourceOwner = tmpResOwner;
751 }
#define ERROR
Definition: elog.h:39
ResourceOwner CurrentResourceOwner
Definition: resowner.c:147
static ResourceOwner SavedResourceOwnerDuringExport
Definition: snapbuild.c:283
static bool ExportInProgress
Definition: snapbuild.c:284
bool IsTransactionState(void)
Definition: xact.c:378
void AbortCurrentTransaction(void)
Definition: xact.c:3305

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 1025 of file snapbuild.c.

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

408 {
409  return builder->state;
410 }

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 861 of file snapbuild.c.

862 {
863  dlist_iter txn_i;
864  ReorderBufferTXN *txn;
865 
866  /*
867  * Iterate through all toplevel transactions. This can include
868  * subtransactions which we just don't yet know to be that, but that's
869  * fine, they will just get an unnecessary snapshot queued.
870  */
871  dlist_foreach(txn_i, &builder->reorder->toplevel_by_lsn)
872  {
873  txn = dlist_container(ReorderBufferTXN, node, txn_i.cur);
874 
876 
877  /*
878  * If we don't have a base snapshot yet, there are no changes in this
879  * transaction which in turn implies we don't yet need a snapshot at
880  * all. We'll add a snapshot when the first change gets queued.
881  *
882  * NB: This works correctly even for subtransactions because
883  * ReorderBufferAssignChild() takes care to transfer the base snapshot
884  * to the top-level transaction, and while iterating the changequeue
885  * we'll get the change from the subtxn.
886  */
887  if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, txn->xid))
888  continue;
889 
890  /*
891  * We don't need to add snapshot to prepared transactions as they
892  * should not see the new catalog contents.
893  */
894  if (rbtxn_prepared(txn) || rbtxn_skip_prepared(txn))
895  continue;
896 
897  elog(DEBUG2, "adding a new snapshot to %u at %X/%X",
898  txn->xid, LSN_FORMAT_ARGS(lsn));
899 
900  /*
901  * increase the snapshot's refcount for the transaction we are handing
902  * it out to
903  */
905  ReorderBufferAddSnapshot(builder->reorder, txn->xid, lsn,
906  builder->snapshot);
907  }
908 }
#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 669 of file snapbuild.c.

670 {
671  Snapshot snap;
672  char *snapname;
673 
675  elog(ERROR, "cannot export a snapshot from within a transaction");
676 
678  elog(ERROR, "can only export one snapshot at a time");
679 
681  ExportInProgress = true;
682 
684 
685  /* There doesn't seem to a nice API to set these */
687  XactReadOnly = true;
688 
689  snap = SnapBuildInitialSnapshot(builder);
690 
691  /*
692  * now that we've built a plain snapshot, make it active and use the
693  * normal mechanisms for exporting it
694  */
695  snapname = ExportSnapshot(snap);
696 
697  ereport(LOG,
698  (errmsg_plural("exported logical decoding snapshot: \"%s\" with %u transaction ID",
699  "exported logical decoding snapshot: \"%s\" with %u transaction IDs",
700  snap->xcnt,
701  snapname, snap->xcnt)));
702  return snapname;
703 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1179
Snapshot SnapBuildInitialSnapshot(SnapBuild *builder)
Definition: snapbuild.c:570
char * ExportSnapshot(Snapshot snapshot)
Definition: snapmgr.c:1071
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4834
bool XactReadOnly
Definition: xact.c:82
void StartTransactionCommand(void)
Definition: xact.c:2937
int XactIsoLevel
Definition: xact.c:79
#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 1320 of file snapbuild.c.

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

383 {
384  /* make sure we don't get passed an external snapshot */
386 
387  /* make sure nobody modified our snapshot */
388  Assert(snap->curcid == FirstCommandId);
389  Assert(!snap->suboverflowed);
390  Assert(!snap->takenDuringRecovery);
391  Assert(snap->regd_count == 0);
392 
393  /* slightly more likely, so it's checked even without c-asserts */
394  if (snap->copied)
395  elog(ERROR, "cannot free a copied snapshot");
396 
397  if (snap->active_count)
398  elog(ERROR, "cannot free an active snapshot");
399 
400  pfree(snap);
401 }
void pfree(void *pointer)
Definition: mcxt.c:1456

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 709 of file snapbuild.c.

710 {
711  Assert(builder->state == SNAPBUILD_CONSISTENT);
712 
713  /* only build a new snapshot if we don't have a prebuilt one */
714  if (builder->snapshot == NULL)
715  {
716  builder->snapshot = SnapBuildBuildSnapshot(builder);
717  /* increase refcount for the snapshot builder */
719  }
720 
721  return builder->snapshot;
722 }

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

Referenced by logicalmsg_decode().

◆ SnapBuildGetTwoPhaseAt()

XLogRecPtr SnapBuildGetTwoPhaseAt ( SnapBuild builder)

Definition at line 416 of file snapbuild.c.

417 {
418  return builder->two_phase_at;
419 }

References SnapBuild::two_phase_at.

Referenced by DecodeCommit().

◆ SnapBuildInitialSnapshot()

Snapshot SnapBuildInitialSnapshot ( SnapBuild builder)

Definition at line 570 of file snapbuild.c.

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

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

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 819 of file snapbuild.c.

821 {
822  CommandId cid;
823 
824  /*
825  * we only log new_cid's if a catalog tuple was modified, so mark the
826  * transaction as containing catalog modifications
827  */
828  ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
829 
830  ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn,
831  xlrec->target_locator, xlrec->target_tid,
832  xlrec->cmin, xlrec->cmax,
833  xlrec->combocid);
834 
835  /* figure out new command id */
836  if (xlrec->cmin != InvalidCommandId &&
837  xlrec->cmax != InvalidCommandId)
838  cid = Max(xlrec->cmin, xlrec->cmax);
839  else if (xlrec->cmax != InvalidCommandId)
840  cid = xlrec->cmax;
841  else if (xlrec->cmin != InvalidCommandId)
842  cid = xlrec->cmin;
843  else
844  {
845  cid = InvalidCommandId; /* silence compiler */
846  elog(ERROR, "xl_heap_new_cid record without a valid CommandId");
847  }
848 
849  ReorderBufferAddNewCommandId(builder->reorder, xid, lsn, cid + 1);
850 }
#define InvalidCommandId
Definition: c.h:658
#define Max(x, y)
Definition: c.h:987
uint32 CommandId
Definition: c.h:655
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:380
CommandId combocid
Definition: heapam_xlog.h:382
ItemPointerData target_tid
Definition: heapam_xlog.h:388
TransactionId top_xid
Definition: heapam_xlog.h:379
CommandId cmax
Definition: heapam_xlog.h:381
RelFileLocator target_locator
Definition: heapam_xlog.h:387

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 1218 of file snapbuild.c.

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

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 948 of file snapbuild.c.

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

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 757 of file snapbuild.c.

758 {
760  ExportInProgress = false;
761 }

References ExportInProgress, and SavedResourceOwnerDuringExport.

Referenced by AbortTransaction().

◆ SnapBuildRestore()

static bool SnapBuildRestore ( SnapBuild builder,
XLogRecPtr  lsn 
)
static

Definition at line 1846 of file snapbuild.c.

1847 {
1848  SnapBuildOnDisk ondisk;
1849  int fd;
1850  char path[MAXPGPATH];
1851  Size sz;
1852  pg_crc32c checksum;
1853 
1854  /* no point in loading a snapshot if we're already there */
1855  if (builder->state == SNAPBUILD_CONSISTENT)
1856  return false;
1857 
1858  sprintf(path, "pg_logical/snapshots/%X-%X.snap",
1859  LSN_FORMAT_ARGS(lsn));
1860 
1861  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
1862 
1863  if (fd < 0 && errno == ENOENT)
1864  return false;
1865  else if (fd < 0)
1866  ereport(ERROR,
1868  errmsg("could not open file \"%s\": %m", path)));
1869 
1870  /* ----
1871  * Make sure the snapshot had been stored safely to disk, that's normally
1872  * cheap.
1873  * Note that we do not need PANIC here, nobody will be able to use the
1874  * slot without fsyncing, and saving it won't succeed without an fsync()
1875  * either...
1876  * ----
1877  */
1878  fsync_fname(path, false);
1879  fsync_fname("pg_logical/snapshots", true);
1880 
1881 
1882  /* read statically sized portion of snapshot */
1883  SnapBuildRestoreContents(fd, (char *) &ondisk, SnapBuildOnDiskConstantSize, path);
1884 
1885  if (ondisk.magic != SNAPBUILD_MAGIC)
1886  ereport(ERROR,
1888  errmsg("snapbuild state file \"%s\" has wrong magic number: %u instead of %u",
1889  path, ondisk.magic, SNAPBUILD_MAGIC)));
1890 
1891  if (ondisk.version != SNAPBUILD_VERSION)
1892  ereport(ERROR,
1894  errmsg("snapbuild state file \"%s\" has unsupported version: %u instead of %u",
1895  path, ondisk.version, SNAPBUILD_VERSION)));
1896 
1897  INIT_CRC32C(checksum);
1898  COMP_CRC32C(checksum,
1899  ((char *) &ondisk) + SnapBuildOnDiskNotChecksummedSize,
1901 
1902  /* read SnapBuild */
1903  SnapBuildRestoreContents(fd, (char *) &ondisk.builder, sizeof(SnapBuild), path);
1904  COMP_CRC32C(checksum, &ondisk.builder, sizeof(SnapBuild));
1905 
1906  /* restore committed xacts information */
1907  if (ondisk.builder.committed.xcnt > 0)
1908  {
1909  sz = sizeof(TransactionId) * ondisk.builder.committed.xcnt;
1910  ondisk.builder.committed.xip = MemoryContextAllocZero(builder->context, sz);
1911  SnapBuildRestoreContents(fd, (char *) ondisk.builder.committed.xip, sz, path);
1912  COMP_CRC32C(checksum, ondisk.builder.committed.xip, sz);
1913  }
1914 
1915  /* restore catalog modifying xacts information */
1916  if (ondisk.builder.catchange.xcnt > 0)
1917  {
1918  sz = sizeof(TransactionId) * ondisk.builder.catchange.xcnt;
1919  ondisk.builder.catchange.xip = MemoryContextAllocZero(builder->context, sz);
1920  SnapBuildRestoreContents(fd, (char *) ondisk.builder.catchange.xip, sz, path);
1921  COMP_CRC32C(checksum, ondisk.builder.catchange.xip, sz);
1922  }
1923 
1924  if (CloseTransientFile(fd) != 0)
1925  ereport(ERROR,
1927  errmsg("could not close file \"%s\": %m", path)));
1928 
1929  FIN_CRC32C(checksum);
1930 
1931  /* verify checksum of what we've read */
1932  if (!EQ_CRC32C(checksum, ondisk.checksum))
1933  ereport(ERROR,
1935  errmsg("checksum mismatch for snapbuild state file \"%s\": is %u, should be %u",
1936  path, checksum, ondisk.checksum)));
1937 
1938  /*
1939  * ok, we now have a sensible snapshot here, figure out if it has more
1940  * information than we have.
1941  */
1942 
1943  /*
1944  * We are only interested in consistent snapshots for now, comparing
1945  * whether one incomplete snapshot is more "advanced" seems to be
1946  * unnecessarily complex.
1947  */
1948  if (ondisk.builder.state < SNAPBUILD_CONSISTENT)
1949  goto snapshot_not_interesting;
1950 
1951  /*
1952  * Don't use a snapshot that requires an xmin that we cannot guarantee to
1953  * be available.
1954  */
1956  goto snapshot_not_interesting;
1957 
1958  /*
1959  * Consistent snapshots have no next phase. Reset next_phase_at as it is
1960  * possible that an old value may remain.
1961  */
1964 
1965  /* ok, we think the snapshot is sensible, copy over everything important */
1966  builder->xmin = ondisk.builder.xmin;
1967  builder->xmax = ondisk.builder.xmax;
1968  builder->state = ondisk.builder.state;
1969 
1970  builder->committed.xcnt = ondisk.builder.committed.xcnt;
1971  /* We only allocated/stored xcnt, not xcnt_space xids ! */
1972  /* don't overwrite preallocated xip, if we don't have anything here */
1973  if (builder->committed.xcnt > 0)
1974  {
1975  pfree(builder->committed.xip);
1976  builder->committed.xcnt_space = ondisk.builder.committed.xcnt;
1977  builder->committed.xip = ondisk.builder.committed.xip;
1978  }
1979  ondisk.builder.committed.xip = NULL;
1980 
1981  /* set catalog modifying transactions */
1982  if (builder->catchange.xip)
1983  pfree(builder->catchange.xip);
1984  builder->catchange.xcnt = ondisk.builder.catchange.xcnt;
1985  builder->catchange.xip = ondisk.builder.catchange.xip;
1986  ondisk.builder.catchange.xip = NULL;
1987 
1988  /* our snapshot is not interesting anymore, build a new one */
1989  if (builder->snapshot != NULL)
1990  {
1992  }
1993  builder->snapshot = SnapBuildBuildSnapshot(builder);
1995 
1996  ReorderBufferSetRestartPoint(builder->reorder, lsn);
1997 
1998  Assert(builder->state == SNAPBUILD_CONSISTENT);
1999 
2000  ereport(LOG,
2001  (errmsg("logical decoding found consistent point at %X/%X",
2002  LSN_FORMAT_ARGS(lsn)),
2003  errdetail("Logical decoding will begin using saved snapshot.")));
2004  return true;
2005 
2006 snapshot_not_interesting:
2007  if (ondisk.builder.committed.xip != NULL)
2008  pfree(ondisk.builder.committed.xip);
2009  if (ondisk.builder.catchange.xip != NULL)
2010  pfree(ondisk.builder.catchange.xip);
2011  return false;
2012 }
#define PG_BINARY
Definition: c.h:1283
int CloseTransientFile(int fd)
Definition: fd.c:2754
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:708
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2578
#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:1584
#define SnapBuildOnDiskNotChecksummedSize
Definition: snapbuild.c:1580
#define SNAPBUILD_MAGIC
Definition: snapbuild.c:1583
#define SnapBuildOnDiskConstantSize
Definition: snapbuild.c:1578
static void SnapBuildRestoreContents(int fd, char *dest, Size size, const char *path)
Definition: snapbuild.c:2018
SnapBuild builder
Definition: snapbuild.c:1573
pg_crc32c checksum
Definition: snapbuild.c:1563

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 2018 of file snapbuild.c.

2019 {
2020  int readBytes;
2021 
2022  pgstat_report_wait_start(WAIT_EVENT_SNAPBUILD_READ);
2023  readBytes = read(fd, dest, size);
2025  if (readBytes != size)
2026  {
2027  int save_errno = errno;
2028 
2030 
2031  if (readBytes < 0)
2032  {
2033  errno = save_errno;
2034  ereport(ERROR,
2036  errmsg("could not read file \"%s\": %m", path)));
2037  }
2038  else
2039  ereport(ERROR,
2041  errmsg("could not read file \"%s\": read %d of %zu",
2042  path, readBytes, size)));
2043  }
2044 }
#define read(a, b, c)
Definition: win32.h:13
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:88
static void pgstat_report_wait_end(void)
Definition: wait_event.h:104

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(), and read.

Referenced by SnapBuildRestore().

◆ SnapBuildSerializationPoint()

void SnapBuildSerializationPoint ( SnapBuild builder,
XLogRecPtr  lsn 
)

Definition at line 1593 of file snapbuild.c.

1594 {
1595  if (builder->state < SNAPBUILD_CONSISTENT)
1596  SnapBuildRestore(builder, lsn);
1597  else
1598  SnapBuildSerialize(builder, lsn);
1599 }

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

Referenced by xlog_decode().

◆ SnapBuildSerialize()

static void SnapBuildSerialize ( SnapBuild builder,
XLogRecPtr  lsn 
)
static

Definition at line 1606 of file snapbuild.c.

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

426 {
427  builder->two_phase_at = ptr;
428 }

References SnapBuild::two_phase_at.

Referenced by CreateDecodingContext().

◆ SnapBuildSnapDecRefcount()

void SnapBuildSnapDecRefcount ( Snapshot  snap)

Definition at line 458 of file snapbuild.c.

459 {
460  /* make sure we don't get passed an external snapshot */
462 
463  /* make sure nobody modified our snapshot */
464  Assert(snap->curcid == FirstCommandId);
465  Assert(!snap->suboverflowed);
466  Assert(!snap->takenDuringRecovery);
467 
468  Assert(snap->regd_count == 0);
469 
470  Assert(snap->active_count > 0);
471 
472  /* slightly more likely, so it's checked even without casserts */
473  if (snap->copied)
474  elog(ERROR, "cannot free a copied snapshot");
475 
476  snap->active_count--;
477  if (snap->active_count == 0)
478  SnapBuildFreeSnapshot(snap);
479 }
static void SnapBuildFreeSnapshot(Snapshot snap)
Definition: snapbuild.c:382

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

◆ SnapBuildWaitSnapshot()

static void SnapBuildWaitSnapshot ( xl_running_xacts running,
TransactionId  cutoff 
)
static

Definition at line 1510 of file snapbuild.c.

1511 {
1512  int off;
1513 
1514  for (off = 0; off < running->xcnt; off++)
1515  {
1516  TransactionId xid = running->xids[off];
1517 
1518  /*
1519  * Upper layers should prevent that we ever need to wait on ourselves.
1520  * Check anyway, since failing to do so would either result in an
1521  * endless wait or an Assert() failure.
1522  */
1524  elog(ERROR, "waiting for ourselves");
1525 
1526  if (TransactionIdFollows(xid, cutoff))
1527  continue;
1528 
1529  XactLockTableWait(xid, NULL, NULL, XLTW_None);
1530  }
1531 
1532  /*
1533  * All transactions we needed to finish finished - try to ensure there is
1534  * another xl_running_xacts record in a timely manner, without having to
1535  * wait for bgwriter or checkpointer to log one. During recovery we can't
1536  * enforce that, so we'll have to wait.
1537  */
1538  if (!RecoveryInProgress())
1539  {
1541  }
1542 }
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:668
@ XLTW_None
Definition: lmgr.h:26
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1287
TransactionId xids[FLEXIBLE_ARRAY_MEMBER]
Definition: standbydefs.h:56
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:926
bool RecoveryInProgress(void)
Definition: xlog.c:5948

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 434 of file snapbuild.c.

435 {
436  return ptr < builder->start_decoding_at;
437 }

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 1188 of file snapbuild.c.

1190 {
1191  if (ReorderBufferXidHasCatalogChanges(builder->reorder, xid))
1192  return true;
1193 
1194  /*
1195  * The transactions that have changed catalogs must have invalidation
1196  * info.
1197  */
1198  if (!(xinfo & XACT_XINFO_HAS_INVALS))
1199  return false;
1200 
1201  /* Check the catchange XID array */
1202  return ((builder->catchange.xcnt > 0) &&
1203  (bsearch(&xid, builder->catchange.xip, builder->catchange.xcnt,
1204  sizeof(TransactionId), xidComparator) != NULL));
1205 }
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