1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* indexam.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* general index access method routines
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-02-26 13:07:10 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/access/index/indexam.c,v 1.21 1998/02/26 12:07:10 vadim Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* INTERFACE ROUTINES
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_open - open an index relation by relationId
|
|
|
|
* index_openr - open a index relation by name
|
|
|
|
* index_close - close a index relation
|
|
|
|
* index_beginscan - start a scan of an index
|
|
|
|
* index_rescan - restart a scan of an index
|
|
|
|
* index_endscan - end a scan
|
|
|
|
* index_insert - insert an index tuple into a relation
|
|
|
|
* index_delete - delete an item from an index relation
|
|
|
|
* index_markpos - mark a scan position
|
|
|
|
* index_restrpos - restore a scan position
|
|
|
|
* index_getnext - get the next tuple from a scan
|
|
|
|
* ** index_fetch - retrieve tuple with tid
|
1996-07-09 08:22:35 +02:00
|
|
|
* ** index_replace - replace a tuple
|
|
|
|
* ** index_getattr - get an attribute from an index tuple
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_getprocid - get a support procedure id from the rel tuple
|
|
|
|
*
|
|
|
|
* IndexScanIsValid - check index scan
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* This file contains the index_ routines which used
|
|
|
|
* to be a scattered collection of stuff in access/genam.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* The ** routines: index_fetch, index_replace, and index_getattr
|
|
|
|
* have not yet been implemented. They may not be needed.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* old comments
|
1997-09-07 07:04:48 +02:00
|
|
|
* Scans are implemented as follows:
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* `0' represents an invalid item pointer.
|
|
|
|
* `-' represents an unknown item pointer.
|
|
|
|
* `X' represents a known item pointers.
|
|
|
|
* `+' represents known or invalid item pointers.
|
|
|
|
* `*' represents any item pointers.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* State is represented by a triple of these symbols in the order of
|
|
|
|
* previous, current, next. Note that the case of reverse scans works
|
|
|
|
* identically.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* State Result
|
|
|
|
* (1) + + - + 0 0 (if the next item pointer is invalid)
|
|
|
|
* (2) + X - (otherwise)
|
|
|
|
* (3) * 0 0 * 0 0 (no change)
|
|
|
|
* (4) + X 0 X 0 0 (shift)
|
|
|
|
* (5) * + X + X - (shift, add unknown)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* All other states cannot occur.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Note: It would be possible to cache the status of the previous and
|
|
|
|
* next item pointer using the flags.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1996-11-05 11:02:06 +01:00
|
|
|
#include <postgres.h>
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
#include <access/genam.h>
|
1996-11-05 11:02:06 +01:00
|
|
|
#include <utils/relcache.h>
|
|
|
|
#include <fmgr.h>
|
|
|
|
#include <storage/lmgr.h>
|
1997-08-27 01:31:58 +02:00
|
|
|
#include <access/heapam.h>
|
1996-10-21 09:38:20 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* undefine macros we aren't going to use that would otherwise
|
|
|
|
* get in our way.. delete is defined in c.h and the am's are
|
|
|
|
* defined in heapam.h
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
#undef delete
|
|
|
|
#undef aminsert
|
|
|
|
#undef amdelete
|
|
|
|
#undef ambeginscan
|
|
|
|
#undef amrescan
|
|
|
|
#undef amendscan
|
|
|
|
#undef ammarkpos
|
|
|
|
#undef amrestrpos
|
|
|
|
#undef amgettuple
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* macros used in index_ routines
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#define RELATION_CHECKS \
|
|
|
|
Assert(RelationIsValid(relation)); \
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(PointerIsValid(relation->rd_am))
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#define SCAN_CHECKS \
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(IndexScanIsValid(scan)); \
|
|
|
|
Assert(RelationIsValid(scan->relation)); \
|
|
|
|
Assert(PointerIsValid(scan->relation->rd_am))
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#define GET_REL_PROCEDURE(x,y) \
|
1997-09-07 07:04:48 +02:00
|
|
|
procedure = relation->rd_am->y; \
|
|
|
|
if (! RegProcedureIsValid(procedure)) \
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "index_%s: invalid %s regproc", \
|
1997-09-07 07:04:48 +02:00
|
|
|
CppAsString(x), CppAsString(y))
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#define GET_SCAN_PROCEDURE(x,y) \
|
1997-09-07 07:04:48 +02:00
|
|
|
procedure = scan->relation->rd_am->y; \
|
|
|
|
if (! RegProcedureIsValid(procedure)) \
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "index_%s: invalid %s regproc", \
|
1997-09-07 07:04:48 +02:00
|
|
|
CppAsString(x), CppAsString(y))
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_ interface functions
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_open - open an index relation by relationId
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* presently the relcache routines do all the work we need
|
|
|
|
* to open/close index relations.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Relation
|
|
|
|
index_open(Oid relationId)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
return RelationIdGetRelation(relationId);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_openr - open a index relation by name
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* presently the relcache routines do all the work we need
|
|
|
|
* to open/close index relations.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Relation
|
|
|
|
index_openr(char *relationName)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
return RelationNameGetRelation(relationName);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_close - close a index relation
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* presently the relcache routines do all the work we need
|
|
|
|
* to open/close index relations.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
index_close(Relation relation)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
RelationClose(relation);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_insert - insert an index tuple into a relation
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
InsertIndexResult
|
|
|
|
index_insert(Relation relation,
|
1997-09-08 23:56:23 +02:00
|
|
|
Datum *datum,
|
1997-09-07 07:04:48 +02:00
|
|
|
char *nulls,
|
|
|
|
ItemPointer heap_t_ctid,
|
|
|
|
Relation heapRel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RegProcedure procedure;
|
1997-09-07 07:04:48 +02:00
|
|
|
InsertIndexResult specificResult;
|
|
|
|
|
|
|
|
RELATION_CHECKS;
|
|
|
|
GET_REL_PROCEDURE(insert, aminsert);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* have the am's insert proc do all the work.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
specificResult = (InsertIndexResult)
|
|
|
|
fmgr(procedure, relation, datum, nulls, heap_t_ctid, heapRel, NULL);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* the insert proc is supposed to return a "specific result" and
|
|
|
|
* this routine has to return a "general result" so after we get
|
|
|
|
* something back from the insert proc, we allocate a
|
|
|
|
* "general result" and copy some crap between the two.
|
|
|
|
*
|
|
|
|
* As far as I'm concerned all this result shit is needlessly c
|
|
|
|
* omplicated and should be eliminated. -cim 1/19/91
|
|
|
|
*
|
|
|
|
* mao concurs. regardless of how we feel here, however, it is
|
|
|
|
* important to free memory we don't intend to return to anyone.
|
|
|
|
* 2/28/91
|
|
|
|
*
|
|
|
|
* this "general result" crap is now gone. -ay 3/6/95
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (specificResult);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_delete - delete an item from an index relation
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
index_delete(Relation relation, ItemPointer indexItem)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RegProcedure procedure;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
RELATION_CHECKS;
|
|
|
|
GET_REL_PROCEDURE(delete, amdelete);
|
|
|
|
|
|
|
|
fmgr(procedure, relation, indexItem);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_beginscan - start a scan of an index
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
IndexScanDesc
|
|
|
|
index_beginscan(Relation relation,
|
1997-09-07 07:04:48 +02:00
|
|
|
bool scanFromEnd,
|
|
|
|
uint16 numberOfKeys,
|
|
|
|
ScanKey key)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
IndexScanDesc scandesc;
|
|
|
|
RegProcedure procedure;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
RELATION_CHECKS;
|
|
|
|
GET_REL_PROCEDURE(beginscan, ambeginscan);
|
|
|
|
|
|
|
|
RelationSetRIntentLock(relation);
|
|
|
|
|
|
|
|
scandesc = (IndexScanDesc)
|
|
|
|
fmgr(procedure, relation, scanFromEnd, numberOfKeys, key);
|
|
|
|
|
|
|
|
return scandesc;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_rescan - restart a scan of an index
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
index_rescan(IndexScanDesc scan, bool scanFromEnd, ScanKey key)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RegProcedure procedure;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
SCAN_CHECKS;
|
|
|
|
GET_SCAN_PROCEDURE(rescan, amrescan);
|
|
|
|
|
|
|
|
fmgr(procedure, scan, scanFromEnd, key);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_endscan - end a scan
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
index_endscan(IndexScanDesc scan)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RegProcedure procedure;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
SCAN_CHECKS;
|
|
|
|
GET_SCAN_PROCEDURE(endscan, amendscan);
|
|
|
|
|
|
|
|
fmgr(procedure, scan);
|
|
|
|
|
|
|
|
RelationUnsetRIntentLock(scan->relation);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_markpos - mark a scan position
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
index_markpos(IndexScanDesc scan)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RegProcedure procedure;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
SCAN_CHECKS;
|
|
|
|
GET_SCAN_PROCEDURE(markpos, ammarkpos);
|
|
|
|
|
|
|
|
fmgr(procedure, scan);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_restrpos - restore a scan position
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
index_restrpos(IndexScanDesc scan)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RegProcedure procedure;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
SCAN_CHECKS;
|
|
|
|
GET_SCAN_PROCEDURE(restrpos, amrestrpos);
|
|
|
|
|
|
|
|
fmgr(procedure, scan);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_getnext - get the next tuple from a scan
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* A RetrieveIndexResult is a index tuple/heap tuple pair
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
RetrieveIndexResult
|
|
|
|
index_getnext(IndexScanDesc scan,
|
1997-09-07 07:04:48 +02:00
|
|
|
ScanDirection direction)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RegProcedure procedure;
|
1997-09-07 07:04:48 +02:00
|
|
|
RetrieveIndexResult result;
|
|
|
|
|
|
|
|
SCAN_CHECKS;
|
|
|
|
GET_SCAN_PROCEDURE(getnext, amgettuple);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* have the am's gettuple proc do all the work.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
result = (RetrieveIndexResult)
|
|
|
|
fmgr(procedure, scan, direction);
|
|
|
|
|
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* index_getprocid
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Some indexed access methods may require support routines that are
|
|
|
|
* not in the operator class/operator model imposed by pg_am. These
|
|
|
|
* access methods may store the OIDs of registered procedures they
|
|
|
|
* need in pg_amproc. These registered procedure OIDs are ordered in
|
|
|
|
* a way that makes sense to the access method, and used only by the
|
|
|
|
* access method. The general index code doesn't know anything about
|
|
|
|
* the routines involved; it just builds an ordered list of them for
|
|
|
|
* each attribute on which an index is defined.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This routine returns the requested procedure OID for a particular
|
|
|
|
* indexed attribute.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
RegProcedure
|
|
|
|
index_getprocid(Relation irel,
|
1997-09-07 07:04:48 +02:00
|
|
|
AttrNumber attnum,
|
|
|
|
uint16 procnum)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RegProcedure *loc;
|
|
|
|
int natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
natts = irel->rd_rel->relnatts;
|
|
|
|
|
|
|
|
loc = irel->rd_support;
|
|
|
|
|
|
|
|
Assert(loc != NULL);
|
|
|
|
|
|
|
|
return (loc[(natts * (procnum - 1)) + (attnum - 1)]);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
GetIndexValue(HeapTuple tuple,
|
1997-09-07 07:04:48 +02:00
|
|
|
TupleDesc hTupDesc,
|
|
|
|
int attOff,
|
|
|
|
AttrNumber attrNums[],
|
1997-09-08 23:56:23 +02:00
|
|
|
FuncIndexInfo *fInfo,
|
|
|
|
bool *attNull,
|
1997-09-07 07:04:48 +02:00
|
|
|
Buffer buffer)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Datum returnVal;
|
|
|
|
bool isNull;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (PointerIsValid(fInfo) && FIgetProcOid(fInfo) != InvalidOid)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
|
|
|
Datum *attData = (Datum *) palloc(FIgetnArgs(fInfo) * sizeof(Datum));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < FIgetnArgs(fInfo); i++)
|
|
|
|
{
|
1997-09-12 06:09:08 +02:00
|
|
|
attData[i] = heap_getattr(tuple,
|
|
|
|
attrNums[i],
|
|
|
|
hTupDesc,
|
|
|
|
attNull);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
returnVal = (Datum) fmgr_array_args(FIgetProcOid(fInfo),
|
|
|
|
FIgetnArgs(fInfo),
|
|
|
|
(char **) attData,
|
|
|
|
&isNull);
|
|
|
|
pfree(attData);
|
|
|
|
*attNull = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-01-31 05:39:26 +01:00
|
|
|
returnVal = heap_getattr(tuple, attrNums[attOff],
|
1997-09-12 06:09:08 +02:00
|
|
|
hTupDesc, attNull);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
return returnVal;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|