PostgreSQL Source Code git master
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 *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, bool in_slot_creation, XLogRecPtr two_phase_at)
 
void FreeSnapshotBuilder (SnapBuild *builder)
 
void SnapBuildSnapDecRefcount (Snapshot snap)
 
Snapshot SnapBuildInitialSnapshot (SnapBuild *builder)
 
const char * SnapBuildExportSnapshot (SnapBuild *builder)
 
void SnapBuildClearExportedSnapshot (void)
 
void SnapBuildResetExportedSnapshotState (void)
 
SnapBuildState SnapBuildCurrentState (SnapBuild *builder)
 
Snapshot SnapBuildGetOrBuildSnapshot (SnapBuild *builder)
 
bool SnapBuildXactNeedsSkip (SnapBuild *builder, XLogRecPtr ptr)
 
XLogRecPtr SnapBuildGetTwoPhaseAt (SnapBuild *builder)
 
void SnapBuildSetTwoPhaseAt (SnapBuild *builder, XLogRecPtr ptr)
 
void SnapBuildCommitTxn (SnapBuild *builder, XLogRecPtr lsn, TransactionId xid, int nsubxacts, TransactionId *subxacts, uint32 xinfo)
 
bool SnapBuildProcessChange (SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
 
void SnapBuildProcessNewCid (SnapBuild *builder, TransactionId xid, XLogRecPtr lsn, struct xl_heap_new_cid *xlrec)
 
void SnapBuildProcessRunningXacts (SnapBuild *builder, XLogRecPtr lsn, struct xl_running_xacts *running)
 
void SnapBuildSerializationPoint (SnapBuild *builder, XLogRecPtr lsn)
 
bool SnapBuildSnapshotExists (XLogRecPtr lsn)
 

Typedef Documentation

◆ SnapBuild

typedef struct SnapBuild SnapBuild

Definition at line 55 of file snapbuild.h.

Enumeration Type Documentation

◆ SnapBuildState

Enumerator
SNAPBUILD_START 
SNAPBUILD_BUILDING_SNAPSHOT 
SNAPBUILD_FULL_SNAPSHOT 
SNAPBUILD_CONSISTENT 

Definition at line 22 of file snapbuild.h.

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

Function Documentation

◆ AllocateSnapshotBuilder()

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

Definition at line 185 of file snapbuild.c.

191{
192 MemoryContext context;
193 MemoryContext oldcontext;
194 SnapBuild *builder;
195
196 /* allocate memory in own context, to have better accountability */
198 "snapshot builder context",
200 oldcontext = MemoryContextSwitchTo(context);
201
202 builder = palloc0_object(SnapBuild);
203
204 builder->state = SNAPBUILD_START;
205 builder->context = context;
206 builder->reorder = reorder;
207 /* Other struct members initialized by zeroing via palloc0 above */
208
209 builder->committed.xcnt = 0;
210 builder->committed.xcnt_space = 128; /* arbitrary number */
211 builder->committed.xip =
212 palloc0(builder->committed.xcnt_space * sizeof(TransactionId));
213 builder->committed.includes_all_transactions = true;
214
215 builder->catchange.xcnt = 0;
216 builder->catchange.xip = NULL;
217
218 builder->initial_xmin_horizon = xmin_horizon;
219 builder->start_decoding_at = start_lsn;
220 builder->in_slot_creation = in_slot_creation;
221 builder->building_full_snapshot = need_full_snapshot;
222 builder->two_phase_at = two_phase_at;
223
224 MemoryContextSwitchTo(oldcontext);
225
226 return builder;
227}
uint32 TransactionId
Definition: c.h:671
#define palloc0_object(type)
Definition: fe_memutils.h:75
void * palloc0(Size size)
Definition: mcxt.c:1395
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
XLogRecPtr start_decoding_at
SnapBuildState state
TransactionId initial_xmin_horizon
struct SnapBuild::@112 committed
struct SnapBuild::@113 catchange
TransactionId * xip
XLogRecPtr two_phase_at
bool building_full_snapshot
bool includes_all_transactions
MemoryContext context
ReorderBuffer * reorder

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, SnapBuild::building_full_snapshot, SnapBuild::catchange, SnapBuild::committed, SnapBuild::context, CurrentMemoryContext, SnapBuild::in_slot_creation, SnapBuild::includes_all_transactions, SnapBuild::initial_xmin_horizon, MemoryContextSwitchTo(), palloc0(), palloc0_object, 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 1969 of file snapbuild.c.

1970{
1971 XLogRecPtr cutoff;
1972 XLogRecPtr redo;
1973 DIR *snap_dir;
1974 struct dirent *snap_de;
1975 char path[MAXPGPATH + sizeof(PG_LOGICAL_SNAPSHOTS_DIR)];
1976
1977 /*
1978 * We start off with a minimum of the last redo pointer. No new
1979 * replication slot will start before that, so that's a safe upper bound
1980 * for removal.
1981 */
1982 redo = GetRedoRecPtr();
1983
1984 /* now check for the restart ptrs from existing slots */
1986
1987 /* don't start earlier than the restart lsn */
1988 if (redo < cutoff)
1989 cutoff = redo;
1990
1992 while ((snap_de = ReadDir(snap_dir, PG_LOGICAL_SNAPSHOTS_DIR)) != NULL)
1993 {
1994 uint32 hi;
1995 uint32 lo;
1996 XLogRecPtr lsn;
1997 PGFileType de_type;
1998
1999 if (strcmp(snap_de->d_name, ".") == 0 ||
2000 strcmp(snap_de->d_name, "..") == 0)
2001 continue;
2002
2003 snprintf(path, sizeof(path), "%s/%s", PG_LOGICAL_SNAPSHOTS_DIR, snap_de->d_name);
2004 de_type = get_dirent_type(path, snap_de, false, DEBUG1);
2005
2006 if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_REG)
2007 {
2008 elog(DEBUG1, "only regular files expected: %s", path);
2009 continue;
2010 }
2011
2012 /*
2013 * temporary filenames from SnapBuildSerialize() include the LSN and
2014 * everything but are postfixed by .$pid.tmp. We can just remove them
2015 * the same as other files because there can be none that are
2016 * currently being written that are older than cutoff.
2017 *
2018 * We just log a message if a file doesn't fit the pattern, it's
2019 * probably some editors lock/state file or similar...
2020 */
2021 if (sscanf(snap_de->d_name, "%X-%X.snap", &hi, &lo) != 2)
2022 {
2023 ereport(LOG,
2024 (errmsg("could not parse file name \"%s\"", path)));
2025 continue;
2026 }
2027
2028 lsn = ((uint64) hi) << 32 | lo;
2029
2030 /* check whether we still need it */
2031 if (lsn < cutoff || !XLogRecPtrIsValid(cutoff))
2032 {
2033 elog(DEBUG1, "removing snapbuild snapshot %s", path);
2034
2035 /*
2036 * It's not particularly harmful, though strange, if we can't
2037 * remove the file here. Don't prevent the checkpoint from
2038 * completing, that'd be a cure worse than the disease.
2039 */
2040 if (unlink(path) < 0)
2041 {
2042 ereport(LOG,
2044 errmsg("could not remove file \"%s\": %m",
2045 path)));
2046 continue;
2047 }
2048 }
2049 }
2050 FreeDir(snap_dir);
2051}
uint64_t uint64
Definition: c.h:553
uint32_t uint32
Definition: c.h:552
int errcode_for_file_access(void)
Definition: elog.c:886
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
int FreeDir(DIR *dir)
Definition: fd.c:3005
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2887
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2953
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition: file_utils.c:547
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:260
#define PG_LOGICAL_SNAPSHOTS_DIR
Definition: reorderbuffer.h:24
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:1304
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6507
#define XLogRecPtrIsValid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21

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

Referenced by CheckPointGuts().

◆ FreeSnapshotBuilder()

void FreeSnapshotBuilder ( SnapBuild builder)

Definition at line 233 of file snapbuild.c.

234{
235 MemoryContext context = builder->context;
236
237 /* free snapshot explicitly, that contains some error checking */
238 if (builder->snapshot != NULL)
239 {
241 builder->snapshot = NULL;
242 }
243
244 /* other resources are deallocated via memory context reset */
245 MemoryContextDelete(context);
246}
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
void SnapBuildSnapDecRefcount(Snapshot snap)
Definition: snapbuild.c:328
Snapshot snapshot

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

Referenced by FreeDecodingContext().

◆ SnapBuildClearExportedSnapshot()

void SnapBuildClearExportedSnapshot ( void  )

Definition at line 599 of file snapbuild.c.

600{
601 ResourceOwner tmpResOwner;
602
603 /* nothing exported, that is the usual case */
604 if (!ExportInProgress)
605 return;
606
607 if (!IsTransactionState())
608 elog(ERROR, "clearing exported snapshot in wrong transaction state");
609
610 /*
611 * AbortCurrentTransaction() takes care of resetting the snapshot state,
612 * so remember SavedResourceOwnerDuringExport.
613 */
614 tmpResOwner = SavedResourceOwnerDuringExport;
615
616 /* make sure nothing could have ever happened */
618
619 CurrentResourceOwner = tmpResOwner;
620}
#define ERROR
Definition: elog.h:39
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
static ResourceOwner SavedResourceOwnerDuringExport
Definition: snapbuild.c:151
static bool ExportInProgress
Definition: snapbuild.c:152
bool IsTransactionState(void)
Definition: xact.c:388
void AbortCurrentTransaction(void)
Definition: xact.c:3469

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

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

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

◆ SnapBuildExportSnapshot()

const char * SnapBuildExportSnapshot ( SnapBuild builder)

Definition at line 538 of file snapbuild.c.

539{
540 Snapshot snap;
541 char *snapname;
542
544 elog(ERROR, "cannot export a snapshot from within a transaction");
545
547 elog(ERROR, "can only export one snapshot at a time");
548
550 ExportInProgress = true;
551
553
554 /* There doesn't seem to a nice API to set these */
556 XactReadOnly = true;
557
558 snap = SnapBuildInitialSnapshot(builder);
559
560 /*
561 * now that we've built a plain snapshot, make it active and use the
562 * normal mechanisms for exporting it
563 */
564 snapname = ExportSnapshot(snap);
565
566 ereport(LOG,
567 (errmsg_plural("exported logical decoding snapshot: \"%s\" with %u transaction ID",
568 "exported logical decoding snapshot: \"%s\" with %u transaction IDs",
569 snap->xcnt,
570 snapname, snap->xcnt)));
571 return snapname;
572}
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1193
Snapshot SnapBuildInitialSnapshot(SnapBuild *builder)
Definition: snapbuild.c:440
char * ExportSnapshot(Snapshot snapshot)
Definition: snapmgr.c:1115
uint32 xcnt
Definition: snapshot.h:165
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:5007
bool XactReadOnly
Definition: xact.c:83
void StartTransactionCommand(void)
Definition: xact.c:3077
int XactIsoLevel
Definition: xact.c:80
#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().

◆ SnapBuildGetOrBuildSnapshot()

Snapshot SnapBuildGetOrBuildSnapshot ( SnapBuild builder)

Definition at line 578 of file snapbuild.c.

579{
580 Assert(builder->state == SNAPBUILD_CONSISTENT);
581
582 /* only build a new snapshot if we don't have a prebuilt one */
583 if (builder->snapshot == NULL)
584 {
585 builder->snapshot = SnapBuildBuildSnapshot(builder);
586 /* increase refcount for the snapshot builder */
588 }
589
590 return builder->snapshot;
591}

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

Referenced by logicalmsg_decode().

◆ SnapBuildGetTwoPhaseAt()

XLogRecPtr SnapBuildGetTwoPhaseAt ( SnapBuild builder)

Definition at line 286 of file snapbuild.c.

287{
288 return builder->two_phase_at;
289}

References SnapBuild::two_phase_at.

Referenced by DecodeCommit().

◆ SnapBuildInitialSnapshot()

Snapshot SnapBuildInitialSnapshot ( SnapBuild builder)

Definition at line 440 of file snapbuild.c.

441{
442 Snapshot snap;
443 TransactionId xid;
444 TransactionId safeXid;
445 TransactionId *newxip;
446 int newxcnt = 0;
447
450
451 /* don't allow older snapshots */
452 InvalidateCatalogSnapshot(); /* about to overwrite MyProc->xmin */
454 elog(ERROR, "cannot build an initial slot snapshot when snapshots exist");
456
457 if (builder->state != SNAPBUILD_CONSISTENT)
458 elog(ERROR, "cannot build an initial slot snapshot before reaching a consistent state");
459
461 elog(ERROR, "cannot build an initial slot snapshot, not all transactions are monitored anymore");
462
463 /* so we don't overwrite the existing value */
465 elog(ERROR, "cannot build an initial slot snapshot when MyProc->xmin already is valid");
466
467 snap = SnapBuildBuildSnapshot(builder);
468
469 /*
470 * We know that snap->xmin is alive, enforced by the logical xmin
471 * mechanism. Due to that we can do this without locks, we're only
472 * changing our own value.
473 *
474 * Building an initial snapshot is expensive and an unenforced xmin
475 * horizon would have bad consequences, therefore always double-check that
476 * the horizon is enforced.
477 */
478 LWLockAcquire(ProcArrayLock, LW_SHARED);
479 safeXid = GetOldestSafeDecodingTransactionId(false);
480 LWLockRelease(ProcArrayLock);
481
482 if (TransactionIdFollows(safeXid, snap->xmin))
483 elog(ERROR, "cannot build an initial slot snapshot as oldest safe xid %u follows snapshot's xmin %u",
484 safeXid, snap->xmin);
485
486 MyProc->xmin = snap->xmin;
487
488 /* allocate in transaction context */
490
491 /*
492 * snapbuild.c builds transactions in an "inverted" manner, which means it
493 * stores committed transactions in ->xip, not ones in progress. Build a
494 * classical snapshot by marking all non-committed transactions as
495 * in-progress. This can be expensive.
496 */
497 for (xid = snap->xmin; NormalTransactionIdPrecedes(xid, snap->xmax);)
498 {
499 void *test;
500
501 /*
502 * Check whether transaction committed using the decoding snapshot
503 * meaning of ->xip.
504 */
505 test = bsearch(&xid, snap->xip, snap->xcnt,
507
508 if (test == NULL)
509 {
510 if (newxcnt >= GetMaxSnapshotXidCount())
513 errmsg("initial slot snapshot too large")));
514
515 newxip[newxcnt++] = xid;
516 }
517
519 }
520
521 /* adjust remaining snapshot fields as needed */
523 snap->xcnt = newxcnt;
524 snap->xip = newxip;
525
526 return snap;
527}
int errcode(int sqlerrcode)
Definition: elog.c:863
#define palloc_array(type, count)
Definition: fe_memutils.h:76
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_SHARED
Definition: lwlock.h:113
#define ERRCODE_T_R_SERIALIZATION_FAILURE
Definition: pgbench.c:77
static void test(void)
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2907
int GetMaxSnapshotXidCount(void)
Definition: procarray.c:2017
bool HistoricSnapshotActive(void)
Definition: snapmgr.c:1692
bool HaveRegisteredOrActiveSnapshot(void)
Definition: snapmgr.c:1644
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:455
@ SNAPSHOT_MVCC
Definition: snapshot.h:46
PGPROC * MyProc
Definition: proc.c:67
TransactionId xmin
Definition: proc.h:194
TransactionId xmin
Definition: snapshot.h:153
TransactionId xmax
Definition: snapshot.h:154
SnapshotType snapshot_type
Definition: snapshot.h:140
TransactionId * xip
Definition: snapshot.h:164
static bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.h:297
#define NormalTransactionIdPrecedes(id1, id2)
Definition: transam.h:147
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:152

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_array, 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 638 of file snapbuild.c.

639{
640 /*
641 * We can't handle data in transactions if we haven't built a snapshot
642 * yet, so don't store them.
643 */
644 if (builder->state < SNAPBUILD_FULL_SNAPSHOT)
645 return false;
646
647 /*
648 * No point in keeping track of changes in transactions that we don't have
649 * enough information about to decode. This means that they started before
650 * we got into the SNAPBUILD_FULL_SNAPSHOT state.
651 */
652 if (builder->state < SNAPBUILD_CONSISTENT &&
654 return false;
655
656 /*
657 * If the reorderbuffer doesn't yet have a snapshot, add one now, it will
658 * be needed to decode the change we're currently processing.
659 */
660 if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, xid))
661 {
662 /* only build a new snapshot if we don't have a prebuilt one */
663 if (builder->snapshot == NULL)
664 {
665 builder->snapshot = SnapBuildBuildSnapshot(builder);
666 /* increase refcount for the snapshot builder */
668 }
669
670 /*
671 * Increase refcount for the transaction we're handing the snapshot
672 * out to.
673 */
675 ReorderBufferSetBaseSnapshot(builder->reorder, xid, lsn,
676 builder->snapshot);
677 }
678
679 return true;
680}

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,
struct xl_heap_new_cid xlrec 
)

Definition at line 688 of file snapbuild.c.

690{
691 CommandId cid;
692
693 /*
694 * we only log new_cid's if a catalog tuple was modified, so mark the
695 * transaction as containing catalog modifications
696 */
697 ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
698
699 ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn,
700 xlrec->target_locator, xlrec->target_tid,
701 xlrec->cmin, xlrec->cmax,
702 xlrec->combocid);
703
704 /* figure out new command id */
705 if (xlrec->cmin != InvalidCommandId &&
706 xlrec->cmax != InvalidCommandId)
707 cid = Max(xlrec->cmin, xlrec->cmax);
708 else if (xlrec->cmax != InvalidCommandId)
709 cid = xlrec->cmax;
710 else if (xlrec->cmin != InvalidCommandId)
711 cid = xlrec->cmin;
712 else
713 {
714 cid = InvalidCommandId; /* silence compiler */
715 elog(ERROR, "xl_heap_new_cid record without a valid CommandId");
716 }
717
718 ReorderBufferAddNewCommandId(builder->reorder, xid, lsn, cid + 1);
719}
#define InvalidCommandId
Definition: c.h:688
#define Max(x, y)
Definition: c.h:1010
uint32 CommandId
Definition: c.h:685
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:467
CommandId combocid
Definition: heapam_xlog.h:469
ItemPointerData target_tid
Definition: heapam_xlog.h:475
TransactionId top_xid
Definition: heapam_xlog.h:466
CommandId cmax
Definition: heapam_xlog.h:468
RelFileLocator target_locator
Definition: heapam_xlog.h:474

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,
struct xl_running_xacts running 
)

Definition at line 1135 of file snapbuild.c.

1136{
1137 ReorderBufferTXN *txn;
1138 TransactionId xmin;
1139
1140 /*
1141 * If we're not consistent yet, inspect the record to see whether it
1142 * allows to get closer to being consistent. If we are consistent, dump
1143 * our snapshot so others or we, after a restart, can use it.
1144 */
1145 if (builder->state < SNAPBUILD_CONSISTENT)
1146 {
1147 /* returns false if there's no point in performing cleanup just yet */
1148 if (!SnapBuildFindSnapshot(builder, lsn, running))
1149 return;
1150 }
1151 else
1152 SnapBuildSerialize(builder, lsn);
1153
1154 /*
1155 * Update range of interesting xids based on the running xacts
1156 * information. We don't increase ->xmax using it, because once we are in
1157 * a consistent state we can do that ourselves and much more efficiently
1158 * so, because we only need to do it for catalog transactions since we
1159 * only ever look at those.
1160 *
1161 * NB: We only increase xmax when a catalog modifying transaction commits
1162 * (see SnapBuildCommitTxn). Because of this, xmax can be lower than
1163 * xmin, which looks odd but is correct and actually more efficient, since
1164 * we hit fast paths in heapam_visibility.c.
1165 */
1166 builder->xmin = running->oldestRunningXid;
1167
1168 /* Remove transactions we don't need to keep track off anymore */
1169 SnapBuildPurgeOlderTxn(builder);
1170
1171 /*
1172 * Advance the xmin limit for the current replication slot, to allow
1173 * vacuum to clean up the tuples this slot has been protecting.
1174 *
1175 * The reorderbuffer might have an xmin among the currently running
1176 * snapshots; use it if so. If not, we need only consider the snapshots
1177 * we'll produce later, which can't be less than the oldest running xid in
1178 * the record we're reading now.
1179 */
1180 xmin = ReorderBufferGetOldestXmin(builder->reorder);
1181 if (xmin == InvalidTransactionId)
1182 xmin = running->oldestRunningXid;
1183 elog(DEBUG3, "xmin: %u, xmax: %u, oldest running: %u, oldest xmin: %u",
1184 builder->xmin, builder->xmax, running->oldestRunningXid, xmin);
1185 LogicalIncreaseXminForSlot(lsn, xmin);
1186
1187 /*
1188 * Also tell the slot where we can restart decoding from. We don't want to
1189 * do that after every commit because changing that implies an fsync of
1190 * the logical slot's state file, so we only do it every time we see a
1191 * running xacts record.
1192 *
1193 * Do so by looking for the oldest in progress transaction (determined by
1194 * the first LSN of any of its relevant records). Every transaction
1195 * remembers the last location we stored the snapshot to disk before its
1196 * beginning. That point is where we can restart from.
1197 */
1198
1199 /*
1200 * Can't know about a serialized snapshot's location if we're not
1201 * consistent.
1202 */
1203 if (builder->state < SNAPBUILD_CONSISTENT)
1204 return;
1205
1206 txn = ReorderBufferGetOldestTXN(builder->reorder);
1207
1208 /*
1209 * oldest ongoing txn might have started when we didn't yet serialize
1210 * anything because we hadn't reached a consistent state yet.
1211 */
1212 if (txn != NULL && XLogRecPtrIsValid(txn->restart_decoding_lsn))
1214
1215 /*
1216 * No in-progress transaction, can reuse the last serialized snapshot if
1217 * we have one.
1218 */
1219 else if (txn == NULL &&
1223 builder->last_serialized_snapshot);
1224}
#define DEBUG3
Definition: elog.h:28
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition: logical.c:1744
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition: logical.c:1676
TransactionId ReorderBufferGetOldestXmin(ReorderBuffer *rb)
ReorderBufferTXN * ReorderBufferGetOldestTXN(ReorderBuffer *rb)
static void SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:1496
static bool SnapBuildFindSnapshot(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
Definition: snapbuild.c:1237
static void SnapBuildPurgeOlderTxn(SnapBuild *builder)
Definition: snapbuild.c:862
XLogRecPtr restart_decoding_lsn
XLogRecPtr current_restart_decoding_lsn
TransactionId xmin
XLogRecPtr last_serialized_snapshot
TransactionId oldestRunningXid
Definition: standbydefs.h:53
#define InvalidTransactionId
Definition: transam.h:31

References ReorderBuffer::current_restart_decoding_lsn, DEBUG3, elog, InvalidTransactionId, 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, XLogRecPtrIsValid, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by standby_decode().

◆ SnapBuildResetExportedSnapshotState()

void SnapBuildResetExportedSnapshotState ( void  )

Definition at line 626 of file snapbuild.c.

627{
629 ExportInProgress = false;
630}

References ExportInProgress, and SavedResourceOwnerDuringExport.

Referenced by AbortTransaction().

◆ SnapBuildSerializationPoint()

void SnapBuildSerializationPoint ( SnapBuild builder,
XLogRecPtr  lsn 
)

Definition at line 1483 of file snapbuild.c.

1484{
1485 if (builder->state < SNAPBUILD_CONSISTENT)
1486 SnapBuildRestore(builder, lsn);
1487 else
1488 SnapBuildSerialize(builder, lsn);
1489}
static bool SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
Definition: snapbuild.c:1840

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

Referenced by xlog_decode().

◆ SnapBuildSetTwoPhaseAt()

void SnapBuildSetTwoPhaseAt ( SnapBuild builder,
XLogRecPtr  ptr 
)

Definition at line 295 of file snapbuild.c.

296{
297 builder->two_phase_at = ptr;
298}

References SnapBuild::two_phase_at.

Referenced by CreateDecodingContext().

◆ SnapBuildSnapDecRefcount()

void SnapBuildSnapDecRefcount ( Snapshot  snap)

Definition at line 328 of file snapbuild.c.

329{
330 /* make sure we don't get passed an external snapshot */
332
333 /* make sure nobody modified our snapshot */
334 Assert(snap->curcid == FirstCommandId);
335 Assert(!snap->suboverflowed);
337
338 Assert(snap->regd_count == 0);
339
340 Assert(snap->active_count > 0);
341
342 /* slightly more likely, so it's checked even without casserts */
343 if (snap->copied)
344 elog(ERROR, "cannot free a copied snapshot");
345
346 snap->active_count--;
347 if (snap->active_count == 0)
349}
#define FirstCommandId
Definition: c.h:687
static void SnapBuildFreeSnapshot(Snapshot snap)
Definition: snapbuild.c:252
@ SNAPSHOT_HISTORIC_MVCC
Definition: snapshot.h:105
bool copied
Definition: snapshot.h:181
uint32 regd_count
Definition: snapshot.h:201
uint32 active_count
Definition: snapshot.h:200
CommandId curcid
Definition: snapshot.h:183
bool suboverflowed
Definition: snapshot.h:178
bool takenDuringRecovery
Definition: snapshot.h:180

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().

◆ SnapBuildSnapshotExists()

bool SnapBuildSnapshotExists ( XLogRecPtr  lsn)

Definition at line 2057 of file snapbuild.c.

2058{
2059 char path[MAXPGPATH];
2060 int ret;
2061 struct stat stat_buf;
2062
2063 sprintf(path, "%s/%X-%X.snap",
2065 LSN_FORMAT_ARGS(lsn));
2066
2067 ret = stat(path, &stat_buf);
2068
2069 if (ret != 0 && errno != ENOENT)
2070 ereport(ERROR,
2072 errmsg("could not stat file \"%s\": %m", path)));
2073
2074 return ret == 0;
2075}
#define sprintf
Definition: port.h:262
#define stat
Definition: win32_port.h:274
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:47

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

Referenced by update_local_synced_slot().

◆ SnapBuildXactNeedsSkip()

bool SnapBuildXactNeedsSkip ( SnapBuild builder,
XLogRecPtr  ptr 
)

Definition at line 304 of file snapbuild.c.

305{
306 return ptr < builder->start_decoding_at;
307}

References SnapBuild::start_decoding_at.

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