summaryrefslogtreecommitdiff
path: root/src/backend/access/index/genam.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2002-02-19 20:11:20 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2002-02-19 20:11:20 +0000
commit786340441706ac1957a031f11ad1c2e5b6e18314 (patch)
tree4e6b689b96778e42e6cc679169f71dc180049e04 /src/backend/access/index/genam.c
parent8e2998d8a6aebc2a3b22e6048fab8abe1c95f1f0 (diff)
A bunch of changes aimed at reducing backend startup time...
Improve 'pg_internal.init' relcache entry preload mechanism so that it is safe to use for all system catalogs, and arrange to preload a realistic set of system-catalog entries instead of only the three nailed-in-cache indexes that were formerly loaded this way. Fix mechanism for deleting out-of-date pg_internal.init files: this must be synchronized with transaction commit, not just done at random times within transactions. Drive it off relcache invalidation mechanism so that no special-case tests are needed. Cache additional information in relcache entries for indexes (their pg_index tuples and index-operator OIDs) to eliminate repeated lookups. Also cache index opclass info at the per-opclass level to avoid repeated lookups during relcache load. Generalize 'systable scan' utilities originally developed by Hiroshi, move them into genam.c, use in a number of places where there was formerly ugly code for choosing either heap or index scan. In particular this allows simplification of the logic that prevents infinite recursion between syscache and relcache during startup: we can easily switch to heapscans in relcache.c when and where needed to avoid recursion, so IndexScanOK becomes simpler and does not need any expensive initialization. Eliminate useless opening of a heapscan data structure while doing an indexscan (this saves an mdnblocks call and thus at least one kernel call).
Diffstat (limited to 'src/backend/access/index/genam.c')
-rw-r--r--src/backend/access/index/genam.c161
1 files changed, 158 insertions, 3 deletions
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 5c3a6dc81ce..e36a1450db3 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/access/index/genam.c,v 1.30 2001/10/28 06:25:41 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/access/index/genam.c,v 1.31 2002/02/19 20:11:10 tgl Exp $
*
* NOTES
* many of the old access method routines have been turned into
@@ -44,12 +44,14 @@
* next item pointer using the flags.
* ----------------------------------------------------------------
*/
-
#include "postgres.h"
-#include "access/genam.h"
+#include "access/genam.h"
+#include "access/heapam.h"
+#include "miscadmin.h"
#include "pgstat.h"
+
/* ----------------------------------------------------------------
* general access method routines
*
@@ -242,3 +244,156 @@ IndexScanRestorePosition(IndexScanDesc scan)
}
#endif
+
+
+/* ----------------------------------------------------------------
+ * heap-or-index-scan access to system catalogs
+ *
+ * These functions support system catalog accesses that normally use
+ * an index but need to be capable of being switched to heap scans
+ * if the system indexes are unavailable. The interface is
+ * as easy to use as a heap scan, and hides all the extra cruft of
+ * the present indexscan API.
+ *
+ * The specified scan keys must be compatible with the named index.
+ * Generally this means that they must constrain either all columns
+ * of the index, or the first K columns of an N-column index.
+ *
+ * These routines would work fine with non-system tables, actually,
+ * but they're only useful when there is a known index to use with
+ * the given scan keys, so in practice they're only good for
+ * predetermined types of scans of system catalogs.
+ * ----------------------------------------------------------------
+ */
+
+/*
+ * systable_beginscan --- set up for heap-or-index scan
+ *
+ * rel: catalog to scan, already opened and suitably locked
+ * indexRelname: name of index to conditionally use
+ * indexOK: if false, forces a heap scan (see notes below)
+ * snapshot: time qual to use (usually should be SnapshotNow)
+ * nkeys, key: scan keys
+ *
+ * The attribute numbers in the scan key should be set for the heap case.
+ * If we choose to index, we reset them to 1..n to reference the index
+ * columns. Note this means there must be one scankey qualification per
+ * index column! This is checked by the Asserts in the normal, index-using
+ * case, but won't be checked if the heapscan path is taken.
+ *
+ * The routine checks the normal cases for whether an indexscan is safe,
+ * but caller can make additional checks and pass indexOK=false if needed.
+ * In standard case indexOK can simply be constant TRUE.
+ */
+SysScanDesc
+systable_beginscan(Relation rel,
+ const char *indexRelname,
+ bool indexOK,
+ Snapshot snapshot,
+ unsigned nkeys, ScanKey key)
+{
+ SysScanDesc sysscan;
+
+ sysscan = (SysScanDesc) palloc(sizeof(SysScanDescData));
+ sysscan->heap_rel = rel;
+ sysscan->snapshot = snapshot;
+ sysscan->tuple.t_datamcxt = NULL;
+ sysscan->tuple.t_data = NULL;
+ sysscan->buffer = InvalidBuffer;
+
+ if (indexOK &&
+ rel->rd_rel->relhasindex &&
+ !IsIgnoringSystemIndexes())
+ {
+ Relation irel;
+ unsigned i;
+
+ sysscan->irel = irel = index_openr(indexRelname);
+ /*
+ * Change attribute numbers to be index column numbers.
+ *
+ * This code could be generalized to search for the index key numbers
+ * to substitute, but for now there's no need.
+ */
+ for (i = 0; i < nkeys; i++)
+ {
+ Assert(key[i].sk_attno == irel->rd_index->indkey[i]);
+ key[i].sk_attno = i+1;
+ }
+ sysscan->iscan = index_beginscan(irel, false, nkeys, key);
+ sysscan->scan = NULL;
+ }
+ else
+ {
+ sysscan->irel = (Relation) NULL;
+ sysscan->scan = heap_beginscan(rel, false, snapshot, nkeys, key);
+ sysscan->iscan = NULL;
+ }
+
+ return sysscan;
+}
+
+/*
+ * systable_getnext --- get next tuple in a heap-or-index scan
+ *
+ * Returns NULL if no more tuples available.
+ *
+ * Note that returned tuple is a reference to data in a disk buffer;
+ * it must not be modified, and should be presumed inaccessible after
+ * next getnext() or endscan() call.
+ */
+HeapTuple
+systable_getnext(SysScanDesc sysscan)
+{
+ HeapTuple htup = (HeapTuple) NULL;
+
+ if (sysscan->irel)
+ {
+ RetrieveIndexResult indexRes;
+
+ if (BufferIsValid(sysscan->buffer))
+ {
+ ReleaseBuffer(sysscan->buffer);
+ sysscan->buffer = InvalidBuffer;
+ }
+
+ while ((indexRes = index_getnext(sysscan->iscan, ForwardScanDirection)) != NULL)
+ {
+ sysscan->tuple.t_self = indexRes->heap_iptr;
+ pfree(indexRes);
+ heap_fetch(sysscan->heap_rel, sysscan->snapshot,
+ &sysscan->tuple, &sysscan->buffer,
+ sysscan->iscan);
+ if (sysscan->tuple.t_data != NULL)
+ {
+ htup = &sysscan->tuple;
+ break;
+ }
+ }
+ }
+ else
+ htup = heap_getnext(sysscan->scan, 0);
+
+ return htup;
+}
+
+/*
+ * systable_endscan --- close scan, release resources
+ *
+ * Note that it's still up to the caller to close the heap relation.
+ */
+void
+systable_endscan(SysScanDesc sysscan)
+{
+ if (sysscan->irel)
+ {
+ if (BufferIsValid(sysscan->buffer))
+ ReleaseBuffer(sysscan->buffer);
+ index_endscan(sysscan->iscan);
+ index_close(sysscan->irel);
+ }
+ else
+ heap_endscan(sysscan->scan);
+
+ pfree(sysscan);
+}