PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
snapbuild.h File Reference
#include "access/xlogdefs.h"
#include "utils/snapmgr.h"
Include dependency graph for snapbuild.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct SnapBuild SnapBuild
 

Enumerations

enum  SnapBuildState { SNAPBUILD_START = -1, SNAPBUILD_BUILDING_SNAPSHOT = 0, SNAPBUILD_FULL_SNAPSHOT = 1, SNAPBUILD_CONSISTENT = 2 }
 

Functions

void CheckPointSnapBuild (void)
 
SnapBuildAllocateSnapshotBuilder (struct ReorderBuffer *cache, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot)
 
void FreeSnapshotBuilder (SnapBuild *cache)
 
void SnapBuildSnapDecRefcount (Snapshot snap)
 
Snapshot SnapBuildInitialSnapshot (SnapBuild *builder)
 
const char * SnapBuildExportSnapshot (SnapBuild *snapstate)
 
void SnapBuildClearExportedSnapshot (void)
 
SnapBuildState SnapBuildCurrentState (SnapBuild *snapstate)
 
Snapshot SnapBuildGetOrBuildSnapshot (SnapBuild *builder, TransactionId xid)
 
bool SnapBuildXactNeedsSkip (SnapBuild *snapstate, XLogRecPtr ptr)
 
void SnapBuildCommitTxn (SnapBuild *builder, XLogRecPtr lsn, TransactionId xid, int nsubxacts, TransactionId *subxacts)
 
bool SnapBuildProcessChange (SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
 
void SnapBuildProcessNewCid (SnapBuild *builder, TransactionId xid, XLogRecPtr lsn, struct xl_heap_new_cid *cid)
 
void SnapBuildProcessRunningXacts (SnapBuild *builder, XLogRecPtr lsn, struct xl_running_xacts *running)
 
void SnapBuildSerializationPoint (SnapBuild *builder, XLogRecPtr lsn)
 

Typedef Documentation

Definition at line 51 of file snapbuild.h.

Enumeration Type Documentation

Enumerator
SNAPBUILD_START 
SNAPBUILD_BUILDING_SNAPSHOT 
SNAPBUILD_FULL_SNAPSHOT 
SNAPBUILD_CONSISTENT 

Definition at line 18 of file snapbuild.h.

19 {
20  /*
21  * Initial state, we can't do much yet.
22  */
23  SNAPBUILD_START = -1,
24 
25  /*
26  * Collecting committed transactions, to build the initial catalog
27  * snapshot.
28  */
30 
31  /*
32  * We have collected enough information to decode tuples in transactions
33  * that started after this.
34  *
35  * Once we reached this we start to collect changes. We cannot apply them
36  * yet, because they might be based on transactions that were still
37  * running when FULL_SNAPSHOT was reached.
38  */
40 
41  /*
42  * Found a point after SNAPBUILD_FULL_SNAPSHOT where all transactions that
43  * were running at that point finished. Till we reach that we hold off
44  * calling any commit callbacks.
45  */
SnapBuildState
Definition: snapbuild.h:18

Function Documentation

SnapBuild* AllocateSnapshotBuilder ( struct ReorderBuffer cache,
TransactionId  xmin_horizon,
XLogRecPtr  start_lsn,
bool  need_full_snapshot 
)

Definition at line 316 of file snapbuild.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), SnapBuild::building_full_snapshot, 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::xcnt, SnapBuild::xcnt_space, and SnapBuild::xip.

Referenced by StartupDecodingContext().

320 {
321  MemoryContext context;
322  MemoryContext oldcontext;
323  SnapBuild *builder;
324 
325  /* allocate memory in own context, to have better accountability */
327  "snapshot builder context",
329  oldcontext = MemoryContextSwitchTo(context);
330 
331  builder = palloc0(sizeof(SnapBuild));
332 
333  builder->state = SNAPBUILD_START;
334  builder->context = context;
335  builder->reorder = reorder;
336  /* Other struct members initialized by zeroing via palloc0 above */
337 
338  builder->committed.xcnt = 0;
339  builder->committed.xcnt_space = 128; /* arbitrary number */
340  builder->committed.xip =
341  palloc0(builder->committed.xcnt_space * sizeof(TransactionId));
342  builder->committed.includes_all_transactions = true;
343 
344  builder->initial_xmin_horizon = xmin_horizon;
345  builder->start_decoding_at = start_lsn;
346  builder->building_full_snapshot = need_full_snapshot;
347 
348  MemoryContextSwitchTo(oldcontext);
349 
350  return builder;
351 }
struct SnapBuild::@24 committed
uint32 TransactionId
Definition: c.h:391
bool building_full_snapshot
Definition: snapbuild.c:180
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
size_t xcnt_space
Definition: snapbuild.c:227
size_t xcnt
Definition: snapbuild.c:224
SnapBuildState state
Definition: snapbuild.c:156
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
ReorderBuffer * reorder
Definition: snapbuild.c:195
TransactionId initial_xmin_horizon
Definition: snapbuild.c:177
TransactionId * xip
Definition: snapbuild.c:250
bool includes_all_transactions
Definition: snapbuild.c:234
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:877
MemoryContext context
Definition: snapbuild.c:159
XLogRecPtr start_decoding_at
Definition: snapbuild.c:171
void CheckPointSnapBuild ( void  )

Definition at line 1861 of file snapbuild.c.

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

Referenced by CheckPointGuts().

1862 {
1863  XLogRecPtr cutoff;
1864  XLogRecPtr redo;
1865  DIR *snap_dir;
1866  struct dirent *snap_de;
1867  char path[MAXPGPATH + 21];
1868 
1869  /*
1870  * We start off with a minimum of the last redo pointer. No new
1871  * replication slot will start before that, so that's a safe upper bound
1872  * for removal.
1873  */
1874  redo = GetRedoRecPtr();
1875 
1876  /* now check for the restart ptrs from existing slots */
1878 
1879  /* don't start earlier than the restart lsn */
1880  if (redo < cutoff)
1881  cutoff = redo;
1882 
1883  snap_dir = AllocateDir("pg_logical/snapshots");
1884  while ((snap_de = ReadDir(snap_dir, "pg_logical/snapshots")) != NULL)
1885  {
1886  uint32 hi;
1887  uint32 lo;
1888  XLogRecPtr lsn;
1889  struct stat statbuf;
1890 
1891  if (strcmp(snap_de->d_name, ".") == 0 ||
1892  strcmp(snap_de->d_name, "..") == 0)
1893  continue;
1894 
1895  snprintf(path, sizeof(path), "pg_logical/snapshots/%s", snap_de->d_name);
1896 
1897  if (lstat(path, &statbuf) == 0 && !S_ISREG(statbuf.st_mode))
1898  {
1899  elog(DEBUG1, "only regular files expected: %s", path);
1900  continue;
1901  }
1902 
1903  /*
1904  * temporary filenames from SnapBuildSerialize() include the LSN and
1905  * everything but are postfixed by .$pid.tmp. We can just remove them
1906  * the same as other files because there can be none that are
1907  * currently being written that are older than cutoff.
1908  *
1909  * We just log a message if a file doesn't fit the pattern, it's
1910  * probably some editors lock/state file or similar...
1911  */
1912  if (sscanf(snap_de->d_name, "%X-%X.snap", &hi, &lo) != 2)
1913  {
1914  ereport(LOG,
1915  (errmsg("could not parse file name \"%s\"", path)));
1916  continue;
1917  }
1918 
1919  lsn = ((uint64) hi) << 32 | lo;
1920 
1921  /* check whether we still need it */
1922  if (lsn < cutoff || cutoff == InvalidXLogRecPtr)
1923  {
1924  elog(DEBUG1, "removing snapbuild snapshot %s", path);
1925 
1926  /*
1927  * It's not particularly harmful, though strange, if we can't
1928  * remove the file here. Don't prevent the checkpoint from
1929  * completing, that'd be a cure worse than the disease.
1930  */
1931  if (unlink(path) < 0)
1932  {
1933  ereport(LOG,
1935  errmsg("could not remove file \"%s\": %m",
1936  path)));
1937  continue;
1938  }
1939  }
1940  }
1941  FreeDir(snap_dir);
1942 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define LOG
Definition: elog.h:26
Definition: dirent.h:9
Definition: dirent.c:25
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:784
unsigned int uint32
Definition: c.h:258
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2367
#define ereport(elevel, rest)
Definition: elog.h:122
uint64 XLogRecPtr
Definition: xlogdefs.h:21
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2433
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8247
int errmsg(const char *fmt,...)
Definition: elog.c:797
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
#define lstat(path, sb)
Definition: win32.h:262
int FreeDir(DIR *dir)
Definition: fd.c:2476
void FreeSnapshotBuilder ( SnapBuild cache)

Definition at line 357 of file snapbuild.c.

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

Referenced by FreeDecodingContext().

358 {
359  MemoryContext context = builder->context;
360 
361  /* free snapshot explicitly, that contains some error checking */
362  if (builder->snapshot != NULL)
363  {
364  SnapBuildSnapDecRefcount(builder->snapshot);
365  builder->snapshot = NULL;
366  }
367 
368  /* other resources are deallocated via memory context reset */
369  MemoryContextDelete(context);
370 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void SnapBuildSnapDecRefcount(Snapshot snap)
Definition: snapbuild.c:434
void SnapBuildClearExportedSnapshot ( void  )

Definition at line 696 of file snapbuild.c.

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

Referenced by exec_replication_command().

697 {
698  /* nothing exported, that is the usual case */
699  if (!ExportInProgress)
700  return;
701 
702  if (!IsTransactionState())
703  elog(ERROR, "clearing exported snapshot in wrong transaction state");
704 
705  /* make sure nothing could have ever happened */
707 
710  ExportInProgress = false;
711 }
void AbortCurrentTransaction(void)
Definition: xact.c:2984
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static ResourceOwner SavedResourceOwnerDuringExport
Definition: snapbuild.c:258
#define ERROR
Definition: elog.h:43
static bool ExportInProgress
Definition: snapbuild.c:259
bool IsTransactionState(void)
Definition: xact.c:351
#define elog
Definition: elog.h:219
void SnapBuildCommitTxn ( SnapBuild builder,
XLogRecPtr  lsn,
TransactionId  xid,
int  nsubxacts,
TransactionId subxacts 
)

Definition at line 927 of file snapbuild.c.

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

Referenced by DecodeCommit().

929 {
930  int nxact;
931 
932  bool needs_snapshot = false;
933  bool needs_timetravel = false;
934  bool sub_needs_timetravel = false;
935 
936  TransactionId xmax = xid;
937 
938  /*
939  * Transactions preceding BUILDING_SNAPSHOT will neither be decoded, nor
940  * will they be part of a snapshot. So we don't need to record anything.
941  */
942  if (builder->state == SNAPBUILD_START ||
943  (builder->state == SNAPBUILD_BUILDING_SNAPSHOT &&
945  {
946  /* ensure that only commits after this are getting replayed */
947  if (builder->start_decoding_at <= lsn)
948  builder->start_decoding_at = lsn + 1;
949  return;
950  }
951 
952  if (builder->state < SNAPBUILD_CONSISTENT)
953  {
954  /* ensure that only commits after this are getting replayed */
955  if (builder->start_decoding_at <= lsn)
956  builder->start_decoding_at = lsn + 1;
957 
958  /*
959  * If building an exportable snapshot, force xid to be tracked, even
960  * if the transaction didn't modify the catalog.
961  */
962  if (builder->building_full_snapshot)
963  {
964  needs_timetravel = true;
965  }
966  }
967 
968  for (nxact = 0; nxact < nsubxacts; nxact++)
969  {
970  TransactionId subxid = subxacts[nxact];
971 
972  /*
973  * Add subtransaction to base snapshot if catalog modifying, we don't
974  * distinguish to toplevel transactions there.
975  */
976  if (ReorderBufferXidHasCatalogChanges(builder->reorder, subxid))
977  {
978  sub_needs_timetravel = true;
979  needs_snapshot = true;
980 
981  elog(DEBUG1, "found subtransaction %u:%u with catalog changes",
982  xid, subxid);
983 
984  SnapBuildAddCommittedTxn(builder, subxid);
985 
986  if (NormalTransactionIdFollows(subxid, xmax))
987  xmax = subxid;
988  }
989 
990  /*
991  * If we're forcing timetravel we also need visibility information
992  * about subtransaction, so keep track of subtransaction's state, even
993  * if not catalog modifying. Don't need to distribute a snapshot in
994  * that case.
995  */
996  else if (needs_timetravel)
997  {
998  SnapBuildAddCommittedTxn(builder, subxid);
999  if (NormalTransactionIdFollows(subxid, xmax))
1000  xmax = subxid;
1001  }
1002  }
1003 
1004  /* if top-level modified catalog, it'll need a snapshot */
1005  if (ReorderBufferXidHasCatalogChanges(builder->reorder, xid))
1006  {
1007  elog(DEBUG2, "found top level transaction %u, with catalog changes",
1008  xid);
1009  needs_snapshot = true;
1010  needs_timetravel = true;
1011  SnapBuildAddCommittedTxn(builder, xid);
1012  }
1013  else if (sub_needs_timetravel)
1014  {
1015  /* track toplevel txn as well, subxact alone isn't meaningful */
1016  SnapBuildAddCommittedTxn(builder, xid);
1017  }
1018  else if (needs_timetravel)
1019  {
1020  elog(DEBUG2, "forced transaction %u to do timetravel", xid);
1021 
1022  SnapBuildAddCommittedTxn(builder, xid);
1023  }
1024 
1025  if (!needs_timetravel)
1026  {
1027  /* record that we cannot export a general snapshot anymore */
1028  builder->committed.includes_all_transactions = false;
1029  }
1030 
1031  Assert(!needs_snapshot || needs_timetravel);
1032 
1033  /*
1034  * Adjust xmax of the snapshot builder, we only do that for committed,
1035  * catalog modifying, transactions, everything else isn't interesting for
1036  * us since we'll never look at the respective rows.
1037  */
1038  if (needs_timetravel &&
1039  (!TransactionIdIsValid(builder->xmax) ||
1040  TransactionIdFollowsOrEquals(xmax, builder->xmax)))
1041  {
1042  builder->xmax = xmax;
1043  TransactionIdAdvance(builder->xmax);
1044  }
1045 
1046  /* if there's any reason to build a historic snapshot, do so now */
1047  if (needs_snapshot)
1048  {
1049  /*
1050  * If we haven't built a complete snapshot yet there's no need to hand
1051  * it out, it wouldn't (and couldn't) be used anyway.
1052  */
1053  if (builder->state < SNAPBUILD_FULL_SNAPSHOT)
1054  return;
1055 
1056  /*
1057  * Decrease the snapshot builder's refcount of the old snapshot, note
1058  * that it still will be used if it has been handed out to the
1059  * reorderbuffer earlier.
1060  */
1061  if (builder->snapshot)
1063 
1064  builder->snapshot = SnapBuildBuildSnapshot(builder);
1065 
1066  /* we might need to execute invalidations, add snapshot */
1067  if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, xid))
1068  {
1070  ReorderBufferSetBaseSnapshot(builder->reorder, xid, lsn,
1071  builder->snapshot);
1072  }
1073 
1074  /* refcount of the snapshot builder for the new snapshot */
1076 
1077  /* add a new Snapshot to all currently running transactions */
1079  }
1080 }
#define TransactionIdAdvance(dest)
Definition: transam.h:48
bool ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
struct SnapBuild::@24 committed
#define DEBUG1
Definition: elog.h:25
static void SnapBuildAddCommittedTxn(SnapBuild *builder, TransactionId xid)
Definition: snapbuild.c:857
uint32 TransactionId
Definition: c.h:391
Snapshot snapshot
Definition: snapbuild.c:185
bool building_full_snapshot
Definition: snapbuild.c:180
void ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Snapshot snap)
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
SnapBuildState state
Definition: snapbuild.c:156
static void SnapBuildDistributeNewCatalogSnapshot(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:811
#define DEBUG2
Definition: elog.h:24
ReorderBuffer * reorder
Definition: snapbuild.c:195
static Snapshot SnapBuildBuildSnapshot(SnapBuild *builder)
Definition: snapbuild.c:466
bool includes_all_transactions
Definition: snapbuild.c:234
bool ReorderBufferXidHasCatalogChanges(ReorderBuffer *rb, TransactionId xid)
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
TransactionId xmax
Definition: snapbuild.c:165
#define Assert(condition)
Definition: c.h:681
#define NormalTransactionIdFollows(id1, id2)
Definition: transam.h:67
void SnapBuildSnapDecRefcount(Snapshot snap)
Definition: snapbuild.c:434
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void SnapBuildSnapIncRefcount(Snapshot snap)
Definition: snapbuild.c:422
static TransactionId SnapBuildNextPhaseAt(SnapBuild *builder)
Definition: snapbuild.c:286
XLogRecPtr start_decoding_at
Definition: snapbuild.c:171
SnapBuildState SnapBuildCurrentState ( SnapBuild snapstate)

Definition at line 401 of file snapbuild.c.

References SnapBuild::state.

Referenced by DecodeHeap2Op(), DecodeHeapOp(), DecodeLogicalMsgOp(), DecodeXactOp(), and DecodingContextReady().

402 {
403  return builder->state;
404 }
const char* SnapBuildExportSnapshot ( SnapBuild snapstate)

Definition at line 635 of file snapbuild.c.

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

Referenced by CreateReplicationSlot().

636 {
637  Snapshot snap;
638  char *snapname;
639 
641  elog(ERROR, "cannot export a snapshot from within a transaction");
642 
644  elog(ERROR, "can only export one snapshot at a time");
645 
647  ExportInProgress = true;
648 
650 
651  /* There doesn't seem to a nice API to set these */
653  XactReadOnly = true;
654 
655  snap = SnapBuildInitialSnapshot(builder);
656 
657  /*
658  * now that we've built a plain snapshot, make it active and use the
659  * normal mechanisms for exporting it
660  */
661  snapname = ExportSnapshot(snap);
662 
663  ereport(LOG,
664  (errmsg_plural("exported logical decoding snapshot: \"%s\" with %u transaction ID",
665  "exported logical decoding snapshot: \"%s\" with %u transaction IDs",
666  snap->xcnt,
667  snapname, snap->xcnt)));
668  return snapname;
669 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:850
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
#define XACT_REPEATABLE_READ
Definition: xact.h:30
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4465
char * ExportSnapshot(Snapshot snapshot)
Definition: snapmgr.c:1159
#define LOG
Definition: elog.h:26
static ResourceOwner SavedResourceOwnerDuringExport
Definition: snapbuild.c:258
#define ERROR
Definition: elog.h:43
static bool ExportInProgress
Definition: snapbuild.c:259
#define ereport(elevel, rest)
Definition: elog.h:122
bool XactReadOnly
Definition: xact.c:77
void StartTransactionCommand(void)
Definition: xact.c:2673
int XactIsoLevel
Definition: xact.c:74
Snapshot SnapBuildInitialSnapshot(SnapBuild *builder)
Definition: snapbuild.c:545
uint32 xcnt
Definition: snapshot.h:80
#define elog
Definition: elog.h:219
Snapshot SnapBuildGetOrBuildSnapshot ( SnapBuild builder,
TransactionId  xid 
)

Definition at line 675 of file snapbuild.c.

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

Referenced by DecodeLogicalMsgOp().

676 {
677  Assert(builder->state == SNAPBUILD_CONSISTENT);
678 
679  /* only build a new snapshot if we don't have a prebuilt one */
680  if (builder->snapshot == NULL)
681  {
682  builder->snapshot = SnapBuildBuildSnapshot(builder);
683  /* increase refcount for the snapshot builder */
685  }
686 
687  return builder->snapshot;
688 }
Snapshot snapshot
Definition: snapbuild.c:185
SnapBuildState state
Definition: snapbuild.c:156
static Snapshot SnapBuildBuildSnapshot(SnapBuild *builder)
Definition: snapbuild.c:466
#define Assert(condition)
Definition: c.h:681
static void SnapBuildSnapIncRefcount(Snapshot snap)
Definition: snapbuild.c:422
Snapshot SnapBuildInitialSnapshot ( SnapBuild builder)

Definition at line 545 of file snapbuild.c.

References Assert, SnapBuild::committed, elog, ereport, errcode(), errmsg(), ERROR, FirstSnapshotSet, GetMaxSnapshotXidCount(), GetOldestSafeDecodingTransactionId(), SnapBuild::includes_all_transactions, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyPgXact, NormalTransactionIdPrecedes, palloc(), SNAPBUILD_CONSISTENT, SnapBuildBuildSnapshot(), SnapBuild::state, test(), TransactionIdAdvance, TransactionIdIsValid, TransactionIdPrecedesOrEquals(), XACT_REPEATABLE_READ, XactIsoLevel, SnapshotData::xcnt, xidComparator(), SnapshotData::xip, SnapshotData::xmax, SnapshotData::xmin, and PGXACT::xmin.

Referenced by CreateReplicationSlot(), and SnapBuildExportSnapshot().

546 {
547  Snapshot snap;
548  TransactionId xid;
549  TransactionId *newxip;
550  int newxcnt = 0;
551 
554 
555  if (builder->state != SNAPBUILD_CONSISTENT)
556  elog(ERROR, "cannot build an initial slot snapshot before reaching a consistent state");
557 
558  if (!builder->committed.includes_all_transactions)
559  elog(ERROR, "cannot build an initial slot snapshot, not all transactions are monitored anymore");
560 
561  /* so we don't overwrite the existing value */
563  elog(ERROR, "cannot build an initial slot snapshot when MyPgXact->xmin already is valid");
564 
565  snap = SnapBuildBuildSnapshot(builder);
566 
567  /*
568  * We know that snap->xmin is alive, enforced by the logical xmin
569  * mechanism. Due to that we can do this without locks, we're only
570  * changing our own value.
571  */
572 #ifdef USE_ASSERT_CHECKING
573  {
574  TransactionId safeXid;
575 
576  LWLockAcquire(ProcArrayLock, LW_SHARED);
577  safeXid = GetOldestSafeDecodingTransactionId(false);
578  LWLockRelease(ProcArrayLock);
579 
580  Assert(TransactionIdPrecedesOrEquals(safeXid, snap->xmin));
581  }
582 #endif
583 
584  MyPgXact->xmin = snap->xmin;
585 
586  /* allocate in transaction context */
587  newxip = (TransactionId *)
589 
590  /*
591  * snapbuild.c builds transactions in an "inverted" manner, which means it
592  * stores committed transactions in ->xip, not ones in progress. Build a
593  * classical snapshot by marking all non-committed transactions as
594  * in-progress. This can be expensive.
595  */
596  for (xid = snap->xmin; NormalTransactionIdPrecedes(xid, snap->xmax);)
597  {
598  void *test;
599 
600  /*
601  * Check whether transaction committed using the decoding snapshot
602  * meaning of ->xip.
603  */
604  test = bsearch(&xid, snap->xip, snap->xcnt,
605  sizeof(TransactionId), xidComparator);
606 
607  if (test == NULL)
608  {
609  if (newxcnt >= GetMaxSnapshotXidCount())
610  ereport(ERROR,
611  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
612  errmsg("initial slot snapshot too large")));
613 
614  newxip[newxcnt++] = xid;
615  }
616 
618  }
619 
620  snap->xcnt = newxcnt;
621  snap->xip = newxip;
622 
623  return snap;
624 }
#define TransactionIdAdvance(dest)
Definition: transam.h:48
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2158
struct SnapBuild::@24 committed
static void test(void)
uint32 TransactionId
Definition: c.h:391
TransactionId xmin
Definition: proc.h:225
#define XACT_REPEATABLE_READ
Definition: xact.h:30
int errcode(int sqlerrcode)
Definition: elog.c:575
PGXACT * MyPgXact
Definition: proc.c:68
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
SnapBuildState state
Definition: snapbuild.c:156
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
#define ERROR
Definition: elog.h:43
bool FirstSnapshotSet
Definition: snapmgr.c:203
static Snapshot SnapBuildBuildSnapshot(SnapBuild *builder)
Definition: snapbuild.c:466
bool includes_all_transactions
Definition: snapbuild.c:234
TransactionId xmax
Definition: snapshot.h:69
TransactionId xmin
Definition: snapshot.h:68
#define ereport(elevel, rest)
Definition: elog.h:122
TransactionId * xip
Definition: snapshot.h:79
int GetMaxSnapshotXidCount(void)
Definition: procarray.c:1456
#define Assert(condition)
Definition: c.h:681
int XactIsoLevel
Definition: xact.c:74
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
#define NormalTransactionIdPrecedes(id1, id2)
Definition: transam.h:62
uint32 xcnt
Definition: snapshot.h:80
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:138
bool SnapBuildProcessChange ( SnapBuild builder,
TransactionId  xid,
XLogRecPtr  lsn 
)

Definition at line 719 of file snapbuild.c.

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

Referenced by DecodeHeap2Op(), DecodeHeapOp(), and DecodeLogicalMsgOp().

720 {
721  /*
722  * We can't handle data in transactions if we haven't built a snapshot
723  * yet, so don't store them.
724  */
725  if (builder->state < SNAPBUILD_FULL_SNAPSHOT)
726  return false;
727 
728  /*
729  * No point in keeping track of changes in transactions that we don't have
730  * enough information about to decode. This means that they started before
731  * we got into the SNAPBUILD_FULL_SNAPSHOT state.
732  */
733  if (builder->state < SNAPBUILD_CONSISTENT &&
735  return false;
736 
737  /*
738  * If the reorderbuffer doesn't yet have a snapshot, add one now, it will
739  * be needed to decode the change we're currently processing.
740  */
741  if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, xid))
742  {
743  /* only build a new snapshot if we don't have a prebuilt one */
744  if (builder->snapshot == NULL)
745  {
746  builder->snapshot = SnapBuildBuildSnapshot(builder);
747  /* increase refcount for the snapshot builder */
749  }
750 
751  /*
752  * Increase refcount for the transaction we're handing the snapshot
753  * out to.
754  */
756  ReorderBufferSetBaseSnapshot(builder->reorder, xid, lsn,
757  builder->snapshot);
758  }
759 
760  return true;
761 }
bool ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
Snapshot snapshot
Definition: snapbuild.c:185
void ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Snapshot snap)
SnapBuildState state
Definition: snapbuild.c:156
ReorderBuffer * reorder
Definition: snapbuild.c:195
static Snapshot SnapBuildBuildSnapshot(SnapBuild *builder)
Definition: snapbuild.c:466
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static void SnapBuildSnapIncRefcount(Snapshot snap)
Definition: snapbuild.c:422
static TransactionId SnapBuildNextPhaseAt(SnapBuild *builder)
Definition: snapbuild.c:286
void SnapBuildProcessNewCid ( SnapBuild builder,
TransactionId  xid,
XLogRecPtr  lsn,
struct xl_heap_new_cid cid 
)

Definition at line 769 of file snapbuild.c.

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_node, xl_heap_new_cid::target_tid, and xl_heap_new_cid::top_xid.

Referenced by DecodeHeap2Op().

771 {
772  CommandId cid;
773 
774  /*
775  * we only log new_cid's if a catalog tuple was modified, so mark the
776  * transaction as containing catalog modifications
777  */
778  ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
779 
780  ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn,
781  xlrec->target_node, xlrec->target_tid,
782  xlrec->cmin, xlrec->cmax,
783  xlrec->combocid);
784 
785  /* figure out new command id */
786  if (xlrec->cmin != InvalidCommandId &&
787  xlrec->cmax != InvalidCommandId)
788  cid = Max(xlrec->cmin, xlrec->cmax);
789  else if (xlrec->cmax != InvalidCommandId)
790  cid = xlrec->cmax;
791  else if (xlrec->cmin != InvalidCommandId)
792  cid = xlrec->cmin;
793  else
794  {
795  cid = InvalidCommandId; /* silence compiler */
796  elog(ERROR, "xl_heap_new_cid record without a valid CommandId");
797  }
798 
799  ReorderBufferAddNewCommandId(builder->reorder, xid, lsn, cid + 1);
800 }
uint32 CommandId
Definition: c.h:405
void ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, RelFileNode node, ItemPointerData tid, CommandId cmin, CommandId cmax, CommandId combocid)
#define ERROR
Definition: elog.h:43
ReorderBuffer * reorder
Definition: snapbuild.c:195
#define InvalidCommandId
Definition: c.h:408
#define Max(x, y)
Definition: c.h:806
void ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
#define elog
Definition: elog.h:219
void ReorderBufferAddNewCommandId(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, CommandId cid)
void SnapBuildProcessRunningXacts ( SnapBuild builder,
XLogRecPtr  lsn,
struct xl_running_xacts running 
)

Definition at line 1094 of file snapbuild.c.

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

Referenced by DecodeStandbyOp().

1095 {
1096  ReorderBufferTXN *txn;
1097 
1098  /*
1099  * If we're not consistent yet, inspect the record to see whether it
1100  * allows to get closer to being consistent. If we are consistent, dump
1101  * our snapshot so others or we, after a restart, can use it.
1102  */
1103  if (builder->state < SNAPBUILD_CONSISTENT)
1104  {
1105  /* returns false if there's no point in performing cleanup just yet */
1106  if (!SnapBuildFindSnapshot(builder, lsn, running))
1107  return;
1108  }
1109  else
1110  SnapBuildSerialize(builder, lsn);
1111 
1112  /*
1113  * Update range of interesting xids based on the running xacts
1114  * information. We don't increase ->xmax using it, because once we are in
1115  * a consistent state we can do that ourselves and much more efficiently
1116  * so, because we only need to do it for catalog transactions since we
1117  * only ever look at those.
1118  *
1119  * NB: We only increase xmax when a catalog modifying transaction commits
1120  * (see SnapBuildCommitTxn). Because of this, xmax can be lower than
1121  * xmin, which looks odd but is correct and actually more efficient, since
1122  * we hit fast paths in tqual.c.
1123  */
1124  builder->xmin = running->oldestRunningXid;
1125 
1126  /* Remove transactions we don't need to keep track off anymore */
1127  SnapBuildPurgeCommittedTxn(builder);
1128 
1129  elog(DEBUG3, "xmin: %u, xmax: %u, oldestrunning: %u",
1130  builder->xmin, builder->xmax,
1131  running->oldestRunningXid);
1132 
1133  /*
1134  * Increase shared memory limits, so vacuum can work on tuples we
1135  * prevented from being pruned till now.
1136  */
1138 
1139  /*
1140  * Also tell the slot where we can restart decoding from. We don't want to
1141  * do that after every commit because changing that implies an fsync of
1142  * the logical slot's state file, so we only do it every time we see a
1143  * running xacts record.
1144  *
1145  * Do so by looking for the oldest in progress transaction (determined by
1146  * the first LSN of any of its relevant records). Every transaction
1147  * remembers the last location we stored the snapshot to disk before its
1148  * beginning. That point is where we can restart from.
1149  */
1150 
1151  /*
1152  * Can't know about a serialized snapshot's location if we're not
1153  * consistent.
1154  */
1155  if (builder->state < SNAPBUILD_CONSISTENT)
1156  return;
1157 
1158  txn = ReorderBufferGetOldestTXN(builder->reorder);
1159 
1160  /*
1161  * oldest ongoing txn might have started when we didn't yet serialize
1162  * anything because we hadn't reached a consistent state yet.
1163  */
1164  if (txn != NULL && txn->restart_decoding_lsn != InvalidXLogRecPtr)
1166 
1167  /*
1168  * No in-progress transaction, can reuse the last serialized snapshot if
1169  * we have one.
1170  */
1171  else if (txn == NULL &&
1175  builder->last_serialized_snapshot);
1176 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG3
Definition: elog.h:23
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition: logical.c:845
XLogRecPtr current_restart_decoding_lsn
static bool SnapBuildFindSnapshot(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
Definition: snapbuild.c:1189
SnapBuildState state
Definition: snapbuild.c:156
static void SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:1477
ReorderBuffer * reorder
Definition: snapbuild.c:195
XLogRecPtr last_serialized_snapshot
Definition: snapbuild.c:190
TransactionId xmax
Definition: snapbuild.c:165
ReorderBufferTXN * ReorderBufferGetOldestTXN(ReorderBuffer *rb)
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition: logical.c:788
TransactionId xmin
Definition: snapbuild.c:162
XLogRecPtr restart_decoding_lsn
TransactionId oldestRunningXid
Definition: standbydefs.h:53
#define elog
Definition: elog.h:219
static void SnapBuildPurgeCommittedTxn(SnapBuild *builder)
Definition: snapbuild.c:886
void SnapBuildSerializationPoint ( SnapBuild builder,
XLogRecPtr  lsn 
)

Definition at line 1464 of file snapbuild.c.

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

Referenced by DecodeXLogOp().

1465 {
1466  if (builder->state < SNAPBUILD_CONSISTENT)
1467  SnapBuildRestore(builder, lsn);
1468  else
1469  SnapBuildSerialize(builder, lsn);
1470 }
static bool SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:1668
SnapBuildState state
Definition: snapbuild.c:156
static void SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:1477
void SnapBuildSnapDecRefcount ( Snapshot  snap)

Definition at line 434 of file snapbuild.c.

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

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

435 {
436  /* make sure we don't get passed an external snapshot */
438 
439  /* make sure nobody modified our snapshot */
440  Assert(snap->curcid == FirstCommandId);
441  Assert(!snap->suboverflowed);
442  Assert(!snap->takenDuringRecovery);
443 
444  Assert(snap->regd_count == 0);
445 
446  Assert(snap->active_count > 0);
447 
448  /* slightly more likely, so it's checked even without casserts */
449  if (snap->copied)
450  elog(ERROR, "cannot free a copied snapshot");
451 
452  snap->active_count--;
453  if (snap->active_count == 0)
454  SnapBuildFreeSnapshot(snap);
455 }
SnapshotSatisfiesFunc satisfies
Definition: snapshot.h:55
bool copied
Definition: snapshot.h:96
bool suboverflowed
Definition: snapshot.h:93
uint32 regd_count
Definition: snapshot.h:110
#define FirstCommandId
Definition: c.h:407
#define ERROR
Definition: elog.h:43
static void SnapBuildFreeSnapshot(Snapshot snap)
Definition: snapbuild.c:376
bool HeapTupleSatisfiesHistoricMVCC(HeapTuple htup, Snapshot snapshot, Buffer buffer)
Definition: tqual.c:1672
CommandId curcid
Definition: snapshot.h:98
#define Assert(condition)
Definition: c.h:681
bool takenDuringRecovery
Definition: snapshot.h:95
#define elog
Definition: elog.h:219
uint32 active_count
Definition: snapshot.h:109
bool SnapBuildXactNeedsSkip ( SnapBuild snapstate,
XLogRecPtr  ptr 
)

Definition at line 410 of file snapbuild.c.

References SnapBuild::start_decoding_at.

Referenced by DecodeCommit(), and DecodeLogicalMsgOp().

411 {
412  return ptr < builder->start_decoding_at;
413 }