*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.251 2008/03/08 21:57:59 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.252 2008/03/26 16:20:46 alvherre Exp $
*
*
* INTERFACE ROUTINES
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/index/indexam.c,v 1.101 2008/01/01 19:45:46 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/access/index/indexam.c,v 1.102 2008/03/26 16:20:46 alvherre Exp $
*
* INTERFACE ROUTINES
* index_open - open an index relation by relation OID
#include "access/transam.h"
#include "pgstat.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
/* ----------------------------------------------------------------
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtpage.c,v 1.106 2008/01/01 19:45:46 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtpage.c,v 1.107 2008/03/26 16:20:46 alvherre Exp $
*
* NOTES
* Postgres btree pages look like ordinary relation pages. The opaque
#include "storage/freespace.h"
#include "storage/lmgr.h"
#include "utils/inval.h"
+#include "utils/snapmgmt.h"
/*
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/backend/access/transam/subtrans.c,v 1.20 2008/01/01 19:45:48 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/subtrans.c,v 1.21 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "access/slru.h"
#include "access/subtrans.h"
#include "access/transam.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
/*
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/transam/transam.c,v 1.74 2008/03/11 20:20:35 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/transam.c,v 1.75 2008/03/26 16:20:46 alvherre Exp $
*
* NOTES
* This file contains the high level access-method interface to the
#include "access/clog.h"
#include "access/subtrans.h"
#include "access/transam.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
/*
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.260 2008/03/17 19:44:41 petere Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.261 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/xml.h"
#include "pg_trace.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/catalog/catalog.c,v 1.73 2008/02/20 17:44:09 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/catalog/catalog.c,v 1.74 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "storage/fd.h"
#include "utils/fmgroids.h"
#include "utils/relcache.h"
+#include "utils/tqual.h"
#define OIDCHARS 10 /* max chars printed by %u */
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.328 2008/03/25 22:42:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.329 2008/03/26 16:20:46 alvherre Exp $
*
*
* INTERFACE ROUTINES
#include "utils/lsyscache.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
+#include "utils/snapmgmt.h"
static void AddNewRelationTuple(Relation pg_class_desc,
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.293 2008/03/25 22:42:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.294 2008/03/26 16:20:46 alvherre Exp $
*
*
* INTERFACE ROUTINES
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "utils/tuplesort.h"
+#include "utils/snapmgmt.h"
/* state info for validate_index bulkdelete callback */
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.169 2008/01/30 19:46:48 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.170 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.296 2008/03/08 01:16:26 adunstan Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.297 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
* Portions Copyright (c) 1994-5, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.169 2008/01/01 19:45:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.170 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/tuplesort.h"
+#include "utils/snapmgmt.h"
/* Hook for plugins to get control in ExplainOneQuery() */
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.171 2008/02/07 17:09:51 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.172 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/portalcmds.c,v 1.70 2008/03/20 20:05:56 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/portalcmds.c,v 1.71 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "executor/executor.h"
#include "tcop/pquery.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
/*
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.82 2008/03/25 22:42:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.83 2008/03/26 16:20:46 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
/*
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.244 2008/03/25 22:42:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.245 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.227 2008/01/02 23:34:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.228 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.368 2008/03/19 14:18:21 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.369 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/memutils.h"
#include "utils/pg_rusage.h"
#include "utils/relcache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "pgstat.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/variable.c,v 1.125 2008/01/03 21:23:15 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/variable.c,v 1.126 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#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"
/*
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.122 2008/03/25 22:42:43 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.123 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/nodeBitmapHeapscan.c,v 1.22 2008/01/01 19:45:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/nodeBitmapHeapscan.c,v 1.23 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "executor/nodeBitmapHeapscan.h"
#include "pgstat.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
static TupleTableSlot *BitmapHeapNext(BitmapHeapScanState *node);
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.189 2008/03/20 20:05:56 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.190 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "executor/spi_priv.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
#include "utils/typcache.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.141 2008/03/15 20:46:31 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.142 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#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"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.41 2008/03/11 20:20:35 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.42 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "access/twophase.h"
#include "miscadmin.h"
#include "storage/procarray.h"
-#include "utils/tqual.h"
+#include "utils/snapmgmt.h"
/* Our shared memory area */
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/storage/large_object/inv_api.c,v 1.128 2008/03/01 19:26:22 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/storage/large_object/inv_api.c,v 1.129 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "storage/large_object.h"
#include "utils/fmgroids.h"
#include "utils/resowner.h"
+#include "utils/snapmgmt.h"
/*
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.97 2008/01/01 19:45:52 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.98 2008/03/26 16:20:47 alvherre Exp $
*
* NOTES
* This cruft is the server side of PQfn.
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/lsyscache.h"
+#include "utils/snapmgmt.h"
#include "utils/syscache.h"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.545 2008/03/12 23:58:27 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.546 2008/03/26 16:20:47 alvherre Exp $
*
* NOTES
* this is the "main" module of the postgres backend and
#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"
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.120 2008/01/01 19:45:52 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.121 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "tcop/tcopprot.h"
#include "tcop/utility.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
/*
*
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.104 2008/02/18 23:00:32 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.105 2008/03/26 16:20:47 alvherre Exp $
*
* ----------
*/
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
/* ----------
* Author: Jan Wieck, Afilias USA INC.
* 64-bit txids: Marko Kreen, Skype Technologies
*
- * $PostgreSQL: pgsql/src/backend/utils/adt/txid.c,v 1.4 2008/01/01 19:45:53 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/txid.c,v 1.5 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
+#include "utils/snapmgmt.h"
#ifndef INT64_IS_BUSTED
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/cache/plancache.c,v 1.15 2008/01/01 19:45:53 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/cache/plancache.c,v 1.16 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
+#include "utils/snapmgmt.h"
typedef struct
# Makefile for utils/time
#
# IDENTIFICATION
-# $PostgreSQL: pgsql/src/backend/utils/time/Makefile,v 1.13 2008/02/19 10:30:09 petere Exp $
+# $PostgreSQL: pgsql/src/backend/utils/time/Makefile,v 1.14 2008/03/26 16:20:47 alvherre Exp $
#
#-------------------------------------------------------------------------
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: pgsql/src/backend/utils/time/snapmgmt.c,v 1.1 2008/03/26 16:20:47 alvherre Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+#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 */
+}
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/time/tqual.c,v 1.109 2008/01/01 19:45:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/time/tqual.c,v 1.110 2008/03/26 16:20:47 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
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?
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/access/relscan.h,v 1.60 2008/01/14 01:39:09 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/relscan.h,v 1.61 2008/03/26 16:20:48 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#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
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/storage/large_object.h,v 1.39 2008/01/01 19:45:59 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/storage/large_object.h,v 1.40 2008/03/26 16:20:48 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef LARGE_OBJECT_H
#define LARGE_OBJECT_H
-#include "utils/tqual.h"
+#include "utils/snapshot.h"
/*----------
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.20 2008/01/09 21:52:36 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.21 2008/03/26 16:20:48 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#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: pgsql/src/include/utils/snapmgmt.h,v 1.1 2008/03/26 16:20:48 alvherre Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+#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: pgsql/src/include/utils/snapshot.h,v 1.1 2008/03/26 16:20:48 alvherre Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+#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 */
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/utils/tqual.h,v 1.71 2008/01/01 19:45:59 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/utils/tqual.h,v 1.72 2008/03/26 16:20:48 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#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 */
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.204 2008/03/25 22:42:45 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.205 2008/03/26 16:20:48 alvherre Exp $
*
*-------------------------------------------------------------------------
*/
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/snapmgmt.h"
#include "utils/typcache.h"