1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-09 06:21:09 +03:00

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).
This commit is contained in:
Tom Lane
2002-02-19 20:11:20 +00:00
parent 8e2998d8a6
commit 7863404417
33 changed files with 1997 additions and 2220 deletions

View File

@@ -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);
}

View File

@@ -9,29 +9,19 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/index/Attic/istrat.c,v 1.56 2001/11/05 17:46:24 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/access/index/Attic/istrat.c,v 1.57 2002/02/19 20:11:10 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/heapam.h"
#include "access/istrat.h"
#include "catalog/catname.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_amproc.h"
#include "catalog/pg_index.h"
#include "catalog/pg_operator.h"
#include "miscadmin.h"
#include "utils/fmgroids.h"
#include "utils/syscache.h"
#ifdef USE_ASSERT_CHECKING
static bool StrategyEvaluationIsValid(StrategyEvaluation evaluation);
static bool StrategyExpressionIsValid(StrategyExpression expression,
StrategyNumber maxStrategy);
static ScanKey StrategyMapGetScanKeyEntry(StrategyMap map,
StrategyNumber strategyNumber);
static bool StrategyOperatorIsValid(StrategyOperator operator,
StrategyNumber maxStrategy);
static bool StrategyTermIsValid(StrategyTerm term,
@@ -63,7 +53,7 @@ static bool StrategyTermIsValid(StrategyTerm term,
* Assumes that the index strategy number is valid.
* Bounds checking should be done outside this routine.
*/
static ScanKey
ScanKey
StrategyMapGetScanKeyEntry(StrategyMap map,
StrategyNumber strategyNumber)
{
@@ -453,161 +443,6 @@ RelationInvokeStrategy(Relation relation,
}
#endif
/* ----------------
* FillScanKeyEntry
*
* Initialize a ScanKey entry for the given operator OID.
* ----------------
*/
static void
FillScanKeyEntry(Oid operatorObjectId, ScanKey entry)
{
HeapTuple tuple;
tuple = SearchSysCache(OPEROID,
ObjectIdGetDatum(operatorObjectId),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "FillScanKeyEntry: unknown operator %u",
operatorObjectId);
MemSet(entry, 0, sizeof(*entry));
entry->sk_flags = 0;
entry->sk_procedure = ((Form_pg_operator) GETSTRUCT(tuple))->oprcode;
ReleaseSysCache(tuple);
if (!RegProcedureIsValid(entry->sk_procedure))
elog(ERROR, "FillScanKeyEntry: no procedure for operator %u",
operatorObjectId);
/*
* Mark entry->sk_func invalid, until and unless someone sets it up.
*/
entry->sk_func.fn_oid = InvalidOid;
}
/*
* IndexSupportInitialize
* Initializes an index strategy and associated support procedures.
*
* Data is returned into *indexStrategy, *indexSupport, and *isUnique,
* all of which are objects allocated by the caller.
*
* The primary input keys are indexObjectId and accessMethodObjectId.
* The caller also passes maxStrategyNumber, maxSupportNumber, and
* maxAttributeNumber, since these indicate the size of the indexStrategy
* and indexSupport arrays it has allocated --- but in practice these
* numbers must always match those obtainable from the system catalog
* entries for the index and access method.
*/
void
IndexSupportInitialize(IndexStrategy indexStrategy,
RegProcedure *indexSupport,
bool *isUnique,
Oid indexObjectId,
Oid accessMethodObjectId,
StrategyNumber maxStrategyNumber,
StrategyNumber maxSupportNumber,
AttrNumber maxAttributeNumber)
{
HeapTuple tuple;
Form_pg_index iform;
int attIndex;
Oid operatorClassObjectId[INDEX_MAX_KEYS];
maxStrategyNumber = AMStrategies(maxStrategyNumber);
tuple = SearchSysCache(INDEXRELID,
ObjectIdGetDatum(indexObjectId),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "IndexSupportInitialize: no pg_index entry for index %u",
indexObjectId);
iform = (Form_pg_index) GETSTRUCT(tuple);
*isUnique = iform->indisunique;
/*
* XXX note that the following assumes the INDEX tuple is well formed
* and that the *key and *class are 0 terminated.
*/
for (attIndex = 0; attIndex < maxAttributeNumber; attIndex++)
{
if (iform->indkey[attIndex] == InvalidAttrNumber ||
!OidIsValid(iform->indclass[attIndex]))
elog(ERROR, "IndexSupportInitialize: bogus pg_index tuple");
operatorClassObjectId[attIndex] = iform->indclass[attIndex];
}
ReleaseSysCache(tuple);
/* if support routines exist for this access method, load them */
if (maxSupportNumber > 0)
{
for (attIndex = 0; attIndex < maxAttributeNumber; attIndex++)
{
Oid opclass = operatorClassObjectId[attIndex];
RegProcedure *loc;
StrategyNumber support;
loc = &indexSupport[attIndex * maxSupportNumber];
for (support = 0; support < maxSupportNumber; ++support)
{
tuple = SearchSysCache(AMPROCNUM,
ObjectIdGetDatum(opclass),
Int16GetDatum(support + 1),
0, 0);
if (HeapTupleIsValid(tuple))
{
Form_pg_amproc amprocform;
amprocform = (Form_pg_amproc) GETSTRUCT(tuple);
loc[support] = amprocform->amproc;
ReleaseSysCache(tuple);
}
else
loc[support] = InvalidOid;
}
}
}
/* Now load the strategy information for the index operators */
for (attIndex = 0; attIndex < maxAttributeNumber; attIndex++)
{
Oid opclass = operatorClassObjectId[attIndex];
StrategyMap map;
StrategyNumber strategy;
map = IndexStrategyGetStrategyMap(indexStrategy,
maxStrategyNumber,
attIndex + 1);
for (strategy = 1; strategy <= maxStrategyNumber; strategy++)
{
ScanKey mapentry = StrategyMapGetScanKeyEntry(map, strategy);
tuple = SearchSysCache(AMOPSTRATEGY,
ObjectIdGetDatum(opclass),
Int16GetDatum(strategy),
0, 0);
if (HeapTupleIsValid(tuple))
{
Form_pg_amop amopform;
amopform = (Form_pg_amop) GETSTRUCT(tuple);
FillScanKeyEntry(amopform->amopopr, mapentry);
ReleaseSysCache(tuple);
}
else
ScanKeyEntrySetIllegal(mapentry);
}
}
}
/* ----------------
* IndexStrategyDisplay
* ----------------