#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "access/transam.h"
#include "pgstat.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
/* ----------------------------------------------------------------
#include "storage/freespace.h"
#include "storage/lmgr.h"
#include "utils/inval.h"
+#include "utils/snapmgmt.h"
/*
#include "access/slru.h"
#include "access/subtrans.h"
#include "access/transam.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
/*
#include "access/clog.h"
#include "access/subtrans.h"
#include "access/transam.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
/*
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/xml.h"
#include "pg_trace.h"
#include "storage/fd.h"
#include "utils/fmgroids.h"
#include "utils/relcache.h"
+#include "utils/tqual.h"
#define OIDCHARS 10 /* max chars printed by %u */
#include "utils/lsyscache.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
+#include "utils/snapmgmt.h"
static void AddNewRelationTuple(Relation pg_class_desc,
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "utils/tuplesort.h"
+#include "utils/snapmgmt.h"
/* state info for validate_index bulkdelete callback */
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/tuplesort.h"
+#include "utils/snapmgmt.h"
/* Hook for plugins to get control in ExplainOneQuery() */
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "executor/executor.h"
#include "tcop/pquery.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
/*
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
/*
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "utils/memutils.h"
#include "utils/pg_rusage.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "pgstat.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/syscache.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
#include "mb/pg_wchar.h"
/*
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "executor/nodeBitmapHeapscan.h"
#include "pgstat.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
static TupleTableSlot *BitmapHeapNext(BitmapHeapScanState *node);
#include "executor/spi_priv.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
#include "utils/typcache.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "catalog/catalog.h"
#include "miscadmin.h"
#include "access/twophase.h"
#include "miscadmin.h"
#include "storage/procarray.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
/* Our shared memory area */
#include "storage/large_object.h"
#include "utils/fmgroids.h"
#include "utils/resowner.h"
+#include "utils/snapmgmt.h"
/*
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/lsyscache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
+#include "utils/snapmgmt.h"
#include "mb/pg_wchar.h"
#include "pgstat.h"
#include "tcop/tcopprot.h"
#include "tcop/utility.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
/*
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
/* ----------
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
+#include "utils/snapmgmt.h"
#ifndef INT64_IS_BUSTED
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
+#include "utils/snapmgmt.h"
typedef struct
top_builddir = ../../../..
include $(top_builddir)/src/Makefile.global
-OBJS = combocid.o tqual.o
+OBJS = combocid.o tqual.o snapmgmt.o
include $(top_srcdir)/src/backend/common.mk
--- /dev/null
+/*-------------------------------------------------------------------------
+ * snapmgmt.c
+ * PostgreSQL snapshot management code.
+ *
+ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * $PostgreSQL$
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/xact.h"
+#include "access/transam.h"
+#include "storage/procarray.h"
+#include "utils/snapmgmt.h"
+#include "utils/tqual.h"
+
+
+/*
+ * These SnapshotData structs are static to simplify memory allocation
+ * (see the hack in GetSnapshotData to avoid repeated malloc/free).
+ */
+static SnapshotData SerializableSnapshotData = {HeapTupleSatisfiesMVCC};
+static SnapshotData LatestSnapshotData = {HeapTupleSatisfiesMVCC};
+
+/* Externally visible pointers to valid snapshots: */
+Snapshot SerializableSnapshot = NULL;
+Snapshot LatestSnapshot = NULL;
+
+/*
+ * This pointer is not maintained by this module, but it's convenient
+ * to declare it here anyway. Callers typically assign a copy of
+ * GetTransactionSnapshot's result to ActiveSnapshot.
+ */
+Snapshot ActiveSnapshot = NULL;
+
+/*
+ * These are updated by GetSnapshotData. We initialize them this way
+ * for the convenience of TransactionIdIsInProgress: even in bootstrap
+ * mode, we don't want it to say that BootstrapTransactionId is in progress.
+ */
+TransactionId TransactionXmin = FirstNormalTransactionId;
+TransactionId RecentXmin = FirstNormalTransactionId;
+TransactionId RecentGlobalXmin = FirstNormalTransactionId;
+
+
+/*
+ * GetTransactionSnapshot
+ * Get the appropriate snapshot for a new query in a transaction.
+ *
+ * The SerializableSnapshot is the first one taken in a transaction.
+ * In serializable mode we just use that one throughout the transaction.
+ * In read-committed mode, we take a new snapshot each time we are called.
+ *
+ * Note that the return value points at static storage that will be modified
+ * by future calls and by CommandCounterIncrement(). Callers should copy
+ * the result with CopySnapshot() if it is to be used very long.
+ */
+Snapshot
+GetTransactionSnapshot(void)
+{
+ /* First call in transaction? */
+ if (SerializableSnapshot == NULL)
+ {
+ SerializableSnapshot = GetSnapshotData(&SerializableSnapshotData, true);
+ return SerializableSnapshot;
+ }
+
+ if (IsXactIsoLevelSerializable)
+ return SerializableSnapshot;
+
+ LatestSnapshot = GetSnapshotData(&LatestSnapshotData, false);
+
+ return LatestSnapshot;
+}
+
+/*
+ * GetLatestSnapshot
+ * Get a snapshot that is up-to-date as of the current instant,
+ * even if we are executing in SERIALIZABLE mode.
+ */
+Snapshot
+GetLatestSnapshot(void)
+{
+ /* Should not be first call in transaction */
+ if (SerializableSnapshot == NULL)
+ elog(ERROR, "no snapshot has been set");
+
+ LatestSnapshot = GetSnapshotData(&LatestSnapshotData, false);
+
+ return LatestSnapshot;
+}
+
+/*
+ * CopySnapshot
+ * Copy the given snapshot.
+ *
+ * The copy is palloc'd in the current memory context.
+ */
+Snapshot
+CopySnapshot(Snapshot snapshot)
+{
+ Snapshot newsnap;
+ Size subxipoff;
+ Size size;
+
+ /* We allocate any XID arrays needed in the same palloc block. */
+ size = subxipoff = sizeof(SnapshotData) +
+ snapshot->xcnt * sizeof(TransactionId);
+ if (snapshot->subxcnt > 0)
+ size += snapshot->subxcnt * sizeof(TransactionId);
+
+ newsnap = (Snapshot) palloc(size);
+ memcpy(newsnap, snapshot, sizeof(SnapshotData));
+
+ /* setup XID array */
+ if (snapshot->xcnt > 0)
+ {
+ newsnap->xip = (TransactionId *) (newsnap + 1);
+ memcpy(newsnap->xip, snapshot->xip,
+ snapshot->xcnt * sizeof(TransactionId));
+ }
+ else
+ newsnap->xip = NULL;
+
+ /* setup subXID array */
+ if (snapshot->subxcnt > 0)
+ {
+ newsnap->subxip = (TransactionId *) ((char *) newsnap + subxipoff);
+ memcpy(newsnap->subxip, snapshot->subxip,
+ snapshot->subxcnt * sizeof(TransactionId));
+ }
+ else
+ newsnap->subxip = NULL;
+
+ return newsnap;
+}
+
+/*
+ * FreeSnapshot
+ * Free a snapshot previously copied with CopySnapshot.
+ *
+ * This is currently identical to pfree, but is provided for cleanliness.
+ *
+ * Do *not* apply this to the results of GetTransactionSnapshot or
+ * GetLatestSnapshot, since those are just static structs.
+ */
+void
+FreeSnapshot(Snapshot snapshot)
+{
+ pfree(snapshot);
+}
+
+/*
+ * FreeXactSnapshot
+ * Free snapshot(s) at end of transaction.
+ */
+void
+FreeXactSnapshot(void)
+{
+ /*
+ * We do not free the xip arrays for the static snapshot structs; they
+ * will be reused soon. So this is now just a state change to prevent
+ * outside callers from accessing the snapshots.
+ */
+ SerializableSnapshot = NULL;
+ LatestSnapshot = NULL;
+ ActiveSnapshot = NULL; /* just for cleanliness */
+}
SnapshotData SnapshotAnyData = {HeapTupleSatisfiesAny};
SnapshotData SnapshotToastData = {HeapTupleSatisfiesToast};
-/*
- * These SnapshotData structs are static to simplify memory allocation
- * (see the hack in GetSnapshotData to avoid repeated malloc/free).
- */
-static SnapshotData SerializableSnapshotData = {HeapTupleSatisfiesMVCC};
-static SnapshotData LatestSnapshotData = {HeapTupleSatisfiesMVCC};
-
-/* Externally visible pointers to valid snapshots: */
-Snapshot SerializableSnapshot = NULL;
-Snapshot LatestSnapshot = NULL;
-
-/*
- * This pointer is not maintained by this module, but it's convenient
- * to declare it here anyway. Callers typically assign a copy of
- * GetTransactionSnapshot's result to ActiveSnapshot.
- */
-Snapshot ActiveSnapshot = NULL;
-
-/*
- * These are updated by GetSnapshotData. We initialize them this way
- * for the convenience of TransactionIdIsInProgress: even in bootstrap
- * mode, we don't want it to say that BootstrapTransactionId is in progress.
- */
-TransactionId TransactionXmin = FirstNormalTransactionId;
-TransactionId RecentXmin = FirstNormalTransactionId;
-TransactionId RecentGlobalXmin = FirstNormalTransactionId;
-
/* local functions */
static bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
}
-/*
- * GetTransactionSnapshot
- * Get the appropriate snapshot for a new query in a transaction.
- *
- * The SerializableSnapshot is the first one taken in a transaction.
- * In serializable mode we just use that one throughout the transaction.
- * In read-committed mode, we take a new snapshot each time we are called.
- *
- * Note that the return value points at static storage that will be modified
- * by future calls and by CommandCounterIncrement(). Callers should copy
- * the result with CopySnapshot() if it is to be used very long.
- */
-Snapshot
-GetTransactionSnapshot(void)
-{
- /* First call in transaction? */
- if (SerializableSnapshot == NULL)
- {
- SerializableSnapshot = GetSnapshotData(&SerializableSnapshotData, true);
- return SerializableSnapshot;
- }
-
- if (IsXactIsoLevelSerializable)
- return SerializableSnapshot;
-
- LatestSnapshot = GetSnapshotData(&LatestSnapshotData, false);
-
- return LatestSnapshot;
-}
-
-/*
- * GetLatestSnapshot
- * Get a snapshot that is up-to-date as of the current instant,
- * even if we are executing in SERIALIZABLE mode.
- */
-Snapshot
-GetLatestSnapshot(void)
-{
- /* Should not be first call in transaction */
- if (SerializableSnapshot == NULL)
- elog(ERROR, "no snapshot has been set");
-
- LatestSnapshot = GetSnapshotData(&LatestSnapshotData, false);
-
- return LatestSnapshot;
-}
-
-/*
- * CopySnapshot
- * Copy the given snapshot.
- *
- * The copy is palloc'd in the current memory context.
- */
-Snapshot
-CopySnapshot(Snapshot snapshot)
-{
- Snapshot newsnap;
- Size subxipoff;
- Size size;
-
- /* We allocate any XID arrays needed in the same palloc block. */
- size = subxipoff = sizeof(SnapshotData) +
- snapshot->xcnt * sizeof(TransactionId);
- if (snapshot->subxcnt > 0)
- size += snapshot->subxcnt * sizeof(TransactionId);
-
- newsnap = (Snapshot) palloc(size);
- memcpy(newsnap, snapshot, sizeof(SnapshotData));
-
- /* setup XID array */
- if (snapshot->xcnt > 0)
- {
- newsnap->xip = (TransactionId *) (newsnap + 1);
- memcpy(newsnap->xip, snapshot->xip,
- snapshot->xcnt * sizeof(TransactionId));
- }
- else
- newsnap->xip = NULL;
-
- /* setup subXID array */
- if (snapshot->subxcnt > 0)
- {
- newsnap->subxip = (TransactionId *) ((char *) newsnap + subxipoff);
- memcpy(newsnap->subxip, snapshot->subxip,
- snapshot->subxcnt * sizeof(TransactionId));
- }
- else
- newsnap->subxip = NULL;
-
- return newsnap;
-}
-
-/*
- * FreeSnapshot
- * Free a snapshot previously copied with CopySnapshot.
- *
- * This is currently identical to pfree, but is provided for cleanliness.
- *
- * Do *not* apply this to the results of GetTransactionSnapshot or
- * GetLatestSnapshot, since those are just static structs.
- */
-void
-FreeSnapshot(Snapshot snapshot)
-{
- pfree(snapshot);
-}
-
-/*
- * FreeXactSnapshot
- * Free snapshot(s) at end of transaction.
- */
-void
-FreeXactSnapshot(void)
-{
- /*
- * We do not free the xip arrays for the static snapshot structs; they
- * will be reused soon. So this is now just a state change to prevent
- * outside callers from accessing the snapshots.
- */
- SerializableSnapshot = NULL;
- LatestSnapshot = NULL;
- ActiveSnapshot = NULL; /* just for cleanliness */
-}
-
/*
* XidInMVCCSnapshot
* Is the given XID still-in-progress according to the snapshot?
#ifndef RELSCAN_H
#define RELSCAN_H
+#include "access/htup.h"
#include "access/skey.h"
#include "storage/bufpage.h"
-#include "utils/tqual.h"
+#include "utils/snapshot.h"
typedef struct HeapScanDescData
#ifndef LARGE_OBJECT_H
#define LARGE_OBJECT_H
-#include "utils/tqual.h"
+#include "utils/snapshot.h"
/*----------
#define PROCARRAY_H
#include "storage/lock.h"
+#include "utils/snapshot.h"
extern Size ProcArrayShmemSize(void);
extern void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid);
extern void ProcArrayClearTransaction(PGPROC *proc);
+extern Snapshot GetSnapshotData(Snapshot snapshot, bool serializable);
+
extern bool TransactionIdIsInProgress(TransactionId xid);
extern bool TransactionIdIsActive(TransactionId xid);
extern TransactionId GetOldestXmin(bool allDbs, bool ignoreVacuum);
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * snapmgmt.h
+ * POSTGRES snapshot management definitions
+ *
+ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * $PostgreSQL$
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef SNAPMGMT_H
+#define SNAPMGMT_H
+
+#include "utils/snapshot.h"
+
+
+extern PGDLLIMPORT Snapshot SerializableSnapshot;
+extern PGDLLIMPORT Snapshot LatestSnapshot;
+extern PGDLLIMPORT Snapshot ActiveSnapshot;
+
+extern TransactionId TransactionXmin;
+extern TransactionId RecentXmin;
+extern TransactionId RecentGlobalXmin;
+
+extern Snapshot GetTransactionSnapshot(void);
+extern Snapshot GetLatestSnapshot(void);
+extern Snapshot CopySnapshot(Snapshot snapshot);
+extern void FreeSnapshot(Snapshot snapshot);
+extern void FreeXactSnapshot(void);
+
+#endif /* SNAPMGMT_H */
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * snapshot.h
+ * POSTGRES snapshot definition
+ *
+ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * $PostgreSQL$
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef SNAPSHOT_H
+#define SNAPSHOT_H
+
+#include "access/htup.h"
+#include "storage/buf.h"
+
+
+typedef struct SnapshotData *Snapshot;
+
+#define InvalidSnapshot ((Snapshot) NULL)
+
+/*
+ * We use SnapshotData structures to represent both "regular" (MVCC)
+ * snapshots and "special" snapshots that have non-MVCC semantics.
+ * The specific semantics of a snapshot are encoded by the "satisfies"
+ * function.
+ */
+typedef bool (*SnapshotSatisfiesFunc) (HeapTupleHeader tuple,
+ Snapshot snapshot, Buffer buffer);
+
+typedef struct SnapshotData
+{
+ SnapshotSatisfiesFunc satisfies; /* tuple test function */
+
+ /*
+ * The remaining fields are used only for MVCC snapshots, and are normally
+ * just zeroes in special snapshots. (But xmin and xmax are used
+ * specially by HeapTupleSatisfiesDirty.)
+ *
+ * An MVCC snapshot can never see the effects of XIDs >= xmax. It can see
+ * the effects of all older XIDs except those listed in the snapshot. xmin
+ * is stored as an optimization to avoid needing to search the XID arrays
+ * for most tuples.
+ */
+ TransactionId xmin; /* all XID < xmin are visible to me */
+ TransactionId xmax; /* all XID >= xmax are invisible to me */
+ uint32 xcnt; /* # of xact ids in xip[] */
+ TransactionId *xip; /* array of xact IDs in progress */
+ /* note: all ids in xip[] satisfy xmin <= xip[i] < xmax */
+ int32 subxcnt; /* # of xact ids in subxip[], -1 if overflow */
+ TransactionId *subxip; /* array of subxact IDs in progress */
+
+ /*
+ * note: all ids in subxip[] are >= xmin, but we don't bother filtering
+ * out any that are >= xmax
+ */
+ CommandId curcid; /* in my xact, CID < curcid are visible */
+} SnapshotData;
+
+#endif /* SNAPSHOT_H */
#ifndef TQUAL_H
#define TQUAL_H
-#include "access/htup.h"
-#include "storage/buf.h"
+#include "utils/snapshot.h"
-/*
- * We use SnapshotData structures to represent both "regular" (MVCC)
- * snapshots and "special" snapshots that have non-MVCC semantics.
- * The specific semantics of a snapshot are encoded by the "satisfies"
- * function.
- */
-typedef struct SnapshotData *Snapshot;
-
-typedef bool (*SnapshotSatisfiesFunc) (HeapTupleHeader tuple,
- Snapshot snapshot, Buffer buffer);
-
-typedef struct SnapshotData
-{
- SnapshotSatisfiesFunc satisfies; /* tuple test function */
-
- /*
- * The remaining fields are used only for MVCC snapshots, and are normally
- * just zeroes in special snapshots. (But xmin and xmax are used
- * specially by HeapTupleSatisfiesDirty.)
- *
- * An MVCC snapshot can never see the effects of XIDs >= xmax. It can see
- * the effects of all older XIDs except those listed in the snapshot. xmin
- * is stored as an optimization to avoid needing to search the XID arrays
- * for most tuples.
- */
- TransactionId xmin; /* all XID < xmin are visible to me */
- TransactionId xmax; /* all XID >= xmax are invisible to me */
- uint32 xcnt; /* # of xact ids in xip[] */
- TransactionId *xip; /* array of xact IDs in progress */
- /* note: all ids in xip[] satisfy xmin <= xip[i] < xmax */
- int32 subxcnt; /* # of xact ids in subxip[], -1 if overflow */
- TransactionId *subxip; /* array of subxact IDs in progress */
-
- /*
- * note: all ids in subxip[] are >= xmin, but we don't bother filtering
- * out any that are >= xmax
- */
- CommandId curcid; /* in my xact, CID < curcid are visible */
-} SnapshotData;
-
-#define InvalidSnapshot ((Snapshot) NULL)
-
/* Static variables representing various special snapshot semantics */
extern PGDLLIMPORT SnapshotData SnapshotNowData;
extern PGDLLIMPORT SnapshotData SnapshotSelfData;
#define IsMVCCSnapshot(snapshot) \
((snapshot)->satisfies == HeapTupleSatisfiesMVCC)
-
-extern PGDLLIMPORT Snapshot SerializableSnapshot;
-extern PGDLLIMPORT Snapshot LatestSnapshot;
-extern PGDLLIMPORT Snapshot ActiveSnapshot;
-
-extern TransactionId TransactionXmin;
-extern TransactionId RecentXmin;
-extern TransactionId RecentGlobalXmin;
-
/*
* HeapTupleSatisfiesVisibility
* True iff heap tuple satisfies a time qual.
extern void HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer,
uint16 infomask, TransactionId xid);
-extern Snapshot GetTransactionSnapshot(void);
-extern Snapshot GetLatestSnapshot(void);
-extern Snapshot CopySnapshot(Snapshot snapshot);
-extern void FreeSnapshot(Snapshot snapshot);
-extern void FreeXactSnapshot(void);
-
-/* in procarray.c; declared here to avoid including tqual.h in procarray.h: */
-extern Snapshot GetSnapshotData(Snapshot snapshot, bool serializable);
-
#endif /* TQUAL_H */
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
#include "utils/typcache.h"