PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 charSnapBuildExportSnapshot (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

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

Definition at line 188 of file snapbuild.c.

194{
195 MemoryContext context;
196 MemoryContext oldcontext;
197 SnapBuild *builder;
198
199 /* allocate memory in own context, to have better accountability */
201 "snapshot builder context",
203 oldcontext = MemoryContextSwitchTo(context);
204
205 builder = palloc0_object(SnapBuild);
206
207 builder->state = SNAPBUILD_START;
208 builder->context = context;
209 builder->reorder = reorder;
210 /* Other struct members initialized by zeroing via palloc0 above */
211
212 builder->committed.xcnt = 0;
213 builder->committed.xcnt_space = 128; /* arbitrary number */
214 builder->committed.xip =
216 builder->committed.includes_all_transactions = true;
217
218 builder->catchange.xcnt = 0;
219 builder->catchange.xip = NULL;
220
222 builder->start_decoding_at = start_lsn;
223 builder->in_slot_creation = in_slot_creation;
225 builder->two_phase_at = two_phase_at;
226
227 MemoryContextSwitchTo(oldcontext);
228
229 return builder;
230}
uint32 TransactionId
Definition c.h:720
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
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
static int fb(int x)
XLogRecPtr start_decoding_at
SnapBuildState state
TransactionId initial_xmin_horizon
TransactionId * xip
XLogRecPtr two_phase_at
struct SnapBuild::@122 catchange
struct SnapBuild::@119 committed
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, fb(), SnapBuild::in_slot_creation, SnapBuild::includes_all_transactions, SnapBuild::initial_xmin_horizon, MemoryContextSwitchTo(), palloc0_array, 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  )
extern

Definition at line 1973 of file snapbuild.c.

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

References AllocateDir(), DEBUG1, elog, ereport, errcode_for_file_access(), errmsg, fb(), 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)
extern

Definition at line 236 of file snapbuild.c.

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

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

Referenced by FreeDecodingContext().

◆ SnapBuildClearExportedSnapshot()

void SnapBuildClearExportedSnapshot ( void  )
extern

Definition at line 602 of file snapbuild.c.

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

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

Referenced by exec_replication_command().

◆ SnapBuildCommitTxn()

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

Definition at line 943 of file snapbuild.c.

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

◆ SnapBuildExportSnapshot()

const char * SnapBuildExportSnapshot ( SnapBuild builder)
extern

Definition at line 541 of file snapbuild.c.

542{
544 char *snapname;
545
547 elog(ERROR, "cannot export a snapshot from within a transaction");
548
550 elog(ERROR, "can only export one snapshot at a time");
551
553 ExportInProgress = true;
554
556
557 /* There doesn't seem to a nice API to set these */
559 XactReadOnly = true;
560
562
563 /*
564 * now that we've built a plain snapshot, make it active and use the
565 * normal mechanisms for exporting it
566 */
568
569 ereport(LOG,
570 (errmsg_plural("exported logical decoding snapshot: \"%s\" with %u transaction ID",
571 "exported logical decoding snapshot: \"%s\" with %u transaction IDs",
572 snap->xcnt,
573 snapname, snap->xcnt)));
574 return snapname;
575}
int int int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
Snapshot SnapBuildInitialSnapshot(SnapBuild *builder)
Definition snapbuild.c:443
char * ExportSnapshot(Snapshot snapshot)
Definition snapmgr.c:1115
bool IsTransactionOrTransactionBlock(void)
Definition xact.c:5012
bool XactReadOnly
Definition xact.c:84
void StartTransactionCommand(void)
Definition xact.c:3081
int XactIsoLevel
Definition xact.c:81
#define XACT_REPEATABLE_READ
Definition xact.h:38

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

Referenced by CreateReplicationSlot().

◆ SnapBuildGetOrBuildSnapshot()

Snapshot SnapBuildGetOrBuildSnapshot ( SnapBuild builder)
extern

Definition at line 581 of file snapbuild.c.

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

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

Referenced by logicalmsg_decode().

◆ SnapBuildGetTwoPhaseAt()

XLogRecPtr SnapBuildGetTwoPhaseAt ( SnapBuild builder)
extern

Definition at line 289 of file snapbuild.c.

290{
291 return builder->two_phase_at;
292}

References SnapBuild::two_phase_at.

Referenced by DecodeCommit().

◆ SnapBuildInitialSnapshot()

Snapshot SnapBuildInitialSnapshot ( SnapBuild builder)
extern

Definition at line 443 of file snapbuild.c.

444{
446 TransactionId xid;
449 int newxcnt = 0;
450
453
454 /* don't allow older snapshots */
455 InvalidateCatalogSnapshot(); /* about to overwrite MyProc->xmin */
457 elog(ERROR, "cannot build an initial slot snapshot when snapshots exist");
459
460 if (builder->state != SNAPBUILD_CONSISTENT)
461 elog(ERROR, "cannot build an initial slot snapshot before reaching a consistent state");
462
464 elog(ERROR, "cannot build an initial slot snapshot, not all transactions are monitored anymore");
465
466 /* so we don't overwrite the existing value */
468 elog(ERROR, "cannot build an initial slot snapshot when MyProc->xmin already is valid");
469
470 snap = SnapBuildBuildSnapshot(builder);
471
472 /*
473 * We know that snap->xmin is alive, enforced by the logical xmin
474 * mechanism. Due to that we can do this without locks, we're only
475 * changing our own value.
476 *
477 * Building an initial snapshot is expensive and an unenforced xmin
478 * horizon would have bad consequences, therefore always double-check that
479 * the horizon is enforced.
480 */
484
486 elog(ERROR, "cannot build an initial slot snapshot as oldest safe xid %u follows snapshot's xmin %u",
487 safeXid, snap->xmin);
488
489 MyProc->xmin = snap->xmin;
490
491 /* allocate in transaction context */
493
494 /*
495 * snapbuild.c builds transactions in an "inverted" manner, which means it
496 * stores committed transactions in ->xip, not ones in progress. Build a
497 * classical snapshot by marking all non-committed transactions as
498 * in-progress. This can be expensive.
499 */
500 for (xid = snap->xmin; NormalTransactionIdPrecedes(xid, snap->xmax);)
501 {
502 void *test;
503
504 /*
505 * Check whether transaction committed using the decoding snapshot
506 * meaning of ->xip.
507 */
508 test = bsearch(&xid, snap->xip, snap->xcnt,
510
511 if (test == NULL)
512 {
516 errmsg("initial slot snapshot too large")));
517
518 newxip[newxcnt++] = xid;
519 }
520
522 }
523
524 /* adjust remaining snapshot fields as needed */
525 snap->snapshot_type = SNAPSHOT_MVCC;
526 snap->xcnt = newxcnt;
527 snap->xip = newxip;
528
529 return snap;
530}
int errcode(int sqlerrcode)
Definition elog.c:874
#define palloc_array(type, count)
Definition fe_memutils.h:76
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1177
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
@ 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:2906
int GetMaxSnapshotXidCount(void)
Definition procarray.c:2016
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:68
TransactionId xmin
Definition proc.h:239
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, fb(), GetMaxSnapshotXidCount(), GetOldestSafeDecodingTransactionId(), HaveRegisteredOrActiveSnapshot(), HistoricSnapshotActive(), SnapBuild::includes_all_transactions, InvalidateCatalogSnapshot(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MyProc, NormalTransactionIdPrecedes, palloc_array, SNAPBUILD_CONSISTENT, SnapBuildBuildSnapshot(), SNAPSHOT_MVCC, SnapBuild::state, test(), TransactionIdAdvance, TransactionIdFollows(), TransactionIdIsValid, XACT_REPEATABLE_READ, XactIsoLevel, xidComparator(), and PGPROC::xmin.

Referenced by CreateReplicationSlot(), and SnapBuildExportSnapshot().

◆ SnapBuildProcessChange()

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

Definition at line 641 of file snapbuild.c.

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

References fb(), 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 
)
extern

Definition at line 691 of file snapbuild.c.

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

References elog, ERROR, fb(), InvalidCommandId, Max, SnapBuild::reorder, ReorderBufferAddNewCommandId(), ReorderBufferAddNewTupleCids(), and ReorderBufferXidSetCatalogChanges().

Referenced by heap2_decode().

◆ SnapBuildProcessRunningXacts()

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

Definition at line 1139 of file snapbuild.c.

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

Definition at line 629 of file snapbuild.c.

630{
632 ExportInProgress = false;
633}

References ExportInProgress, fb(), and SavedResourceOwnerDuringExport.

Referenced by AbortTransaction().

◆ SnapBuildSerializationPoint()

void SnapBuildSerializationPoint ( SnapBuild builder,
XLogRecPtr  lsn 
)
extern

Definition at line 1487 of file snapbuild.c.

1488{
1489 if (builder->state < SNAPBUILD_CONSISTENT)
1490 SnapBuildRestore(builder, lsn);
1491 else
1492 SnapBuildSerialize(builder, lsn);
1493}
static bool SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
Definition snapbuild.c:1844

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

Referenced by xlog_decode().

◆ SnapBuildSetTwoPhaseAt()

void SnapBuildSetTwoPhaseAt ( SnapBuild builder,
XLogRecPtr  ptr 
)
extern

Definition at line 298 of file snapbuild.c.

299{
300 builder->two_phase_at = ptr;
301}

References SnapBuild::two_phase_at.

Referenced by CreateDecodingContext().

◆ SnapBuildSnapDecRefcount()

void SnapBuildSnapDecRefcount ( Snapshot  snap)
extern

Definition at line 331 of file snapbuild.c.

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

References Assert, elog, ERROR, fb(), FirstCommandId, SnapBuildFreeSnapshot(), and SNAPSHOT_HISTORIC_MVCC.

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

◆ SnapBuildSnapshotExists()

bool SnapBuildSnapshotExists ( XLogRecPtr  lsn)
extern

Definition at line 2061 of file snapbuild.c.

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

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

Referenced by update_local_synced_slot().

◆ SnapBuildXactNeedsSkip()

bool SnapBuildXactNeedsSkip ( SnapBuild builder,
XLogRecPtr  ptr 
)
extern

Definition at line 307 of file snapbuild.c.

308{
309 return ptr < builder->start_decoding_at;
310}

References SnapBuild::start_decoding_at.

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