1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-15 03:41:20 +03:00

Restructure indexscan API (index_beginscan, index_getnext) per

yesterday's proposal to pghackers.  Also remove unnecessary parameters
to heap_beginscan, heap_rescan.  I modified pg_proc.h to reflect the
new numbers of parameters for the AM interface routines, but did not
force an initdb because nothing actually looks at those fields.
This commit is contained in:
Tom Lane
2002-05-20 23:51:44 +00:00
parent c961474c96
commit 44fbe20d62
59 changed files with 834 additions and 1283 deletions

View File

@@ -12,7 +12,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtree.c,v 1.88 2002/03/02 21:39:18 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtree.c,v 1.89 2002/05/20 23:51:41 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -296,7 +296,7 @@ btgettuple(PG_FUNCTION_ARGS)
{
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
ScanDirection dir = (ScanDirection) PG_GETARG_INT32(1);
RetrieveIndexResult res;
bool res;
/*
* If we've already initialized this scan, we can just advance it in
@@ -326,12 +326,12 @@ btgettuple(PG_FUNCTION_ARGS)
*/
if (res)
{
((BTScanOpaque) scan->opaque)->curHeapIptr = res->heap_iptr;
((BTScanOpaque) scan->opaque)->curHeapIptr = scan->xs_ctup.t_self;
LockBuffer(((BTScanOpaque) scan->opaque)->btso_curbuf,
BUFFER_LOCK_UNLOCK);
}
PG_RETURN_POINTER(res);
PG_RETURN_BOOL(res);
}
/*
@@ -341,13 +341,12 @@ Datum
btbeginscan(PG_FUNCTION_ARGS)
{
Relation rel = (Relation) PG_GETARG_POINTER(0);
bool fromEnd = PG_GETARG_BOOL(1);
uint16 keysz = PG_GETARG_UINT16(2);
ScanKey scankey = (ScanKey) PG_GETARG_POINTER(3);
int keysz = PG_GETARG_INT32(1);
ScanKey scankey = (ScanKey) PG_GETARG_POINTER(2);
IndexScanDesc scan;
/* get the scan */
scan = RelationGetIndexScan(rel, fromEnd, keysz, scankey);
scan = RelationGetIndexScan(rel, keysz, scankey);
PG_RETURN_POINTER(scan);
}
@@ -359,11 +358,7 @@ Datum
btrescan(PG_FUNCTION_ARGS)
{
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
#ifdef NOT_USED /* XXX surely it's wrong to ignore this? */
bool fromEnd = PG_GETARG_BOOL(1);
#endif
ScanKey scankey = (ScanKey) PG_GETARG_POINTER(2);
ScanKey scankey = (ScanKey) PG_GETARG_POINTER(1);
ItemPointer iptr;
BTScanOpaque so;
@@ -373,11 +368,13 @@ btrescan(PG_FUNCTION_ARGS)
{
so = (BTScanOpaque) palloc(sizeof(BTScanOpaqueData));
so->btso_curbuf = so->btso_mrkbuf = InvalidBuffer;
so->keyData = (ScanKey) NULL;
ItemPointerSetInvalid(&(so->curHeapIptr));
ItemPointerSetInvalid(&(so->mrkHeapIptr));
if (scan->numberOfKeys > 0)
so->keyData = (ScanKey) palloc(scan->numberOfKeys * sizeof(ScanKeyData));
else
so->keyData = (ScanKey) NULL;
scan->opaque = so;
scan->flags = 0x0;
}
/* we aren't holding any read locks, but gotta drop the pins */
@@ -385,6 +382,7 @@ btrescan(PG_FUNCTION_ARGS)
{
ReleaseBuffer(so->btso_curbuf);
so->btso_curbuf = InvalidBuffer;
ItemPointerSetInvalid(&(so->curHeapIptr));
ItemPointerSetInvalid(iptr);
}
@@ -392,6 +390,7 @@ btrescan(PG_FUNCTION_ARGS)
{
ReleaseBuffer(so->btso_mrkbuf);
so->btso_mrkbuf = InvalidBuffer;
ItemPointerSetInvalid(&(so->mrkHeapIptr));
ItemPointerSetInvalid(iptr);
}
@@ -491,7 +490,7 @@ btmarkpos(PG_FUNCTION_ARGS)
/* bump pin on current buffer for assignment to mark buffer */
if (ItemPointerIsValid(&(scan->currentItemData)))
{
so->btso_mrkbuf = ReadBuffer(scan->relation,
so->btso_mrkbuf = ReadBuffer(scan->indexRelation,
BufferGetBlockNumber(so->btso_curbuf));
scan->currentMarkData = scan->currentItemData;
so->mrkHeapIptr = so->curHeapIptr;
@@ -523,7 +522,7 @@ btrestrpos(PG_FUNCTION_ARGS)
/* bump pin on marked buffer */
if (ItemPointerIsValid(&(scan->currentMarkData)))
{
so->btso_curbuf = ReadBuffer(scan->relation,
so->btso_curbuf = ReadBuffer(scan->indexRelation,
BufferGetBlockNumber(so->btso_mrkbuf));
scan->currentItemData = scan->currentMarkData;
so->curHeapIptr = so->mrkHeapIptr;
@@ -549,7 +548,6 @@ btbulkdelete(PG_FUNCTION_ARGS)
BlockNumber num_pages;
double tuples_removed;
double num_index_tuples;
RetrieveIndexResult res;
IndexScanDesc scan;
BTScanOpaque so;
ItemPointer current;
@@ -569,19 +567,16 @@ btbulkdelete(PG_FUNCTION_ARGS)
* doesn't care which kind of lock it's releasing). This should
* minimize the amount of work needed per page.
*/
scan = index_beginscan(rel, false, 0, (ScanKey) NULL);
scan = index_beginscan(NULL, rel, SnapshotAny, 0, (ScanKey) NULL);
so = (BTScanOpaque) scan->opaque;
current = &(scan->currentItemData);
/* Use _bt_first to get started, then _bt_step to remaining tuples */
res = _bt_first(scan, ForwardScanDirection);
if (res != NULL)
if (_bt_first(scan, ForwardScanDirection))
{
Buffer buf;
BlockNumber lockedBlock = InvalidBlockNumber;
pfree(res);
/* we have the buffer pinned and locked */
buf = so->btso_curbuf;
Assert(BufferIsValid(buf));
@@ -683,7 +678,7 @@ btbulkdelete(PG_FUNCTION_ARGS)
static void
_bt_restscan(IndexScanDesc scan)
{
Relation rel = scan->relation;
Relation rel = scan->indexRelation;
BTScanOpaque so = (BTScanOpaque) scan->opaque;
Buffer buf = so->btso_curbuf;
Page page;

View File

@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.69 2001/10/25 05:49:21 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.70 2002/05/20 23:51:41 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -19,7 +19,7 @@
#include "access/nbtree.h"
static RetrieveIndexResult _bt_endpoint(IndexScanDesc scan, ScanDirection dir);
static bool _bt_endpoint(IndexScanDesc scan, ScanDirection dir);
/*
@@ -357,11 +357,11 @@ _bt_compare(Relation rel,
*
* On entry, we have a valid currentItemData in the scan, and a
* read lock and pin count on the page that contains that item.
* We return the next item in the scan, or NULL if no more.
* We return the next item in the scan, or false if no more.
* On successful exit, the page containing the new item is locked
* and pinned; on NULL exit, no lock or pin is held.
* and pinned; on failure exit, no lock or pin is held.
*/
RetrieveIndexResult
bool
_bt_next(IndexScanDesc scan, ScanDirection dir)
{
Relation rel;
@@ -374,7 +374,7 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
BTScanOpaque so;
bool continuescan;
rel = scan->relation;
rel = scan->indexRelation;
so = (BTScanOpaque) scan->opaque;
current = &(scan->currentItemData);
@@ -386,7 +386,7 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
{
/* step one tuple in the appropriate direction */
if (!_bt_step(scan, &buf, dir))
return (RetrieveIndexResult) NULL;
return false;
/* current is the next candidate tuple to return */
offnum = ItemPointerGetOffsetNumber(current);
@@ -397,7 +397,8 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
if (_bt_checkkeys(scan, itup, dir, &continuescan))
{
/* tuple passes all scan key conditions, so return it */
return FormRetrieveIndexResult(current, &(itup->t_tid));
scan->xs_ctup.t_self = itup->t_tid;
return true;
}
/* This tuple doesn't pass, but there might be more that do */
@@ -408,20 +409,20 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
so->btso_curbuf = InvalidBuffer;
_bt_relbuf(rel, buf);
return (RetrieveIndexResult) NULL;
return false;
}
/*
* _bt_first() -- Find the first item in a scan.
*
* We need to be clever about the type of scan, the operation it's
* performing, and the tree ordering. We return the RetrieveIndexResult
* of the first item in the tree that satisfies the qualification
* performing, and the tree ordering. We find the
* first item in the tree that satisfies the qualification
* associated with the scan descriptor. On exit, the page containing
* the current index tuple is read locked and pinned, and the scan's
* opaque data entry is updated to include the buffer.
*/
RetrieveIndexResult
bool
_bt_first(IndexScanDesc scan, ScanDirection dir)
{
Relation rel;
@@ -434,9 +435,10 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
ItemPointer current;
BlockNumber blkno;
StrategyNumber strat;
RetrieveIndexResult res;
bool res;
int32 result;
BTScanOpaque so;
bool scanFromEnd;
bool continuescan;
ScanKey scankeys = NULL;
int keysCount = 0;
@@ -445,7 +447,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
j;
StrategyNumber strat_total;
rel = scan->relation;
rel = scan->indexRelation;
so = (BTScanOpaque) scan->opaque;
/*
@@ -459,12 +461,12 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
* be satisfied (eg, x == 1 AND x > 2).
*/
if (!so->qual_ok)
return (RetrieveIndexResult) NULL;
return false;
/*
* Examine the scan keys to discover where we need to start the scan.
*/
scan->scanFromEnd = false;
scanFromEnd = false;
strat_total = BTEqualStrategyNumber;
if (so->numberOfKeys > 0)
{
@@ -511,13 +513,13 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
}
}
if (keysCount == 0)
scan->scanFromEnd = true;
scanFromEnd = true;
}
else
scan->scanFromEnd = true;
scanFromEnd = true;
/* if we just need to walk down one edge of the tree, do that */
if (scan->scanFromEnd)
if (scanFromEnd)
{
if (nKeyIs)
pfree(nKeyIs);
@@ -544,7 +546,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
pfree(nKeyIs);
pfree(scankeys);
elog(ERROR, "_bt_first: btree doesn't support is(not)null, yet");
return ((RetrieveIndexResult) NULL);
return false;
}
procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
ScanKeyEntryInitializeWithInfo(scankeys + i,
@@ -574,7 +576,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
ItemPointerSetInvalid(current);
so->btso_curbuf = InvalidBuffer;
pfree(scankeys);
return (RetrieveIndexResult) NULL;
return false;
}
/* remember which buffer we have pinned */
@@ -598,7 +600,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
* the scan strategy to find the exact place to start the scan.
*
* Note: if _bt_step fails (meaning we fell off the end of the index in
* one direction or the other), we either return NULL (no matches) or
* one direction or the other), we either return false (no matches) or
* call _bt_endpoint() to set up a scan starting at that index
* endpoint, as appropriate for the desired scan type.
*
@@ -615,7 +617,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
if (!_bt_step(scan, &buf, BackwardScanDirection))
{
pfree(scankeys);
return (RetrieveIndexResult) NULL;
return false;
}
break;
@@ -649,7 +651,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
if (!_bt_step(scan, &buf, BackwardScanDirection))
{
pfree(scankeys);
return (RetrieveIndexResult) NULL;
return false;
}
break;
@@ -664,7 +666,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
if (!_bt_step(scan, &buf, ForwardScanDirection))
{
pfree(scankeys);
return (RetrieveIndexResult) NULL;
return false;
}
offnum = ItemPointerGetOffsetNumber(current);
page = BufferGetPage(buf);
@@ -706,7 +708,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
if (!_bt_step(scan, &buf, ForwardScanDirection))
{
pfree(scankeys);
return (RetrieveIndexResult) NULL;
return false;
}
}
break;
@@ -722,7 +724,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
if (!_bt_step(scan, &buf, ForwardScanDirection))
{
pfree(scankeys);
return (RetrieveIndexResult) NULL;
return false;
}
offnum = ItemPointerGetOffsetNumber(current);
page = BufferGetPage(buf);
@@ -733,7 +735,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
if (!_bt_step(scan, &buf, ForwardScanDirection))
{
pfree(scankeys);
return (RetrieveIndexResult) NULL;
return false;
}
offnum = ItemPointerGetOffsetNumber(current);
page = BufferGetPage(buf);
@@ -752,7 +754,8 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
if (_bt_checkkeys(scan, itup, dir, &continuescan))
{
/* yes, return it */
res = FormRetrieveIndexResult(current, &(itup->t_tid));
scan->xs_ctup.t_self = itup->t_tid;
res = true;
}
else if (continuescan)
{
@@ -766,7 +769,7 @@ nomatches:
ItemPointerSetInvalid(current);
so->btso_curbuf = InvalidBuffer;
_bt_relbuf(rel, buf);
res = (RetrieveIndexResult) NULL;
res = false;
}
pfree(scankeys);
@@ -788,7 +791,7 @@ nomatches:
bool
_bt_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
{
Relation rel = scan->relation;
Relation rel = scan->indexRelation;
ItemPointer current = &(scan->currentItemData);
BTScanOpaque so = (BTScanOpaque) scan->opaque;
Page page;
@@ -902,7 +905,7 @@ _bt_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
* that the scan must start at the beginning or end of the index (for
* a forward or backward scan respectively).
*/
static RetrieveIndexResult
static bool
_bt_endpoint(IndexScanDesc scan, ScanDirection dir)
{
Relation rel;
@@ -917,10 +920,10 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
BTItem btitem;
IndexTuple itup;
BTScanOpaque so;
RetrieveIndexResult res;
bool res;
bool continuescan;
rel = scan->relation;
rel = scan->indexRelation;
current = &(scan->currentItemData);
so = (BTScanOpaque) scan->opaque;
@@ -936,7 +939,7 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
/* empty index... */
ItemPointerSetInvalid(current);
so->btso_curbuf = InvalidBuffer;
return (RetrieveIndexResult) NULL;
return false;
}
blkno = BufferGetBlockNumber(buf);
@@ -1016,7 +1019,7 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
if (start > maxoff)
{
if (!_bt_step(scan, &buf, dir))
return (RetrieveIndexResult) NULL;
return false;
start = ItemPointerGetOffsetNumber(current);
page = BufferGetPage(buf);
}
@@ -1028,7 +1031,8 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
if (_bt_checkkeys(scan, itup, dir, &continuescan))
{
/* yes, return it */
res = FormRetrieveIndexResult(current, &(itup->t_tid));
scan->xs_ctup.t_self = itup->t_tid;
res = true;
}
else if (continuescan)
{
@@ -1041,7 +1045,7 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
ItemPointerSetInvalid(current);
so->btso_curbuf = InvalidBuffer;
_bt_relbuf(rel, buf);
res = (RetrieveIndexResult) NULL;
res = false;
}
return res;

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtutils.c,v 1.47 2001/10/25 05:49:21 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtutils.c,v 1.48 2002/05/20 23:51:41 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -195,7 +195,7 @@ _bt_orderkeys(Relation relation, BTScanOpaque so)
{
ScanKeyData xform[BTMaxStrategyNumber];
bool init[BTMaxStrategyNumber];
uint16 numberOfKeys = so->numberOfKeys;
int numberOfKeys = so->numberOfKeys;
ScanKey key;
ScanKey cur;
StrategyMap map;
@@ -203,7 +203,7 @@ _bt_orderkeys(Relation relation, BTScanOpaque so)
int i,
j;
AttrNumber attno;
uint16 new_numberOfKeys;
int new_numberOfKeys;
bool allEqualSoFar;
so->qual_ok = true;
@@ -439,10 +439,10 @@ _bt_checkkeys(IndexScanDesc scan, IndexTuple tuple,
ScanDirection dir, bool *continuescan)
{
BTScanOpaque so = (BTScanOpaque) scan->opaque;
Size keysz = so->numberOfKeys;
int keysz = so->numberOfKeys;
int keysok;
TupleDesc tupdesc;
ScanKey key;
Size keysok;
*continuescan = true;
@@ -450,7 +450,7 @@ _bt_checkkeys(IndexScanDesc scan, IndexTuple tuple,
if (keysz == 0)
return true;
tupdesc = RelationGetDescr(scan->relation);
tupdesc = RelationGetDescr(scan->indexRelation);
key = so->keyData;
keysok = 0;