1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-21 16:02:15 +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

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.133 2002/05/01 01:23:37 inoue Exp $
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.134 2002/05/20 23:51:41 tgl Exp $
*
*
* INTERFACE ROUTINES
@ -24,7 +24,7 @@
* heap_rescan - restart a relation scan
* heap_endscan - end relation scan
* heap_getnext - retrieve next tuple in scan
* heap_fetch - retrive tuple with tid
* heap_fetch - retrieve tuple with tid
* heap_insert - insert tuple into a relation
* heap_delete - delete a tuple from a relation
* heap_update - replace a tuple in a relation with another tuple
@ -70,11 +70,7 @@ static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf,
* ----------------
*/
static void
initscan(HeapScanDesc scan,
Relation relation,
int atend,
unsigned nkeys,
ScanKey key)
initscan(HeapScanDesc scan, ScanKey key)
{
/*
* Make sure we have up-to-date idea of number of blocks in relation.
@ -82,7 +78,7 @@ initscan(HeapScanDesc scan,
* added while the scan is in progress will be invisible to my
* transaction anyway...
*/
relation->rd_nblocks = RelationGetNumberOfBlocks(relation);
scan->rs_rd->rd_nblocks = RelationGetNumberOfBlocks(scan->rs_rd);
scan->rs_ctup.t_datamcxt = NULL;
scan->rs_ctup.t_data = NULL;
@ -95,7 +91,7 @@ initscan(HeapScanDesc scan,
* copy the scan key, if appropriate
*/
if (key != NULL)
memmove(scan->rs_key, key, nkeys * sizeof(ScanKeyData));
memcpy(scan->rs_key, key, scan->rs_nkeys * sizeof(ScanKeyData));
}
/* ----------------
@ -185,7 +181,7 @@ heapgettup(Relation relation,
/*
* calculate next starting lineoff, given scan direction
*/
if (!dir)
if (dir == 0)
{
/*
* ``no movement'' scan direction: refetch same tuple
@ -216,8 +212,8 @@ heapgettup(Relation relation,
tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lpp);
tuple->t_len = ItemIdGetLength(lpp);
LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
return;
return;
}
else if (dir < 0)
{
@ -255,7 +251,6 @@ heapgettup(Relation relation,
OffsetNumberPrev(ItemPointerGetOffsetNumber(tid));
}
/* page and lineoff now reference the physically previous tid */
}
else
{
@ -287,7 +282,6 @@ heapgettup(Relation relation,
dp = (Page) BufferGetPage(*buffer);
lines = PageGetMaxOffsetNumber(dp);
/* page and lineoff now reference the physically next tid */
}
/* 'dir' is now non-zero */
@ -675,11 +669,8 @@ heap_openr(const char *sysRelationName, LOCKMODE lockmode)
* ----------------
*/
HeapScanDesc
heap_beginscan(Relation relation,
int atend,
Snapshot snapshot,
unsigned nkeys,
ScanKey key)
heap_beginscan(Relation relation, Snapshot snapshot,
int nkeys, ScanKey key)
{
HeapScanDesc scan;
@ -715,20 +706,20 @@ heap_beginscan(Relation relation,
scan->rs_rd = relation;
scan->rs_snapshot = snapshot;
scan->rs_nkeys = (short) nkeys;
pgstat_initstats(&scan->rs_pgstat_info, relation);
scan->rs_nkeys = nkeys;
/*
* we do this here instead of in initscan() because heap_rescan also
* calls initscan() and we don't want to allocate memory again
*/
if (nkeys)
if (nkeys > 0)
scan->rs_key = (ScanKey) palloc(sizeof(ScanKeyData) * nkeys);
else
scan->rs_key = NULL;
initscan(scan, relation, atend, nkeys, key);
pgstat_initstats(&scan->rs_pgstat_info, relation);
initscan(scan, key);
return scan;
}
@ -739,7 +730,6 @@ heap_beginscan(Relation relation,
*/
void
heap_rescan(HeapScanDesc scan,
bool scanFromEnd,
ScanKey key)
{
/*
@ -757,7 +747,7 @@ heap_rescan(HeapScanDesc scan,
/*
* reinitialize scan descriptor
*/
initscan(scan, scan->rs_rd, scanFromEnd, scan->rs_nkeys, key);
initscan(scan, key);
pgstat_reset_heap_scan(&scan->rs_pgstat_info);
}
@ -808,14 +798,14 @@ heap_endscan(HeapScanDesc scan)
#ifdef HEAPDEBUGALL
#define HEAPDEBUG_1 \
elog(LOG, "heap_getnext([%s,nkeys=%d],backw=%d) called", \
RelationGetRelationName(scan->rs_rd), scan->rs_nkeys, backw)
elog(LOG, "heap_getnext([%s,nkeys=%d],dir=%d) called", \
RelationGetRelationName(scan->rs_rd), scan->rs_nkeys, (int) direction)
#define HEAPDEBUG_2 \
elog(LOG, "heap_getnext returning EOS")
#define HEAPDEBUG_3 \
elog(LOG, "heap_getnext returning tuple");
elog(LOG, "heap_getnext returning tuple")
#else
#define HEAPDEBUG_1
#define HEAPDEBUG_2
@ -824,7 +814,7 @@ elog(LOG, "heap_getnext([%s,nkeys=%d],backw=%d) called", \
HeapTuple
heap_getnext(HeapScanDesc scan, int backw)
heap_getnext(HeapScanDesc scan, ScanDirection direction)
{
/*
* increment access statistics
@ -842,43 +832,21 @@ heap_getnext(HeapScanDesc scan, int backw)
HEAPDEBUG_1; /* heap_getnext( info ) */
if (backw)
{
/*
* handle reverse scan
*/
heapgettup(scan->rs_rd,
-1,
&(scan->rs_ctup),
&(scan->rs_cbuf),
scan->rs_snapshot,
scan->rs_nkeys,
scan->rs_key);
/*
* Note: we depend here on the -1/0/1 encoding of ScanDirection.
*/
heapgettup(scan->rs_rd,
(int) direction,
&(scan->rs_ctup),
&(scan->rs_cbuf),
scan->rs_snapshot,
scan->rs_nkeys,
scan->rs_key);
if (scan->rs_ctup.t_data == NULL && !BufferIsValid(scan->rs_cbuf))
{
HEAPDEBUG_2; /* heap_getnext returning EOS */
return NULL;
}
}
else
if (scan->rs_ctup.t_data == NULL && !BufferIsValid(scan->rs_cbuf))
{
/*
* handle forward scan
*/
heapgettup(scan->rs_rd,
1,
&(scan->rs_ctup),
&(scan->rs_cbuf),
scan->rs_snapshot,
scan->rs_nkeys,
scan->rs_key);
if (scan->rs_ctup.t_data == NULL && !BufferIsValid(scan->rs_cbuf))
{
HEAPDEBUG_2; /* heap_getnext returning EOS */
return NULL;
}
HEAPDEBUG_2; /* heap_getnext returning EOS */
return NULL;
}
pgstat_count_heap_scan(&scan->rs_pgstat_info);
@ -897,16 +865,17 @@ heap_getnext(HeapScanDesc scan, int backw)
}
/* ----------------
* heap_fetch - retrive tuple with tid
* heap_fetch - retrieve tuple with given tid
*
* Currently ignores LP_IVALID during processing!
* On entry, tuple->t_self is the TID to fetch.
*
* Because this is not part of a scan, there is no way to
* automatically lock/unlock the shared buffers.
* For this reason, we require that the user retrieve the buffer
* value, and they are required to BufferRelease() it when they
* are done. If they want to make a copy of it before releasing it,
* they can call heap_copytyple().
* If successful (ie, tuple found and passes snapshot time qual),
* then the rest of *tuple is filled in, and *userbuf is set to the
* buffer holding the tuple. A pin is obtained on the buffer; the
* caller must BufferRelease the buffer when done with the tuple.
*
* If not successful, tuple->t_data is set to NULL and *userbuf is set to
* InvalidBuffer.
* ----------------
*/
void
@ -914,7 +883,7 @@ heap_fetch(Relation relation,
Snapshot snapshot,
HeapTuple tuple,
Buffer *userbuf,
IndexScanDesc iscan)
PgStat_Info *pgstat_info)
{
ItemId lp;
Buffer buffer;
@ -936,8 +905,9 @@ heap_fetch(Relation relation,
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
if (!BufferIsValid(buffer))
elog(ERROR, "heap_fetch: %s relation: ReadBuffer(%lx) failed",
RelationGetRelationName(relation), (long) tid);
elog(ERROR, "heap_fetch: %s relation: ReadBuffer(%ld) failed",
RelationGetRelationName(relation),
(long) ItemPointerGetBlockNumber(tid));
LockBuffer(buffer, BUFFER_LOCK_SHARE);
@ -990,8 +960,12 @@ heap_fetch(Relation relation,
*/
*userbuf = buffer;
if (iscan != NULL)
pgstat_count_heap_fetch(&iscan->xs_pgstat_info);
/*
* Count the successful fetch in *pgstat_info if given,
* otherwise in the relation's default statistics area.
*/
if (pgstat_info != NULL)
pgstat_count_heap_fetch(pgstat_info);
else
pgstat_count_heap_fetch(&relation->pgstat_info);
}

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/heap/tuptoaster.c,v 1.28 2002/03/05 05:33:06 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/access/heap/tuptoaster.c,v 1.29 2002/05/20 23:51:41 tgl Exp $
*
*
* INTERFACE ROUTINES
@ -37,8 +37,6 @@
#include "utils/pg_lzcompress.h"
#ifdef TUPLE_TOASTER_ACTIVE
#undef TOAST_DEBUG
static void toast_delete(Relation rel, HeapTuple oldtup);
@ -961,14 +959,12 @@ toast_save_datum(Relation rel, Datum value)
static void
toast_delete_datum(Relation rel, Datum value)
{
register varattrib *attr = (varattrib *) value;
varattrib *attr = (varattrib *) DatumGetPointer(value);
Relation toastrel;
Relation toastidx;
ScanKeyData toastkey;
IndexScanDesc toastscan;
HeapTupleData toasttup;
RetrieveIndexResult indexRes;
Buffer buffer;
HeapTuple toasttup;
if (!VARATT_IS_EXTERNAL(attr))
return;
@ -993,22 +989,14 @@ toast_delete_datum(Relation rel, Datum value)
/*
* Find the chunks by index
*/
toastscan = index_beginscan(toastidx, false, 1, &toastkey);
while ((indexRes = index_getnext(toastscan, ForwardScanDirection)) != NULL)
toastscan = index_beginscan(toastrel, toastidx, SnapshotToast,
1, &toastkey);
while ((toasttup = index_getnext(toastscan, ForwardScanDirection)) != NULL)
{
toasttup.t_self = indexRes->heap_iptr;
heap_fetch(toastrel, SnapshotToast, &toasttup, &buffer, toastscan);
pfree(indexRes);
if (!toasttup.t_data)
continue;
/*
* Have a chunk, delete it
*/
simple_heap_delete(toastrel, &toasttup.t_self);
ReleaseBuffer(buffer);
simple_heap_delete(toastrel, &toasttup->t_self);
}
/*
@ -1034,11 +1022,8 @@ toast_fetch_datum(varattrib *attr)
Relation toastidx;
ScanKeyData toastkey;
IndexScanDesc toastscan;
HeapTupleData toasttup;
HeapTuple ttup;
TupleDesc toasttupDesc;
RetrieveIndexResult indexRes;
Buffer buffer;
varattrib *result;
int32 ressize;
int32 residx,
@ -1082,17 +1067,10 @@ toast_fetch_datum(varattrib *attr)
*/
nextidx = 0;
toastscan = index_beginscan(toastidx, false, 1, &toastkey);
while ((indexRes = index_getnext(toastscan, ForwardScanDirection)) != NULL)
toastscan = index_beginscan(toastrel, toastidx, SnapshotToast,
1, &toastkey);
while ((ttup = index_getnext(toastscan, ForwardScanDirection)) != NULL)
{
toasttup.t_self = indexRes->heap_iptr;
heap_fetch(toastrel, SnapshotToast, &toasttup, &buffer, toastscan);
pfree(indexRes);
if (toasttup.t_data == NULL)
continue;
ttup = &toasttup;
/*
* Have a chunk, extract the sequence number and the data
*/
@ -1135,7 +1113,6 @@ toast_fetch_datum(varattrib *attr)
VARATT_DATA(chunk),
chunksize);
ReleaseBuffer(buffer);
nextidx++;
}
@ -1170,16 +1147,12 @@ toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
Relation toastrel;
Relation toastidx;
ScanKeyData toastkey[3];
int nscankeys;
IndexScanDesc toastscan;
HeapTupleData toasttup;
HeapTuple ttup;
TupleDesc toasttupDesc;
RetrieveIndexResult indexRes;
Buffer buffer;
varattrib *result;
int32 attrsize;
int32 nscankeys;
int32 residx;
int32 nextidx;
int numchunks;
@ -1198,15 +1171,15 @@ toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
totalchunks = ((attrsize - 1) / TOAST_MAX_CHUNK_SIZE) + 1;
if (sliceoffset >= attrsize)
{
{
sliceoffset = 0;
length = 0;
}
}
if (((sliceoffset + length) > attrsize) || length < 0)
{
{
length = attrsize - sliceoffset;
}
}
result = (varattrib *) palloc(length + VARHDRSZ);
VARATT_SIZEP(result) = length + VARHDRSZ;
@ -1274,17 +1247,10 @@ toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
* The index is on (valueid, chunkidx) so they will come in order
*/
nextidx = startchunk;
toastscan = index_beginscan(toastidx, false, nscankeys, &toastkey[0]);
while ((indexRes = index_getnext(toastscan, ForwardScanDirection)) != NULL)
toastscan = index_beginscan(toastrel, toastidx, SnapshotToast,
nscankeys, toastkey);
while ((ttup = index_getnext(toastscan, ForwardScanDirection)) != NULL)
{
toasttup.t_self = indexRes->heap_iptr;
heap_fetch(toastrel, SnapshotToast, &toasttup, &buffer, toastscan);
pfree(indexRes);
if (toasttup.t_data == NULL)
continue;
ttup = &toasttup;
/*
* Have a chunk, extract the sequence number and the data
*/
@ -1329,7 +1295,6 @@ toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
VARATT_DATA(chunk) + chcpystrt,
(chcpyend - chcpystrt) + 1);
ReleaseBuffer(buffer);
nextidx++;
}
@ -1350,5 +1315,3 @@ toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
return result;
}
#endif /* TUPLE_TOASTER_ACTIVE */