202#define SNAPSHOT_EXPORT_DIR "pg_snapshots"
225 .
name =
"snapshot reference",
306 "cannot take query snapshot during a parallel operation");
361 "cannot update SecondarySnapshot during a parallel operation");
510 int sourcepid,
PGPROC *sourceproc)
537 if (sourcesnap->
xcnt > 0)
562 if (sourceproc != NULL)
566 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
567 errmsg(
"could not import the requested snapshot"),
568 errdetail(
"The source transaction is not running anymore.")));
572 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
573 errmsg(
"could not import the requested snapshot"),
574 errdetail(
"The source process with PID %d is not running anymore.",
629 if (snapshot->
xcnt > 0)
632 memcpy(newsnap->
xip, snapshot->
xip,
762 elog(
ERROR,
"cannot modify commandid in active snapshot during a parallel operation");
866 if (snapshot == NULL)
879 if (snapshot == NULL)
1075 elog(
WARNING,
"registered snapshots seem to remain after cleanup");
1079 elog(
WARNING,
"snapshot %p still active", active);
1153 (
errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
1154 errmsg(
"cannot export a snapshot from a subtransaction")));
1219 for (
i = 0;
i < snapshot->
xcnt;
i++)
1237 for (
i = 0;
i < nchildren;
i++)
1248 snprintf(pathtmp,
sizeof(pathtmp),
"%s.tmp", path);
1252 errmsg(
"could not create file \"%s\": %m", pathtmp)));
1254 if (fwrite(
buf.data,
buf.len, 1, f) != 1)
1257 errmsg(
"could not write to file \"%s\": %m", pathtmp)));
1264 errmsg(
"could not write to file \"%s\": %m", pathtmp)));
1270 if (rename(pathtmp, path) < 0)
1273 errmsg(
"could not rename file \"%s\" to \"%s\": %m",
1308 int prefixlen = strlen(prefix);
1311 if (strncmp(ptr, prefix, prefixlen) != 0)
1313 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1316 if (sscanf(ptr,
"%d", &
val) != 1)
1318 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1320 ptr = strchr(ptr,
'\n');
1323 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1333 int prefixlen = strlen(prefix);
1336 if (strncmp(ptr, prefix, prefixlen) != 0)
1338 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1341 if (sscanf(ptr,
"%u", &
val) != 1)
1343 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1345 ptr = strchr(ptr,
'\n');
1348 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1359 int prefixlen = strlen(prefix);
1361 if (strncmp(ptr, prefix, prefixlen) != 0)
1363 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1368 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1370 ptr = strchr(ptr,
'\n');
1373 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1389 struct stat stat_buf;
1410 (
errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
1411 errmsg(
"SET TRANSACTION SNAPSHOT must be called before any query")));
1419 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1420 errmsg(
"a snapshot-importing transaction must have isolation level SERIALIZABLE or REPEATABLE READ")));
1426 if (strspn(idstr,
"0123456789ABCDEF-") != strlen(idstr))
1428 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1429 errmsg(
"invalid snapshot identifier: \"%s\"", idstr)));
1441 if (errno == ENOENT)
1443 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1444 errmsg(
"snapshot \"%s\" does not exist", idstr)));
1448 errmsg(
"could not open file \"%s\" for reading: %m",
1453 if (
fstat(fileno(f), &stat_buf))
1454 elog(
ERROR,
"could not stat file \"%s\": %m", path);
1458 if (fread(filebuf, stat_buf.
st_size, 1, f) != 1)
1459 elog(
ERROR,
"could not read file \"%s\": %m", path);
1461 filebuf[stat_buf.
st_size] =
'\0';
1468 memset(&snapshot, 0,
sizeof(snapshot));
1487 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1488 errmsg(
"invalid snapshot data in file \"%s\"", path)));
1491 for (
i = 0;
i < xcnt;
i++)
1503 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1504 errmsg(
"invalid snapshot data in file \"%s\"", path)));
1507 for (
i = 0;
i < xcnt;
i++)
1528 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1529 errmsg(
"invalid snapshot data in file \"%s\"", path)));
1541 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1542 errmsg(
"a serializable transaction cannot import a snapshot from a non-serializable transaction")));
1545 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1546 errmsg(
"a non-read-only serializable transaction cannot import a snapshot from a read-only transaction")));
1560 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1561 errmsg(
"cannot import a snapshot from a different database")));
1600 if (strcmp(s_de->
d_name,
".") == 0 ||
1601 strcmp(s_de->
d_name,
"..") == 0)
1606 if (unlink(
buf) != 0)
1609 errmsg(
"could not remove file \"%s\": %m",
buf)));
1669 Assert(historic_snapshot != NULL);
1741 serialized_snapshot.
xmin = snapshot->
xmin;
1742 serialized_snapshot.
xmax = snapshot->
xmax;
1743 serialized_snapshot.
xcnt = snapshot->
xcnt;
1755 serialized_snapshot.
subxcnt = 0;
1758 memcpy(start_address,
1762 if (snapshot->
xcnt > 0)
1773 if (serialized_snapshot.
subxcnt > 0)
1798 memcpy(&serialized_snapshot, start_address,
1811 snapshot->
xmin = serialized_snapshot.
xmin;
1812 snapshot->
xmax = serialized_snapshot.
xmax;
1813 snapshot->
xip = NULL;
1814 snapshot->
xcnt = serialized_snapshot.
xcnt;
1823 if (serialized_snapshot.
xcnt > 0)
1826 memcpy(snapshot->
xip, serialized_xids,
1831 if (serialized_snapshot.
subxcnt > 0)
1834 serialized_snapshot.
xcnt;
1835 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()