1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* genam.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* general index access method routines
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2005-10-15 04:49:52 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/access/index/genam.c,v 1.49 2005/10/15 02:49:09 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* many of the old access method routines have been turned into
|
|
|
|
* macros and moved to genam.h -cim 4/30/91
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2002-05-21 01:51:44 +02:00
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "postgres.h"
|
1996-10-21 09:38:20 +02:00
|
|
|
|
2002-02-19 21:11:20 +01:00
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "miscadmin.h"
|
2001-06-22 21:16:24 +02:00
|
|
|
#include "pgstat.h"
|
1996-10-21 09:38:20 +02:00
|
|
|
|
2002-02-19 21:11:20 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* general access method routines
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* All indexed access methods use an identical scan structure.
|
|
|
|
* We don't know how the various AMs do locking, however, so we don't
|
|
|
|
* do anything about that here.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-12-30 06:05:13 +01:00
|
|
|
* The intent is that an AM implementor will define a beginscan routine
|
|
|
|
* that calls RelationGetIndexScan, to fill in the scan, and then does
|
|
|
|
* whatever kind of locking he wants.
|
|
|
|
*
|
|
|
|
* At the end of a scan, the AM's endscan routine undoes the locking,
|
|
|
|
* but does *not* call IndexScanEnd --- the higher-level index_endscan
|
2000-04-12 19:17:23 +02:00
|
|
|
* routine does that. (We can't do it in the AM because index_endscan
|
1999-12-30 06:05:13 +01:00
|
|
|
* still needs to touch the IndexScanDesc after calling the AM.)
|
|
|
|
*
|
|
|
|
* Because of this, the AM does not have a choice whether to call
|
|
|
|
* RelationGetIndexScan or not; its beginscan routine must return an
|
|
|
|
* object made by RelationGetIndexScan. This is kinda ugly but not
|
|
|
|
* worth cleaning up now.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* RelationGetIndexScan -- Create and fill an IndexScanDesc.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This routine creates an index scan structure and sets its contents
|
|
|
|
* up correctly. This routine calls AMrescan to set up the scan with
|
|
|
|
* the passed key.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Parameters:
|
2002-05-21 01:51:44 +02:00
|
|
|
* indexRelation -- index relation for scan.
|
|
|
|
* nkeys -- count of scan keys.
|
|
|
|
* key -- array of scan keys to restrict the index scan.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns:
|
|
|
|
* An initialized IndexScanDesc.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
IndexScanDesc
|
2002-05-21 01:51:44 +02:00
|
|
|
RelationGetIndexScan(Relation indexRelation,
|
|
|
|
int nkeys, ScanKey key)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
IndexScanDesc scan;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
scan = (IndexScanDesc) palloc(sizeof(IndexScanDescData));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
scan->heapRelation = NULL; /* may be set later */
|
|
|
|
scan->indexRelation = indexRelation;
|
2002-09-04 22:31:48 +02:00
|
|
|
scan->xs_snapshot = SnapshotNow; /* may be set later */
|
2002-05-21 01:51:44 +02:00
|
|
|
scan->numberOfKeys = nkeys;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We allocate the key space here, but the AM is responsible for actually
|
|
|
|
* filling it from the passed key array.
|
2002-05-21 01:51:44 +02:00
|
|
|
*/
|
|
|
|
if (nkeys > 0)
|
|
|
|
scan->keyData = (ScanKey) palloc(sizeof(ScanKeyData) * nkeys);
|
|
|
|
else
|
|
|
|
scan->keyData = NULL;
|
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
scan->is_multiscan = false; /* caller may change this */
|
2002-05-24 20:57:57 +02:00
|
|
|
scan->kill_prior_tuple = false;
|
2002-09-04 22:31:48 +02:00
|
|
|
scan->ignore_killed_tuples = true; /* default setting */
|
2003-03-24 22:42:33 +01:00
|
|
|
scan->keys_are_unique = false; /* may be set by index AM */
|
2002-05-24 20:57:57 +02:00
|
|
|
scan->got_tuple = false;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
scan->opaque = NULL;
|
|
|
|
|
|
|
|
ItemPointerSetInvalid(&scan->currentItemData);
|
|
|
|
ItemPointerSetInvalid(&scan->currentMarkData);
|
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
ItemPointerSetInvalid(&scan->xs_ctup.t_self);
|
|
|
|
scan->xs_ctup.t_datamcxt = NULL;
|
|
|
|
scan->xs_ctup.t_data = NULL;
|
|
|
|
scan->xs_cbuf = InvalidBuffer;
|
2001-06-22 21:16:24 +02:00
|
|
|
|
2003-01-08 20:41:40 +01:00
|
|
|
scan->unique_tuple_pos = 0;
|
|
|
|
scan->unique_tuple_mark = 0;
|
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
pgstat_initstats(&scan->xs_pgstat_info, indexRelation);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
/*
|
|
|
|
* Let the AM fill in the key and any opaque data it wants.
|
|
|
|
*/
|
|
|
|
index_rescan(scan, key);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return scan;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-12-30 06:05:13 +01:00
|
|
|
/* ----------------
|
|
|
|
* IndexScanEnd -- End an index scan.
|
|
|
|
*
|
|
|
|
* This routine just releases the storage acquired by
|
|
|
|
* RelationGetIndexScan(). Any AM-level resources are
|
|
|
|
* assumed to already have been released by the AM's
|
|
|
|
* endscan routine.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* None.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
IndexScanEnd(IndexScanDesc scan)
|
|
|
|
{
|
|
|
|
if (scan->keyData != NULL)
|
|
|
|
pfree(scan->keyData);
|
|
|
|
|
|
|
|
pfree(scan);
|
|
|
|
}
|
|
|
|
|
2002-02-19 21:11:20 +01:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* 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
|
2002-05-21 01:51:44 +02:00
|
|
|
* if the system indexes are unavailable.
|
2002-02-19 21:11:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2002-05-21 01:51:44 +02:00
|
|
|
* These routines could work with non-system tables, actually,
|
2002-02-19 21:11:20 +01:00
|
|
|
* but they're only useful when there is a known index to use with
|
2002-05-21 01:51:44 +02:00
|
|
|
* the given scan keys; so in practice they're only good for
|
2002-02-19 21:11:20 +01:00
|
|
|
* 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
|
2005-04-14 22:03:27 +02:00
|
|
|
* indexId: OID of index to conditionally use
|
2002-02-19 21:11:20 +01:00
|
|
|
* 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
|
2002-05-21 01:51:44 +02:00
|
|
|
systable_beginscan(Relation heapRelation,
|
2005-04-14 22:03:27 +02:00
|
|
|
Oid indexId,
|
2002-02-19 21:11:20 +01:00
|
|
|
bool indexOK,
|
|
|
|
Snapshot snapshot,
|
2002-05-21 01:51:44 +02:00
|
|
|
int nkeys, ScanKey key)
|
2002-02-19 21:11:20 +01:00
|
|
|
{
|
|
|
|
SysScanDesc sysscan;
|
2003-09-24 20:54:02 +02:00
|
|
|
Relation irel;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
if (indexOK &&
|
|
|
|
!IsIgnoringSystemIndexes() &&
|
|
|
|
!ReindexIsProcessingIndex(indexId))
|
|
|
|
irel = index_open(indexId);
|
2003-09-24 20:54:02 +02:00
|
|
|
else
|
|
|
|
irel = NULL;
|
2002-02-19 21:11:20 +01:00
|
|
|
|
|
|
|
sysscan = (SysScanDesc) palloc(sizeof(SysScanDescData));
|
2002-05-21 01:51:44 +02:00
|
|
|
|
|
|
|
sysscan->heap_rel = heapRelation;
|
2003-09-24 20:54:02 +02:00
|
|
|
sysscan->irel = irel;
|
2002-02-19 21:11:20 +01:00
|
|
|
|
2003-09-24 20:54:02 +02:00
|
|
|
if (irel)
|
2002-02-19 21:11:20 +01:00
|
|
|
{
|
2002-05-21 01:51:44 +02:00
|
|
|
int i;
|
2002-02-19 21:11:20 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Change attribute numbers to be index column numbers.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This code could be generalized to search for the index key numbers to
|
|
|
|
* substitute, but for now there's no need.
|
2002-02-19 21:11:20 +01:00
|
|
|
*/
|
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
{
|
2005-03-29 02:17:27 +02:00
|
|
|
Assert(key[i].sk_attno == irel->rd_index->indkey.values[i]);
|
2002-09-04 22:31:48 +02:00
|
|
|
key[i].sk_attno = i + 1;
|
2002-02-19 21:11:20 +01:00
|
|
|
}
|
2003-09-24 20:54:02 +02:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
sysscan->iscan = index_beginscan(heapRelation, irel, snapshot,
|
|
|
|
nkeys, key);
|
2002-02-19 21:11:20 +01:00
|
|
|
sysscan->scan = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-21 01:51:44 +02:00
|
|
|
sysscan->scan = heap_beginscan(heapRelation, snapshot, nkeys, key);
|
2002-02-19 21:11:20 +01:00
|
|
|
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)
|
|
|
|
{
|
2002-05-21 01:51:44 +02:00
|
|
|
HeapTuple htup;
|
2002-02-19 21:11:20 +01:00
|
|
|
|
|
|
|
if (sysscan->irel)
|
2002-05-21 01:51:44 +02:00
|
|
|
htup = index_getnext(sysscan->iscan, ForwardScanDirection);
|
2002-02-19 21:11:20 +01:00
|
|
|
else
|
2002-05-21 01:51:44 +02:00
|
|
|
htup = heap_getnext(sysscan->scan, ForwardScanDirection);
|
2002-02-19 21:11:20 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
index_endscan(sysscan->iscan);
|
|
|
|
index_close(sysscan->irel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
heap_endscan(sysscan->scan);
|
|
|
|
|
|
|
|
pfree(sysscan);
|
|
|
|
}
|