202#define SNAPSHOT_EXPORT_DIR "pg_snapshots"
225 .
name =
"snapshot reference",
279 elog(
ERROR,
"cannot take query snapshot during logical decoding");
295 "cannot take query snapshot during a parallel operation");
350 "cannot update SecondarySnapshot during a parallel operation");
499 int sourcepid,
PGPROC *sourceproc)
526 if (sourcesnap->
xcnt > 0)
551 if (sourceproc != NULL)
555 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
556 errmsg(
"could not import the requested snapshot"),
557 errdetail(
"The source transaction is not running anymore.")));
561 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
562 errmsg(
"could not import the requested snapshot"),
563 errdetail(
"The source process with PID %d is not running anymore.",
618 if (snapshot->
xcnt > 0)
621 memcpy(newsnap->
xip, snapshot->
xip,
751 elog(
ERROR,
"cannot modify commandid in active snapshot during a parallel operation");
855 if (snapshot == NULL)
868 if (snapshot == NULL)
1064 elog(
WARNING,
"registered snapshots seem to remain after cleanup");
1068 elog(
WARNING,
"snapshot %p still active", active);
1142 (
errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
1143 errmsg(
"cannot export a snapshot from a subtransaction")));
1208 for (
i = 0;
i < snapshot->
xcnt;
i++)
1226 for (
i = 0;
i < nchildren;
i++)
1237 snprintf(pathtmp,
sizeof(pathtmp),
"%s.tmp", path);
1241 errmsg(
"could not create file \"%s\": %m", pathtmp)));
1243 if (fwrite(
buf.data,
buf.len, 1, f) != 1)
1246 errmsg(
"could not write to file \"%s\": %m", pathtmp)));
1253 errmsg(
"could not write to file \"%s\": %m", pathtmp)));
1259 if (rename(pathtmp, path) < 0)
1262 errmsg(
"could not rename file \"%s\" to \"%s\": %m",
1297 int prefixlen = strlen(prefix);
1300 if (strncmp(ptr, prefix, prefixlen) != 0)
1302 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1305 if (sscanf(ptr,
"%d", &
val) != 1)
1307 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1309 ptr = strchr(ptr,
'\n');
1312 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1322 int prefixlen = strlen(prefix);
1325 if (strncmp(ptr, prefix, prefixlen) != 0)
1327 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1330 if (sscanf(ptr,
"%u", &
val) != 1)
1332 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1334 ptr = strchr(ptr,
'\n');
1337 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1348 int prefixlen = strlen(prefix);
1350 if (strncmp(ptr, prefix, prefixlen) != 0)
1352 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1357 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1359 ptr = strchr(ptr,
'\n');
1362 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1378 struct stat stat_buf;
1399 (
errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
1400 errmsg(
"SET TRANSACTION SNAPSHOT must be called before any query")));
1408 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1409 errmsg(
"a snapshot-importing transaction must have isolation level SERIALIZABLE or REPEATABLE READ")));
1415 if (strspn(idstr,
"0123456789ABCDEF-") != strlen(idstr))
1417 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1418 errmsg(
"invalid snapshot identifier: \"%s\"", idstr)));
1430 if (errno == ENOENT)
1432 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1433 errmsg(
"snapshot \"%s\" does not exist", idstr)));
1437 errmsg(
"could not open file \"%s\" for reading: %m",
1442 if (
fstat(fileno(f), &stat_buf))
1443 elog(
ERROR,
"could not stat file \"%s\": %m", path);
1447 if (fread(filebuf, stat_buf.
st_size, 1, f) != 1)
1448 elog(
ERROR,
"could not read file \"%s\": %m", path);
1450 filebuf[stat_buf.
st_size] =
'\0';
1457 memset(&snapshot, 0,
sizeof(snapshot));
1476 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1477 errmsg(
"invalid snapshot data in file \"%s\"", path)));
1480 for (
i = 0;
i < xcnt;
i++)
1492 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1493 errmsg(
"invalid snapshot data in file \"%s\"", path)));
1496 for (
i = 0;
i < xcnt;
i++)
1517 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1518 errmsg(
"invalid snapshot data in file \"%s\"", path)));
1530 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1531 errmsg(
"a serializable transaction cannot import a snapshot from a non-serializable transaction")));
1534 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1535 errmsg(
"a non-read-only serializable transaction cannot import a snapshot from a read-only transaction")));
1549 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1550 errmsg(
"cannot import a snapshot from a different database")));
1589 if (strcmp(s_de->
d_name,
".") == 0 ||
1590 strcmp(s_de->
d_name,
"..") == 0)
1595 if (unlink(
buf) != 0)
1598 errmsg(
"could not remove file \"%s\": %m",
buf)));
1658 Assert(historic_snapshot != NULL);
1730 serialized_snapshot.
xmin = snapshot->
xmin;
1731 serialized_snapshot.
xmax = snapshot->
xmax;
1732 serialized_snapshot.
xcnt = snapshot->
xcnt;
1744 serialized_snapshot.
subxcnt = 0;
1747 memcpy(start_address,
1751 if (snapshot->
xcnt > 0)
1762 if (serialized_snapshot.
subxcnt > 0)
1787 memcpy(&serialized_snapshot, start_address,
1800 snapshot->
xmin = serialized_snapshot.
xmin;
1801 snapshot->
xmax = serialized_snapshot.
xmax;
1802 snapshot->
xip = NULL;
1803 snapshot->
xcnt = serialized_snapshot.
xcnt;
1812 if (serialized_snapshot.
xcnt > 0)
1815 memcpy(snapshot->
xip, serialized_xids,
1820 if (serialized_snapshot.
subxcnt > 0)
1823 serialized_snapshot.
xcnt;
1824 memcpy(snapshot->
subxip, serialized_xids + serialized_snapshot.
xcnt,
#define OidIsValid(objectId)
int errcode_for_file_access(void)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
DIR * AllocateDir(const char *dirname)
FILE * AllocateFile(const char *name, const char *mode)
#define PG_RETURN_TEXT_P(x)
Assert(PointerIsAligned(start, uint64))
List * lappend(List *list, void *datum)
#define VirtualTransactionIdIsValid(vxid)
void * MemoryContextAlloc(MemoryContext context, Size size)
MemoryContext TopTransactionContext
char * pstrdup(const char *in)
void pfree(void *pointer)
void pairingheap_remove(pairingheap *heap, pairingheap_node *node)
void pairingheap_add(pairingheap *heap, pairingheap_node *node)
pairingheap_node * pairingheap_first(pairingheap *heap)
#define pairingheap_is_empty(h)
#define pairingheap_is_singular(h)
#define pairingheap_container(type, membername, ptr)
#define pairingheap_const_container(type, membername, ptr)
#define pairingheap_reset(h)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static bool pg_lfind32(uint32 key, const uint32 *base, uint32 nelem)
static int list_length(const List *l)
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
void SetSerializableTransactionSnapshot(Snapshot snapshot, VirtualTransactionId *sourcevxid, int sourcepid)
Snapshot GetSerializableTransactionSnapshot(Snapshot snapshot)
int GetMaxSnapshotSubxidCount(void)
Snapshot GetSnapshotData(Snapshot snapshot)
int GetMaxSnapshotXidCount(void)
bool ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc)
bool ProcArrayInstallImportedXmin(TransactionId xmin, VirtualTransactionId *sourcevxid)
ResourceOwner CurrentResourceOwner
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
void ResourceOwnerEnlarge(ResourceOwner owner)
#define RELEASE_PRIO_SNAPSHOT_REFS
@ RESOURCE_RELEASE_AFTER_LOCKS
Size add_size(Size s1, Size s2)
Size mul_size(Size s1, Size s2)
static Snapshot HistoricSnapshot
static Snapshot FirstXactSnapshot
static SnapshotData CatalogSnapshotData
void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner)
static void SetTransactionSnapshot(Snapshot sourcesnap, VirtualTransactionId *sourcevxid, int sourcepid, PGPROC *sourceproc)
void AtSubAbort_Snapshot(int level)
void SerializeSnapshot(Snapshot snapshot, char *start_address)
SnapshotData SnapshotSelfData
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
struct ActiveSnapshotElt ActiveSnapshotElt
static Snapshot CurrentSnapshot
static Snapshot SecondarySnapshot
bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
static List * exportedSnapshots
static pairingheap RegisteredSnapshots
static void UnregisterSnapshotNoOwner(Snapshot snapshot)
Snapshot GetTransactionSnapshot(void)
Snapshot GetLatestSnapshot(void)
void TeardownHistoricSnapshot(bool is_error)
Snapshot GetCatalogSnapshot(Oid relid)
void UnregisterSnapshot(Snapshot snapshot)
void PushActiveSnapshot(Snapshot snapshot)
static Snapshot CopySnapshot(Snapshot snapshot)
Snapshot RestoreSnapshot(char *start_address)
void AtSubCommit_Snapshot(int level)
static void ResourceOwnerRememberSnapshot(ResourceOwner owner, Snapshot snap)
void UpdateActiveSnapshotCommandId(void)
static void SnapshotResetXmin(void)
static int parseIntFromText(const char *prefix, char **s, const char *filename)
static SnapshotData SecondarySnapshotData
static int xmin_cmp(const pairingheap_node *a, const pairingheap_node *b, void *arg)
TransactionId TransactionXmin
SnapshotData SnapshotAnyData
bool HistoricSnapshotActive(void)
void ImportSnapshot(const char *idstr)
bool ActiveSnapshotSet(void)
Snapshot RegisterSnapshot(Snapshot snapshot)
bool XactHasExportedSnapshots(void)
static const ResourceOwnerDesc snapshot_resowner_desc
void DeleteAllExportedSnapshotFiles(void)
static void parseVxidFromText(const char *prefix, char **s, const char *filename, VirtualTransactionId *vxid)
static void FreeSnapshot(Snapshot snapshot)
#define SNAPSHOT_EXPORT_DIR
bool HaveRegisteredOrActiveSnapshot(void)
void InvalidateCatalogSnapshotConditionally(void)
static SnapshotData CurrentSnapshotData
SnapshotData SnapshotToastData
static void ResourceOwnerForgetSnapshot(ResourceOwner owner, Snapshot snap)
bool ThereAreNoPriorRegisteredSnapshots(void)
void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
void SnapshotSetCommandId(CommandId curcid)
struct SerializedSnapshotData SerializedSnapshotData
void PopActiveSnapshot(void)
void PushCopiedSnapshot(Snapshot snapshot)
char * ExportSnapshot(Snapshot snapshot)
Size EstimateSnapshotSpace(Snapshot snapshot)
static ActiveSnapshotElt * ActiveSnapshot
void SetupHistoricSnapshot(Snapshot historic_snapshot, HTAB *tuplecids)
static HTAB * tuplecid_data
Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner)
static TransactionId parseXidFromText(const char *prefix, char **s, const char *filename)
HTAB * HistoricSnapshotGetTupleCids(void)
void InvalidateCatalogSnapshot(void)
void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level)
Snapshot GetNonHistoricCatalogSnapshot(Oid relid)
struct ExportedSnapshot ExportedSnapshot
static Snapshot CatalogSnapshot
Snapshot GetActiveSnapshot(void)
Datum pg_export_snapshot(PG_FUNCTION_ARGS)
static void ResOwnerReleaseSnapshot(Datum res)
struct SnapshotData * Snapshot
struct SnapshotData SnapshotData
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoString(StringInfo str, const char *s)
void initStringInfo(StringInfo str)
struct ActiveSnapshotElt * as_next
uint64 snapXactCompletionCount
SnapshotType snapshot_type
LocalTransactionId localTransactionId
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
bool RelationHasSysCache(Oid relid)
bool RelationInvalidatesSnapshotsOnly(Oid relid)
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
#define InvalidTransactionId
#define FirstNormalTransactionId
#define TransactionIdIsValid(xid)
#define TransactionIdIsNormal(xid)
text * cstring_to_text(const char *s)
int GetCurrentTransactionNestLevel(void)
TransactionId GetTopTransactionIdIfAny(void)
bool IsSubTransaction(void)
bool IsInParallelMode(void)
int xactGetCommittedChildren(TransactionId **ptr)
CommandId GetCurrentCommandId(bool used)
#define XACT_SERIALIZABLE
#define IsolationUsesXactSnapshot()
#define IsolationIsSerializable()