Separate snapshot management code from tuple visibility code, create a
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Wed, 26 Mar 2008 16:20:48 +0000 (16:20 +0000)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Wed, 26 Mar 2008 16:20:48 +0000 (16:20 +0000)
snapmgmt.c file for the former.  The header files have also been reorganized
in three parts: the most basic snapshot definitions are now in a new file
snapshot.h, and the also new snapmgmt.h keeps the definitions for snapmgmt.c.
tqual.h has been reduced to the bare minimum.

This patch is just a first step towards managing live snapshots within a
transaction; there is no functionality change.

Per my proposal to pgsql-patches on 20080318191940.GB27458@alvh.no-ip.org and
subsequent discussion.

41 files changed:
src/backend/access/heap/heapam.c
src/backend/access/index/indexam.c
src/backend/access/nbtree/nbtpage.c
src/backend/access/transam/subtrans.c
src/backend/access/transam/transam.c
src/backend/access/transam/xact.c
src/backend/catalog/catalog.c
src/backend/catalog/heap.c
src/backend/catalog/index.c
src/backend/commands/cluster.c
src/backend/commands/copy.c
src/backend/commands/explain.c
src/backend/commands/indexcmds.c
src/backend/commands/portalcmds.c
src/backend/commands/prepare.c
src/backend/commands/tablecmds.c
src/backend/commands/trigger.c
src/backend/commands/vacuum.c
src/backend/commands/variable.c
src/backend/executor/functions.c
src/backend/executor/nodeBitmapHeapscan.c
src/backend/executor/spi.c
src/backend/optimizer/util/plancat.c
src/backend/storage/ipc/procarray.c
src/backend/storage/large_object/inv_api.c
src/backend/tcop/fastpath.c
src/backend/tcop/postgres.c
src/backend/tcop/pquery.c
src/backend/utils/adt/ri_triggers.c
src/backend/utils/adt/txid.c
src/backend/utils/cache/plancache.c
src/backend/utils/time/Makefile
src/backend/utils/time/snapmgmt.c [new file with mode: 0644]
src/backend/utils/time/tqual.c
src/include/access/relscan.h
src/include/storage/large_object.h
src/include/storage/procarray.h
src/include/utils/snapmgmt.h [new file with mode: 0644]
src/include/utils/snapshot.h [new file with mode: 0644]
src/include/utils/tqual.h
src/pl/plpgsql/src/pl_exec.c

index 6f43060adcb77aab080babd2dbff6a168c43d156..e165446f362149085951f45328873bfdf1e08e96 100644 (file)
@@ -56,6 +56,7 @@
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
 #include "utils/relcache.h"
+#include "utils/snapmgmt.h"
 #include "utils/syscache.h"
 
 
index 3310bfc6adb2db265a9b07b6e04584017befb5bc..d4f3ccf2ac2a445d7dbb2ec14725c563d8a0b426 100644 (file)
@@ -67,6 +67,7 @@
 #include "access/transam.h"
 #include "pgstat.h"
 #include "utils/relcache.h"
+#include "utils/snapmgmt.h"
 
 
 /* ----------------------------------------------------------------
index d833ae16f6ea42f8861cc1c37f53c51cce988c24..ceb44ed5726c0afae35bbeb170dc260c23d29216 100644 (file)
@@ -28,6 +28,7 @@
 #include "storage/freespace.h"
 #include "storage/lmgr.h"
 #include "utils/inval.h"
+#include "utils/snapmgmt.h"
 
 
 /*
index 03ae4efbcc03ca01baca336708a224c179272347..d182d3a319e01d177c6bde25cb84494193e23b9f 100644 (file)
@@ -31,7 +31,7 @@
 #include "access/slru.h"
 #include "access/subtrans.h"
 #include "access/transam.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
 
 
 /*
index c83e4a8237d2d78485703f7f48aa3395c8c05def..58d81fead38f8c6020a17b248afe3eb23f23fedc 100644 (file)
@@ -22,7 +22,7 @@
 #include "access/clog.h"
 #include "access/subtrans.h"
 #include "access/transam.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
 
 
 /*
index 16f51799a2e9f42c08d61e272a94bd7ff0304cd0..f28027b9cee5249390ecc19c1481f2096dfe4ca6 100644 (file)
@@ -45,6 +45,7 @@
 #include "utils/inval.h"
 #include "utils/memutils.h"
 #include "utils/relcache.h"
+#include "utils/snapmgmt.h"
 #include "utils/xml.h"
 #include "pg_trace.h"
 
index 8acc0013fbee077545346662ac03fefdc53a20b0..32a1a8b2a93bcc4b0e2ca1ba3ebf474d29890f50 100644 (file)
@@ -38,6 +38,7 @@
 #include "storage/fd.h"
 #include "utils/fmgroids.h"
 #include "utils/relcache.h"
+#include "utils/tqual.h"
 
 
 #define OIDCHARS       10                      /* max chars printed by %u */
index 5c253b8bf01676e3c21ee8a60779b5c2395b6cc9..365f6b121e342e3a21076a1640cdc4b999023eb3 100644 (file)
@@ -60,6 +60,7 @@
 #include "utils/lsyscache.h"
 #include "utils/relcache.h"
 #include "utils/syscache.h"
+#include "utils/snapmgmt.h"
 
 
 static void AddNewRelationTuple(Relation pg_class_desc,
index f30c5b32a3738a168b93be4a00edde16076a4293..5d7f265e6de79896515efd26b36dd1147543a032 100644 (file)
@@ -55,6 +55,7 @@
 #include "utils/relcache.h"
 #include "utils/syscache.h"
 #include "utils/tuplesort.h"
+#include "utils/snapmgmt.h"
 
 
 /* state info for validate_index bulkdelete callback */
index 56e8579e076d182ec0a36a832536b30d73389110..2cb1938a00f1e10b858d80ce4d53ec25bb13b0a8 100644 (file)
@@ -41,6 +41,7 @@
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/relcache.h"
+#include "utils/snapmgmt.h"
 #include "utils/syscache.h"
 
 
index b24973f8a0a37dc5d34d3c316ff5e56ef6143285..b4aa68f391e60afc452b7ebea823644e73ca18ca 100644 (file)
@@ -40,6 +40,7 @@
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 
 
 #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
index cd1cb047ce3cf2710cde703089075e342cba729a..b03b1b16ee5e25d15dec907888c97e710348ce07 100644 (file)
@@ -31,6 +31,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() */
index f39a4e84aaba6df38f3c57e12a8095b0b10442f8..4b9722c5304aa192d4385b632389374f121f2ac4 100644 (file)
@@ -47,6 +47,7 @@
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/relcache.h"
+#include "utils/snapmgmt.h"
 #include "utils/syscache.h"
 
 
index 73c042ea6823e08360a7370d2152d8cb5b2847e9..1dadcff964e45063ef1179f78e816c0ae5f2eddb 100644 (file)
@@ -28,6 +28,7 @@
 #include "executor/executor.h"
 #include "tcop/pquery.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 
 
 /*
index 64fdc2ff85540534911da83eece67bd1cb6ed163..0a135efa2e4940f88b68e0f5e684ea02f7b9175b 100644 (file)
@@ -32,6 +32,7 @@
 #include "tcop/utility.h"
 #include "utils/builtins.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 
 
 /*
index 2fda398ce6e02b308f6a3841fc48c923d7293990..8e08cf0b896e68b833783d7f2b0d5ef65c234531 100644 (file)
@@ -65,6 +65,7 @@
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/relcache.h"
+#include "utils/snapmgmt.h"
 #include "utils/syscache.h"
 
 
index 3f0b829d7d3663a9522b9b99e88559bf96ffb33c..fd27367cd85e89d707a86e5747128eb514777078 100644 (file)
@@ -38,6 +38,7 @@
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 #include "utils/syscache.h"
 
 
index 3e9b2a228682c8cc0a3b7fa17e0103f332833ed5..97bfb1254e5e447035dd9995b84cd962a2e2a86d 100644 (file)
@@ -48,6 +48,7 @@
 #include "utils/memutils.h"
 #include "utils/pg_rusage.h"
 #include "utils/relcache.h"
+#include "utils/snapmgmt.h"
 #include "utils/syscache.h"
 #include "pgstat.h"
 
index aca1f77a95e0c3bb8b020aabc1592f514f52ead3..1699752a04224c914e635c0c932746133a2169f1 100644 (file)
@@ -25,7 +25,7 @@
 #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"
 
 /*
index f31470486223499e4ea96004a8134a6de443c675..e686bf122f48729e6795420aa1d693026343d004 100644 (file)
@@ -28,6 +28,7 @@
 #include "utils/builtins.h"
 #include "utils/datum.h"
 #include "utils/lsyscache.h"
+#include "utils/snapmgmt.h"
 #include "utils/syscache.h"
 #include "utils/typcache.h"
 
index b7f41fc55256fa85c8c4e0d42b672cfc4251c817..eba4eae0440d2434b75df813fa383cdd84db16a3 100644 (file)
@@ -40,6 +40,7 @@
 #include "executor/nodeBitmapHeapscan.h"
 #include "pgstat.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 
 
 static TupleTableSlot *BitmapHeapNext(BitmapHeapScanState *node);
index e5370d0ceec0a62082a42f9b6b8642f2db1de50b..b081b82046a77854bf028e72497a3537a3f7f41a 100644 (file)
@@ -20,6 +20,7 @@
 #include "executor/spi_priv.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 #include "utils/typcache.h"
 
 
index c04a4c03e79fc1bc06329bcf9888c0fc2d262f43..e4ed6af6cda4018ae1dece3bb542046b04ec5963 100644 (file)
@@ -33,6 +33,7 @@
 #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"
index 5927f15a6e8b426752c7df4e6d1657d6255d24e4..c69a9675b64586df0a657f116b382c91ef8a1c3c 100644 (file)
@@ -37,7 +37,7 @@
 #include "access/twophase.h"
 #include "miscadmin.h"
 #include "storage/procarray.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
 
 
 /* Our shared memory area */
index 80a4048d7f13d541691b73159f946ae57a48ea7e..9696907660ab347734481afa7bb3fa217a971492 100644 (file)
@@ -42,6 +42,7 @@
 #include "storage/large_object.h"
 #include "utils/fmgroids.h"
 #include "utils/resowner.h"
+#include "utils/snapmgmt.h"
 
 
 /*
index bef863fbc502cfa08c616547b87be096b14db918..8e85587faf5aa473aca5ed0304ec3fb51fa4b4af 100644 (file)
@@ -30,6 +30,7 @@
 #include "tcop/tcopprot.h"
 #include "utils/acl.h"
 #include "utils/lsyscache.h"
+#include "utils/snapmgmt.h"
 #include "utils/syscache.h"
 
 
index e4b8cb8165e570a02183f1adcb858f7406b13b4c..d7645910dfce0e52af43fb8bde2327fa0b197893 100644 (file)
@@ -66,6 +66,7 @@
 #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"
index 152701422a18a0bb8e30b36a502860436e173e21..7e59195626fc0718755ad36d02f948d5604b1a90 100644 (file)
@@ -23,6 +23,7 @@
 #include "tcop/tcopprot.h"
 #include "tcop/utility.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 
 
 /*
index 9d81c09f803bb046938c2ec14899e14d1c49cc0a..d77dc7fdfa7717fd5bb549f6c793ad55f86c6803 100644 (file)
@@ -41,6 +41,7 @@
 #include "utils/fmgroids.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 
 
 /* ----------
index d4b148f6021858eae2b65595c878d8692cca8d85..a0616d0af9cdae0e3d333b2537c07d37c0817f47 100644 (file)
@@ -26,6 +26,7 @@
 #include "funcapi.h"
 #include "libpq/pqformat.h"
 #include "utils/builtins.h"
+#include "utils/snapmgmt.h"
 
 
 #ifndef INT64_IS_BUSTED
index 6ff34c085f31269a8677ab2a35017131a8fcf3f6..d1ff90e646a1e6bd7548baf7a03d6a7ba10bc679 100644 (file)
@@ -51,6 +51,7 @@
 #include "utils/inval.h"
 #include "utils/memutils.h"
 #include "utils/resowner.h"
+#include "utils/snapmgmt.h"
 
 
 typedef struct
index 4e18b77e32800f22d67c72be62ab4419a6ba11a1..83b451a5fb35a1ebc0ff8007d88090c23968aec4 100644 (file)
@@ -12,6 +12,6 @@ subdir = src/backend/utils/time
 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
diff --git a/src/backend/utils/time/snapmgmt.c b/src/backend/utils/time/snapmgmt.c
new file mode 100644 (file)
index 0000000..9ad65ae
--- /dev/null
@@ -0,0 +1,172 @@
+/*-------------------------------------------------------------------------
+ * 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 */
+}
index aaead5ef45d15e0f9764ee7bf760a7b031b139f9..a891caa6085f57be8985d77dfab95e2cf994e0d7 100644 (file)
@@ -53,33 +53,6 @@ SnapshotData SnapshotSelfData = {HeapTupleSatisfiesSelf};
 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);
 
@@ -1235,130 +1208,6 @@ HeapTupleSatisfiesVacuum(HeapTupleHeader tuple, TransactionId OldestXmin,
 }
 
 
-/*
- * 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?
index 356f6c3d4285b504dc4c26a2c236f7218389bfd3..fba6dac60446a44d94b5bb28850949044a201a77 100644 (file)
 #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
index 2e07c3a7bd9a08b97756cdd04a0b1af1dee0aedc..fa6c99f75709b84f355005d72bb7ccffe14660a2 100644 (file)
@@ -15,7 +15,7 @@
 #ifndef LARGE_OBJECT_H
 #define LARGE_OBJECT_H
 
-#include "utils/tqual.h"
+#include "utils/snapshot.h"
 
 
 /*----------
index 567ea6465440fdd8c7b84761e84bfb73e9f8bbea..06e355706b669dad4b30ec87acbf55cb856ec1d1 100644 (file)
@@ -15,6 +15,7 @@
 #define PROCARRAY_H
 
 #include "storage/lock.h"
+#include "utils/snapshot.h"
 
 
 extern Size ProcArrayShmemSize(void);
@@ -25,6 +26,8 @@ extern void ProcArrayRemove(PGPROC *proc, TransactionId latestXid);
 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);
diff --git a/src/include/utils/snapmgmt.h b/src/include/utils/snapmgmt.h
new file mode 100644 (file)
index 0000000..dcadbca
--- /dev/null
@@ -0,0 +1,33 @@
+/*-------------------------------------------------------------------------
+ *
+ * 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 */
diff --git a/src/include/utils/snapshot.h b/src/include/utils/snapshot.h
new file mode 100644 (file)
index 0000000..45f1337
--- /dev/null
@@ -0,0 +1,62 @@
+/*-------------------------------------------------------------------------
+ *
+ * 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 */
index 7083da85ffd953a4e939cc53c2619a3a9ade278f..019a964cd1f67497566c1e784bec4add64a430f3 100644 (file)
 #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;
@@ -84,15 +41,6 @@ extern PGDLLIMPORT SnapshotData SnapshotToastData;
 #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.
@@ -149,13 +97,4 @@ extern HTSV_Result HeapTupleSatisfiesVacuum(HeapTupleHeader tuple,
 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 */
index 6677716c1fc2d06a6e17f022cb68a91d63a770b5..c953e72c5e0793ebe5004b5b9edf95a1f3b319d6 100644 (file)
@@ -32,6 +32,7 @@
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
+#include "utils/snapmgmt.h"
 #include "utils/typcache.h"