pgindent new GIST index code, per request from Tom.

This commit is contained in:
Bruce Momjian 2005-09-22 20:44:36 +00:00
parent 08817bdb76
commit b3364fc81b
7 changed files with 1531 additions and 1194 deletions

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.50 2005/06/27 12:45:22 teodor Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.51 2005/09/22 20:44:36 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -20,64 +20,71 @@
#include "utils/memutils.h" #include "utils/memutils.h"
static OffsetNumber gistfindnext(IndexScanDesc scan, OffsetNumber n, static OffsetNumber gistfindnext(IndexScanDesc scan, OffsetNumber n,
ScanDirection dir); ScanDirection dir);
static int gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, bool ignore_killed_tuples); static int gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, bool ignore_killed_tuples);
static bool gistindex_keytest(IndexTuple tuple, IndexScanDesc scan, static bool gistindex_keytest(IndexTuple tuple, IndexScanDesc scan,
OffsetNumber offset); OffsetNumber offset);
static void static void
killtuple(Relation r, GISTScanOpaque so, ItemPointer iptr) { killtuple(Relation r, GISTScanOpaque so, ItemPointer iptr)
Buffer buffer = so->curbuf; {
Buffer buffer = so->curbuf;
for(;;) { for (;;)
Page p; {
Page p;
BlockNumber blkno; BlockNumber blkno;
OffsetNumber offset, maxoff; OffsetNumber offset,
maxoff;
LockBuffer( buffer, GIST_SHARE ); LockBuffer(buffer, GIST_SHARE);
p = (Page)BufferGetPage( buffer ); p = (Page) BufferGetPage(buffer);
if ( buffer == so->curbuf && XLByteEQ( so->stack->lsn, PageGetLSN(p) ) ) { if (buffer == so->curbuf && XLByteEQ(so->stack->lsn, PageGetLSN(p)))
{
/* page unchanged, so all is simple */ /* page unchanged, so all is simple */
offset = ItemPointerGetOffsetNumber(iptr); offset = ItemPointerGetOffsetNumber(iptr);
PageGetItemId(p, offset)->lp_flags |= LP_DELETE; PageGetItemId(p, offset)->lp_flags |= LP_DELETE;
SetBufferCommitInfoNeedsSave(buffer); SetBufferCommitInfoNeedsSave(buffer);
LockBuffer( buffer, GIST_UNLOCK ); LockBuffer(buffer, GIST_UNLOCK);
break; break;
} }
maxoff = PageGetMaxOffsetNumber( p ); maxoff = PageGetMaxOffsetNumber(p);
for(offset = FirstOffsetNumber; offset<= maxoff; offset = OffsetNumberNext(offset)) { for (offset = FirstOffsetNumber; offset <= maxoff; offset = OffsetNumberNext(offset))
IndexTuple ituple = (IndexTuple) PageGetItem(p, PageGetItemId(p, offset)); {
IndexTuple ituple = (IndexTuple) PageGetItem(p, PageGetItemId(p, offset));
if ( ItemPointerEquals( &(ituple->t_tid), iptr ) ) { if (ItemPointerEquals(&(ituple->t_tid), iptr))
{
/* found */ /* found */
PageGetItemId(p, offset)->lp_flags |= LP_DELETE; PageGetItemId(p, offset)->lp_flags |= LP_DELETE;
SetBufferCommitInfoNeedsSave(buffer); SetBufferCommitInfoNeedsSave(buffer);
LockBuffer( buffer, GIST_UNLOCK ); LockBuffer(buffer, GIST_UNLOCK);
if ( buffer != so->curbuf ) if (buffer != so->curbuf)
ReleaseBuffer( buffer ); ReleaseBuffer(buffer);
return; return;
} }
} }
/* follow right link */ /* follow right link */
/* /*
* ??? is it good? if tuple dropped by concurrent vacuum, * ??? is it good? if tuple dropped by concurrent vacuum, we will read
* we will read all leaf pages... * all leaf pages...
*/ */
blkno = GistPageGetOpaque(p)->rightlink; blkno = GistPageGetOpaque(p)->rightlink;
LockBuffer( buffer, GIST_UNLOCK ); LockBuffer(buffer, GIST_UNLOCK);
if ( buffer != so->curbuf ) if (buffer != so->curbuf)
ReleaseBuffer( buffer ); ReleaseBuffer(buffer);
if ( blkno==InvalidBlockNumber ) if (blkno == InvalidBlockNumber)
/* can't found, dropped by somebody else */ /* can't found, dropped by somebody else */
return; return;
buffer = ReadBuffer( r, blkno ); buffer = ReadBuffer(r, blkno);
} }
} }
/* /*
* gistgettuple() -- Get the next tuple in the scan * gistgettuple() -- Get the next tuple in the scan
@ -85,27 +92,27 @@ killtuple(Relation r, GISTScanOpaque so, ItemPointer iptr) {
Datum Datum
gistgettuple(PG_FUNCTION_ARGS) gistgettuple(PG_FUNCTION_ARGS)
{ {
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
ScanDirection dir = (ScanDirection) PG_GETARG_INT32(1); ScanDirection dir = (ScanDirection) PG_GETARG_INT32(1);
GISTScanOpaque so; GISTScanOpaque so;
ItemPointerData tid; ItemPointerData tid;
bool res; bool res;
so = (GISTScanOpaque) scan->opaque; so = (GISTScanOpaque) scan->opaque;
/* /*
* If we have produced an index tuple in the past and the executor * If we have produced an index tuple in the past and the executor has
* has informed us we need to mark it as "killed", do so now. * informed us we need to mark it as "killed", do so now.
*/ */
if (scan->kill_prior_tuple && ItemPointerIsValid(&(scan->currentItemData))) if (scan->kill_prior_tuple && ItemPointerIsValid(&(scan->currentItemData)))
killtuple(scan->indexRelation, so, &(scan->currentItemData)); killtuple(scan->indexRelation, so, &(scan->currentItemData));
/* /*
* Get the next tuple that matches the search key. If asked to * Get the next tuple that matches the search key. If asked to skip killed
* skip killed tuples, continue looping until we find a non-killed * tuples, continue looping until we find a non-killed tuple that matches
* tuple that matches the search key. * the search key.
*/ */
res = ( gistnext(scan, dir, &tid, 1, scan->ignore_killed_tuples) ) ? true : false; res = (gistnext(scan, dir, &tid, 1, scan->ignore_killed_tuples)) ? true : false;
PG_RETURN_BOOL(res); PG_RETURN_BOOL(res);
} }
@ -114,12 +121,12 @@ Datum
gistgetmulti(PG_FUNCTION_ARGS) gistgetmulti(PG_FUNCTION_ARGS)
{ {
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
ItemPointer tids = (ItemPointer) PG_GETARG_POINTER(1); ItemPointer tids = (ItemPointer) PG_GETARG_POINTER(1);
int32 max_tids = PG_GETARG_INT32(2); int32 max_tids = PG_GETARG_INT32(2);
int32 *returned_tids = (int32 *) PG_GETARG_POINTER(3); int32 *returned_tids = (int32 *) PG_GETARG_POINTER(3);
*returned_tids = gistnext(scan, ForwardScanDirection, tids, max_tids, false); *returned_tids = gistnext(scan, ForwardScanDirection, tids, max_tids, false);
PG_RETURN_BOOL(*returned_tids == max_tids); PG_RETURN_BOOL(*returned_tids == max_tids);
} }
@ -128,17 +135,17 @@ gistgetmulti(PG_FUNCTION_ARGS)
* either to fetch the first such tuple or subsequent matching * either to fetch the first such tuple or subsequent matching
* tuples. Returns true iff a matching tuple was found. * tuples. Returns true iff a matching tuple was found.
*/ */
static int static int
gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, bool ignore_killed_tuples) gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, bool ignore_killed_tuples)
{ {
Page p; Page p;
OffsetNumber n; OffsetNumber n;
GISTScanOpaque so; GISTScanOpaque so;
GISTSearchStack *stk; GISTSearchStack *stk;
IndexTuple it; IndexTuple it;
GISTPageOpaque opaque; GISTPageOpaque opaque;
bool resetoffset=false; bool resetoffset = false;
int ntids=0; int ntids = 0;
so = (GISTScanOpaque) scan->opaque; so = (GISTScanOpaque) scan->opaque;
@ -149,59 +156,67 @@ gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, b
Assert(so->stack == NULL); Assert(so->stack == NULL);
so->curbuf = ReadBuffer(scan->indexRelation, GIST_ROOT_BLKNO); so->curbuf = ReadBuffer(scan->indexRelation, GIST_ROOT_BLKNO);
stk = so->stack = (GISTSearchStack*) palloc0( sizeof(GISTSearchStack) ); stk = so->stack = (GISTSearchStack *) palloc0(sizeof(GISTSearchStack));
stk->next = NULL; stk->next = NULL;
stk->block = GIST_ROOT_BLKNO; stk->block = GIST_ROOT_BLKNO;
} else if ( so->curbuf == InvalidBuffer ) { }
else if (so->curbuf == InvalidBuffer)
{
return 0; return 0;
} }
for(;;) { for (;;)
{
/* First of all, we need lock buffer */ /* First of all, we need lock buffer */
Assert( so->curbuf != InvalidBuffer ); Assert(so->curbuf != InvalidBuffer);
LockBuffer( so->curbuf, GIST_SHARE ); LockBuffer(so->curbuf, GIST_SHARE);
p = BufferGetPage(so->curbuf); p = BufferGetPage(so->curbuf);
opaque = GistPageGetOpaque( p ); opaque = GistPageGetOpaque(p);
resetoffset = false; resetoffset = false;
if ( XLogRecPtrIsInvalid( so->stack->lsn ) || !XLByteEQ( so->stack->lsn, PageGetLSN(p) ) ) { if (XLogRecPtrIsInvalid(so->stack->lsn) || !XLByteEQ(so->stack->lsn, PageGetLSN(p)))
{
/* page changed from last visit or visit first time , reset offset */ /* page changed from last visit or visit first time , reset offset */
so->stack->lsn = PageGetLSN(p); so->stack->lsn = PageGetLSN(p);
resetoffset = true; resetoffset = true;
/* check page split, occured from last visit or visit to parent */ /* check page split, occured from last visit or visit to parent */
if ( !XLogRecPtrIsInvalid( so->stack->parentlsn ) && if (!XLogRecPtrIsInvalid(so->stack->parentlsn) &&
XLByteLT( so->stack->parentlsn, opaque->nsn ) && XLByteLT(so->stack->parentlsn, opaque->nsn) &&
opaque->rightlink != InvalidBlockNumber /* sanity check */ && opaque->rightlink != InvalidBlockNumber /* sanity check */ &&
(so->stack->next==NULL || so->stack->next->block != opaque->rightlink) /* check if already added */) { (so->stack->next == NULL || so->stack->next->block != opaque->rightlink) /* check if already
added */ )
{
/* detect page split, follow right link to add pages */ /* detect page split, follow right link to add pages */
stk = (GISTSearchStack*) palloc( sizeof(GISTSearchStack) ); stk = (GISTSearchStack *) palloc(sizeof(GISTSearchStack));
stk->next = so->stack->next; stk->next = so->stack->next;
stk->block = opaque->rightlink; stk->block = opaque->rightlink;
stk->parentlsn = so->stack->parentlsn; stk->parentlsn = so->stack->parentlsn;
memset( &(stk->lsn), 0, sizeof(GistNSN) ); memset(&(stk->lsn), 0, sizeof(GistNSN));
so->stack->next = stk; so->stack->next = stk;
} }
} }
/* if page is empty, then just skip it */ /* if page is empty, then just skip it */
if ( PageIsEmpty(p) ) { if (PageIsEmpty(p))
LockBuffer( so->curbuf, GIST_UNLOCK ); {
LockBuffer(so->curbuf, GIST_UNLOCK);
stk = so->stack->next; stk = so->stack->next;
pfree( so->stack ); pfree(so->stack);
so->stack = stk; so->stack = stk;
if (so->stack == NULL) { if (so->stack == NULL)
{
ReleaseBuffer(so->curbuf); ReleaseBuffer(so->curbuf);
so->curbuf = InvalidBuffer; so->curbuf = InvalidBuffer;
return ntids; return ntids;
} }
so->curbuf = ReleaseAndReadBuffer(so->curbuf, scan->indexRelation, so->curbuf = ReleaseAndReadBuffer(so->curbuf, scan->indexRelation,
stk->block); stk->block);
continue; continue;
} }
@ -215,33 +230,33 @@ gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, b
else else
{ {
n = ItemPointerGetOffsetNumber(&(scan->currentItemData)); n = ItemPointerGetOffsetNumber(&(scan->currentItemData));
if (ScanDirectionIsBackward(dir)) if (ScanDirectionIsBackward(dir))
n = OffsetNumberPrev(n); n = OffsetNumberPrev(n);
else else
n = OffsetNumberNext(n); n = OffsetNumberNext(n);
} }
/* wonderfull, we can look at page */ /* wonderfull, we can look at page */
for(;;) for (;;)
{ {
n = gistfindnext(scan, n, dir); n = gistfindnext(scan, n, dir);
if (!OffsetNumberIsValid(n)) if (!OffsetNumberIsValid(n))
{ {
/* /*
* We ran out of matching index entries on the current * We ran out of matching index entries on the current page,
* page, so pop the top stack entry and use it to continue * so pop the top stack entry and use it to continue the
* the search. * search.
*/ */
LockBuffer( so->curbuf, GIST_UNLOCK ); LockBuffer(so->curbuf, GIST_UNLOCK);
stk = so->stack->next; stk = so->stack->next;
pfree( so->stack ); pfree(so->stack);
so->stack = stk; so->stack = stk;
/* If we're out of stack entries, we're done */ /* If we're out of stack entries, we're done */
if (so->stack == NULL) if (so->stack == NULL)
{ {
ReleaseBuffer(so->curbuf); ReleaseBuffer(so->curbuf);
@ -250,8 +265,8 @@ gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, b
} }
so->curbuf = ReleaseAndReadBuffer(so->curbuf, scan->indexRelation, so->curbuf = ReleaseAndReadBuffer(so->curbuf, scan->indexRelation,
stk->block); stk->block);
/* XXX go up */ /* XXX go up */
break; break;
} }
@ -259,20 +274,22 @@ gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, b
{ {
/* /*
* We've found a matching index entry in a leaf page, so * We've found a matching index entry in a leaf page, so
* return success. Note that we keep "curbuf" pinned so * return success. Note that we keep "curbuf" pinned so that
* that we can efficiently resume the index scan later. * we can efficiently resume the index scan later.
*/ */
ItemPointerSet(&(scan->currentItemData), ItemPointerSet(&(scan->currentItemData),
BufferGetBlockNumber(so->curbuf), n); BufferGetBlockNumber(so->curbuf), n);
if ( ! ( ignore_killed_tuples && ItemIdDeleted(PageGetItemId(p, n)) ) ) { if (!(ignore_killed_tuples && ItemIdDeleted(PageGetItemId(p, n))))
{
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n)); it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
tids[ntids] = scan->xs_ctup.t_self = it->t_tid; tids[ntids] = scan->xs_ctup.t_self = it->t_tid;
ntids++; ntids++;
if ( ntids == maxtids ) { if (ntids == maxtids)
LockBuffer( so->curbuf, GIST_UNLOCK ); {
LockBuffer(so->curbuf, GIST_UNLOCK);
return ntids; return ntids;
} }
} }
@ -281,14 +298,14 @@ gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, b
{ {
/* /*
* We've found an entry in an internal node whose key is * We've found an entry in an internal node whose key is
* consistent with the search key, so push it to stack * consistent with the search key, so push it to stack
*/ */
stk = (GISTSearchStack *) palloc(sizeof(GISTSearchStack)); stk = (GISTSearchStack *) palloc(sizeof(GISTSearchStack));
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n)); it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
stk->block = ItemPointerGetBlockNumber(&(it->t_tid)); stk->block = ItemPointerGetBlockNumber(&(it->t_tid));
memset( &(stk->lsn), 0, sizeof(GistNSN) ); memset(&(stk->lsn), 0, sizeof(GistNSN));
stk->parentlsn = so->stack->lsn; stk->parentlsn = so->stack->lsn;
stk->next = so->stack->next; stk->next = so->stack->next;
@ -320,12 +337,12 @@ gistindex_keytest(IndexTuple tuple,
IndexScanDesc scan, IndexScanDesc scan,
OffsetNumber offset) OffsetNumber offset)
{ {
int keySize = scan->numberOfKeys; int keySize = scan->numberOfKeys;
ScanKey key = scan->keyData; ScanKey key = scan->keyData;
Relation r = scan->indexRelation; Relation r = scan->indexRelation;
GISTScanOpaque so; GISTScanOpaque so;
Page p; Page p;
GISTSTATE *giststate; GISTSTATE *giststate;
so = (GISTScanOpaque) scan->opaque; so = (GISTScanOpaque) scan->opaque;
giststate = so->giststate; giststate = so->giststate;
@ -334,9 +351,10 @@ gistindex_keytest(IndexTuple tuple,
IncrIndexProcessed(); IncrIndexProcessed();
/* /*
* Tuple doesn't restore after crash recovery because of inclomplete insert * Tuple doesn't restore after crash recovery because of inclomplete
*/ * insert
if ( !GistPageIsLeaf(p) && GistTupleIsInvalid(tuple) ) */
if (!GistPageIsLeaf(p) && GistTupleIsInvalid(tuple))
return true; return true;
while (keySize > 0) while (keySize > 0)
@ -366,13 +384,12 @@ gistindex_keytest(IndexTuple tuple,
FALSE, isNull); FALSE, isNull);
/* /*
* Call the Consistent function to evaluate the test. The * Call the Consistent function to evaluate the test. The arguments
* arguments are the index datum (as a GISTENTRY*), the comparison * are the index datum (as a GISTENTRY*), the comparison datum, and
* datum, and the comparison operator's strategy number and * the comparison operator's strategy number and subtype from pg_amop.
* subtype from pg_amop.
* *
* (Presently there's no need to pass the subtype since it'll always * (Presently there's no need to pass the subtype since it'll always be
* be zero, but might as well pass it for possible future use.) * zero, but might as well pass it for possible future use.)
*/ */
test = FunctionCall4(&key->sk_func, test = FunctionCall4(&key->sk_func,
PointerGetDatum(&de), PointerGetDatum(&de),
@ -399,26 +416,26 @@ gistindex_keytest(IndexTuple tuple,
static OffsetNumber static OffsetNumber
gistfindnext(IndexScanDesc scan, OffsetNumber n, ScanDirection dir) gistfindnext(IndexScanDesc scan, OffsetNumber n, ScanDirection dir)
{ {
OffsetNumber maxoff; OffsetNumber maxoff;
IndexTuple it; IndexTuple it;
GISTScanOpaque so; GISTScanOpaque so;
MemoryContext oldcxt; MemoryContext oldcxt;
Page p; Page p;
so = (GISTScanOpaque) scan->opaque; so = (GISTScanOpaque) scan->opaque;
p = BufferGetPage(so->curbuf); p = BufferGetPage(so->curbuf);
maxoff = PageGetMaxOffsetNumber(p); maxoff = PageGetMaxOffsetNumber(p);
/* /*
* Make sure we're in a short-lived memory context when we invoke * Make sure we're in a short-lived memory context when we invoke a
* a user-supplied GiST method in gistindex_keytest(), so we don't * user-supplied GiST method in gistindex_keytest(), so we don't leak
* leak memory * memory
*/ */
oldcxt = MemoryContextSwitchTo(so->tempCxt); oldcxt = MemoryContextSwitchTo(so->tempCxt);
/* /*
* If we modified the index during the scan, we may have a pointer to * If we modified the index during the scan, we may have a pointer to a
* a ghost tuple, before the scan. If this is the case, back up one. * ghost tuple, before the scan. If this is the case, back up one.
*/ */
if (so->flags & GS_CURBEFORE) if (so->flags & GS_CURBEFORE)
{ {
@ -442,9 +459,8 @@ gistfindnext(IndexScanDesc scan, OffsetNumber n, ScanDirection dir)
MemoryContextReset(so->tempCxt); MemoryContextReset(so->tempCxt);
/* /*
* If we found a matching entry, return its offset; otherwise * If we found a matching entry, return its offset; otherwise return
* return InvalidOffsetNumber to inform the caller to go to the * InvalidOffsetNumber to inform the caller to go to the next page.
* next page.
*/ */
if (n >= FirstOffsetNumber && n <= maxoff) if (n >= FirstOffsetNumber && n <= maxoff)
return n; return n;

View File

@ -10,7 +10,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/gist/gistproc.c,v 1.1 2005/07/01 19:19:02 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gistproc.c,v 1.2 2005/09/22 20:44:36 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -30,10 +30,10 @@ typedef struct
static int compare_KB(const void *a, const void *b); static int compare_KB(const void *a, const void *b);
static bool gist_box_leaf_consistent(BOX *key, BOX *query, static bool gist_box_leaf_consistent(BOX *key, BOX *query,
StrategyNumber strategy); StrategyNumber strategy);
static double size_box(Datum dbox); static double size_box(Datum dbox);
static bool rtree_internal_consistent(BOX *key, BOX *query, static bool rtree_internal_consistent(BOX *key, BOX *query,
StrategyNumber strategy); StrategyNumber strategy);
/************************************************** /**************************************************
@ -268,11 +268,11 @@ gist_box_picksplit(PG_FUNCTION_ARGS)
#define ADDLIST( list, unionD, pos, num ) do { \ #define ADDLIST( list, unionD, pos, num ) do { \
if ( pos ) { \ if ( pos ) { \
if ( (unionD)->high.x < cur->high.x ) (unionD)->high.x = cur->high.x; \ if ( (unionD)->high.x < cur->high.x ) (unionD)->high.x = cur->high.x; \
if ( (unionD)->low.x > cur->low.x ) (unionD)->low.x = cur->low.x; \ if ( (unionD)->low.x > cur->low.x ) (unionD)->low.x = cur->low.x; \
if ( (unionD)->high.y < cur->high.y ) (unionD)->high.y = cur->high.y; \ if ( (unionD)->high.y < cur->high.y ) (unionD)->high.y = cur->high.y; \
if ( (unionD)->low.y > cur->low.y ) (unionD)->low.y = cur->low.y; \ if ( (unionD)->low.y > cur->low.y ) (unionD)->low.y = cur->low.y; \
} else { \ } else { \
memcpy( (void*)(unionD), (void*) cur, sizeof( BOX ) ); \ memcpy( (void*)(unionD), (void*) cur, sizeof( BOX ) ); \
} \ } \
(list)[pos] = num; \ (list)[pos] = num; \
(pos)++; \ (pos)++; \
@ -411,62 +411,62 @@ gist_box_leaf_consistent(BOX *key, BOX *query, StrategyNumber strategy)
case RTLeftStrategyNumber: case RTLeftStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_left, retval = DatumGetBool(DirectFunctionCall2(box_left,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverLeftStrategyNumber: case RTOverLeftStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_overleft, retval = DatumGetBool(DirectFunctionCall2(box_overleft,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverlapStrategyNumber: case RTOverlapStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_overlap, retval = DatumGetBool(DirectFunctionCall2(box_overlap,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverRightStrategyNumber: case RTOverRightStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_overright, retval = DatumGetBool(DirectFunctionCall2(box_overright,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTRightStrategyNumber: case RTRightStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_right, retval = DatumGetBool(DirectFunctionCall2(box_right,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTSameStrategyNumber: case RTSameStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_same, retval = DatumGetBool(DirectFunctionCall2(box_same,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTContainsStrategyNumber: case RTContainsStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_contain, retval = DatumGetBool(DirectFunctionCall2(box_contain,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTContainedByStrategyNumber: case RTContainedByStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_contained, retval = DatumGetBool(DirectFunctionCall2(box_contained,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverBelowStrategyNumber: case RTOverBelowStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_overbelow, retval = DatumGetBool(DirectFunctionCall2(box_overbelow,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTBelowStrategyNumber: case RTBelowStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_below, retval = DatumGetBool(DirectFunctionCall2(box_below,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTAboveStrategyNumber: case RTAboveStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_above, retval = DatumGetBool(DirectFunctionCall2(box_above,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverAboveStrategyNumber: case RTOverAboveStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_overabove, retval = DatumGetBool(DirectFunctionCall2(box_overabove,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
default: default:
retval = FALSE; retval = FALSE;
@ -477,7 +477,7 @@ gist_box_leaf_consistent(BOX *key, BOX *query, StrategyNumber strategy)
static double static double
size_box(Datum dbox) size_box(Datum dbox)
{ {
BOX *box = DatumGetBoxP(dbox); BOX *box = DatumGetBoxP(dbox);
if (box == NULL || box->high.x <= box->low.x || box->high.y <= box->low.y) if (box == NULL || box->high.x <= box->low.x || box->high.y <= box->low.y)
return 0.0; return 0.0;
@ -506,58 +506,58 @@ rtree_internal_consistent(BOX *key, BOX *query, StrategyNumber strategy)
case RTLeftStrategyNumber: case RTLeftStrategyNumber:
retval = !DatumGetBool(DirectFunctionCall2(box_overright, retval = !DatumGetBool(DirectFunctionCall2(box_overright,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverLeftStrategyNumber: case RTOverLeftStrategyNumber:
retval = !DatumGetBool(DirectFunctionCall2(box_right, retval = !DatumGetBool(DirectFunctionCall2(box_right,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverlapStrategyNumber: case RTOverlapStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_overlap, retval = DatumGetBool(DirectFunctionCall2(box_overlap,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverRightStrategyNumber: case RTOverRightStrategyNumber:
retval = !DatumGetBool(DirectFunctionCall2(box_left, retval = !DatumGetBool(DirectFunctionCall2(box_left,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTRightStrategyNumber: case RTRightStrategyNumber:
retval = !DatumGetBool(DirectFunctionCall2(box_overleft, retval = !DatumGetBool(DirectFunctionCall2(box_overleft,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTSameStrategyNumber: case RTSameStrategyNumber:
case RTContainsStrategyNumber: case RTContainsStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_contain, retval = DatumGetBool(DirectFunctionCall2(box_contain,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTContainedByStrategyNumber: case RTContainedByStrategyNumber:
retval = DatumGetBool(DirectFunctionCall2(box_overlap, retval = DatumGetBool(DirectFunctionCall2(box_overlap,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverBelowStrategyNumber: case RTOverBelowStrategyNumber:
retval = !DatumGetBool(DirectFunctionCall2(box_above, retval = !DatumGetBool(DirectFunctionCall2(box_above,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTBelowStrategyNumber: case RTBelowStrategyNumber:
retval = !DatumGetBool(DirectFunctionCall2(box_overabove, retval = !DatumGetBool(DirectFunctionCall2(box_overabove,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTAboveStrategyNumber: case RTAboveStrategyNumber:
retval = !DatumGetBool(DirectFunctionCall2(box_overbelow, retval = !DatumGetBool(DirectFunctionCall2(box_overbelow,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
case RTOverAboveStrategyNumber: case RTOverAboveStrategyNumber:
retval = !DatumGetBool(DirectFunctionCall2(box_below, retval = !DatumGetBool(DirectFunctionCall2(box_below,
PointerGetDatum(key), PointerGetDatum(key),
PointerGetDatum(query))); PointerGetDatum(query)));
break; break;
default: default:
retval = FALSE; retval = FALSE;
@ -621,8 +621,8 @@ gist_poly_consistent(PG_FUNCTION_ARGS)
/* /*
* Since the operators are marked lossy anyway, we can just use * Since the operators are marked lossy anyway, we can just use
* rtree_internal_consistent even at leaf nodes. (This works * rtree_internal_consistent even at leaf nodes. (This works in part
* in part because the index entries are bounding boxes not polygons.) * because the index entries are bounding boxes not polygons.)
*/ */
result = rtree_internal_consistent(DatumGetBoxP(entry->key), result = rtree_internal_consistent(DatumGetBoxP(entry->key),
&(query->boundbox), strategy); &(query->boundbox), strategy);
@ -651,7 +651,7 @@ gist_circle_compress(PG_FUNCTION_ARGS)
retval = palloc(sizeof(GISTENTRY)); retval = palloc(sizeof(GISTENTRY));
if (DatumGetCircleP(entry->key) != NULL) if (DatumGetCircleP(entry->key) != NULL)
{ {
CIRCLE *in = DatumGetCircleP(entry->key); CIRCLE *in = DatumGetCircleP(entry->key);
BOX *r; BOX *r;
r = (BOX *) palloc(sizeof(BOX)); r = (BOX *) palloc(sizeof(BOX));
@ -683,7 +683,7 @@ Datum
gist_circle_consistent(PG_FUNCTION_ARGS) gist_circle_consistent(PG_FUNCTION_ARGS)
{ {
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
CIRCLE *query = PG_GETARG_CIRCLE_P(1); CIRCLE *query = PG_GETARG_CIRCLE_P(1);
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
BOX bbox; BOX bbox;
bool result; bool result;
@ -693,8 +693,8 @@ gist_circle_consistent(PG_FUNCTION_ARGS)
/* /*
* Since the operators are marked lossy anyway, we can just use * Since the operators are marked lossy anyway, we can just use
* rtree_internal_consistent even at leaf nodes. (This works * rtree_internal_consistent even at leaf nodes. (This works in part
* in part because the index entries are bounding boxes not circles.) * because the index entries are bounding boxes not circles.)
*/ */
bbox.high.x = query->center.x + query->radius; bbox.high.x = query->center.x + query->radius;
bbox.low.x = query->center.x - query->radius; bbox.low.x = query->center.x - query->radius;

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/gist/gistscan.c,v 1.60 2005/09/22 18:49:45 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gistscan.c,v 1.61 2005/09/22 20:44:36 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -120,11 +120,11 @@ gistrescan(PG_FUNCTION_ARGS)
scan->numberOfKeys * sizeof(ScanKeyData)); scan->numberOfKeys * sizeof(ScanKeyData));
/* /*
* Modify the scan key so that all the Consistent method is * Modify the scan key so that all the Consistent method is called for
* called for all comparisons. The original operator is passed * all comparisons. The original operator is passed to the Consistent
* to the Consistent function in the form of its strategy * function in the form of its strategy number, which is available
* number, which is available from the sk_strategy field, and * from the sk_strategy field, and its subtype from the sk_subtype
* its subtype from the sk_subtype field. * field.
*/ */
for (i = 0; i < scan->numberOfKeys; i++) for (i = 0; i < scan->numberOfKeys; i++)
scan->keyData[i].sk_func = so->giststate->consistentFn[scan->keyData[i].sk_attno - 1]; scan->keyData[i].sk_func = so->giststate->consistentFn[scan->keyData[i].sk_attno - 1];
@ -138,7 +138,7 @@ gistmarkpos(PG_FUNCTION_ARGS)
{ {
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
GISTScanOpaque so; GISTScanOpaque so;
GISTSearchStack *o, GISTSearchStack *o,
*n, *n,
*tmp; *tmp;
@ -187,7 +187,7 @@ gistrestrpos(PG_FUNCTION_ARGS)
{ {
IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
GISTScanOpaque so; GISTScanOpaque so;
GISTSearchStack *o, GISTSearchStack *o,
*n, *n,
*tmp; *tmp;
@ -308,9 +308,9 @@ ReleaseResources_gist(void)
GISTScanList next; GISTScanList next;
/* /*
* Note: this should be a no-op during normal query shutdown. However, * Note: this should be a no-op during normal query shutdown. However, in
* in an abort situation ExecutorEnd is not called and so there may be * an abort situation ExecutorEnd is not called and so there may be open
* open index scans to clean up. * index scans to clean up.
*/ */
prev = NULL; prev = NULL;
@ -338,8 +338,8 @@ gistadjscans(Relation rel, int op, BlockNumber blkno, OffsetNumber offnum, XLogR
GISTScanList l; GISTScanList l;
Oid relid; Oid relid;
if ( XLogRecPtrIsInvalid(newlsn) || XLogRecPtrIsInvalid(oldlsn) ) if (XLogRecPtrIsInvalid(newlsn) || XLogRecPtrIsInvalid(oldlsn))
return; return;
relid = RelationGetRelid(rel); relid = RelationGetRelid(rel);
for (l = GISTScans; l != NULL; l = l->gsl_next) for (l = GISTScans; l != NULL; l = l->gsl_next)
@ -365,7 +365,7 @@ gistadjone(IndexScanDesc scan,
BlockNumber blkno, BlockNumber blkno,
OffsetNumber offnum, XLogRecPtr newlsn, XLogRecPtr oldlsn) OffsetNumber offnum, XLogRecPtr newlsn, XLogRecPtr oldlsn)
{ {
GISTScanOpaque so = (GISTScanOpaque) scan->opaque ; GISTScanOpaque so = (GISTScanOpaque) scan->opaque;
adjustiptr(scan, &(scan->currentItemData), so->stack, op, blkno, offnum, newlsn, oldlsn); adjustiptr(scan, &(scan->currentItemData), so->stack, op, blkno, offnum, newlsn, oldlsn);
adjustiptr(scan, &(scan->currentMarkData), so->markstk, op, blkno, offnum, newlsn, oldlsn); adjustiptr(scan, &(scan->currentMarkData), so->markstk, op, blkno, offnum, newlsn, oldlsn);
@ -399,7 +399,8 @@ adjustiptr(IndexScanDesc scan,
{ {
case GISTOP_DEL: case GISTOP_DEL:
/* back up one if we need to */ /* back up one if we need to */
if (curoff >= offnum && XLByteEQ(stk->lsn, oldlsn) ) /* the same vesrion of page */ if (curoff >= offnum && XLByteEQ(stk->lsn, oldlsn)) /* the same vesrion of
* page */
{ {
if (curoff > FirstOffsetNumber) if (curoff > FirstOffsetNumber)
{ {
@ -409,8 +410,7 @@ adjustiptr(IndexScanDesc scan,
else else
{ {
/* /*
* remember that we're before the current * remember that we're before the current tuple
* tuple
*/ */
ItemPointerSet(iptr, blkno, FirstOffsetNumber); ItemPointerSet(iptr, blkno, FirstOffsetNumber);
if (iptr == &(scan->currentItemData)) if (iptr == &(scan->currentItemData))
@ -435,6 +435,7 @@ gistfreestack(GISTSearchStack *s)
while (s != NULL) while (s != NULL)
{ {
GISTSearchStack *p = s->next; GISTSearchStack *p = s->next;
pfree(s); pfree(s);
s = p; s = p;
} }

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/gist/gistutil.c,v 1.6 2005/09/22 18:49:45 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gistutil.c,v 1.7 2005/09/22 20:44:36 momjian Exp $
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
@ -22,9 +22,9 @@
#include "storage/freespace.h" #include "storage/freespace.h"
/* group flags ( in gistadjsubkey ) */ /* group flags ( in gistadjsubkey ) */
#define LEFT_ADDED 0x01 #define LEFT_ADDED 0x01
#define RIGHT_ADDED 0x02 #define RIGHT_ADDED 0x02
#define BOTH_ADDED ( LEFT_ADDED | RIGHT_ADDED ) #define BOTH_ADDED ( LEFT_ADDED | RIGHT_ADDED )
/* /*
@ -47,8 +47,7 @@
} while(0); } while(0);
static void static void gistpenalty(GISTSTATE *giststate, int attno,
gistpenalty(GISTSTATE *giststate, int attno,
GISTENTRY *key1, bool isNull1, GISTENTRY *key1, bool isNull1,
GISTENTRY *key2, bool isNull2, float *penalty); GISTENTRY *key2, bool isNull2, float *penalty);
@ -57,13 +56,13 @@ gistpenalty(GISTSTATE *giststate, int attno,
*/ */
OffsetNumber OffsetNumber
gistfillbuffer(Relation r, Page page, IndexTuple *itup, gistfillbuffer(Relation r, Page page, IndexTuple *itup,
int len, OffsetNumber off) int len, OffsetNumber off)
{ {
OffsetNumber l = InvalidOffsetNumber; OffsetNumber l = InvalidOffsetNumber;
int i; int i;
if ( off == InvalidOffsetNumber ) if (off == InvalidOffsetNumber)
off = ( PageIsEmpty(page) ) ? FirstOffsetNumber : off = (PageIsEmpty(page)) ? FirstOffsetNumber :
OffsetNumberNext(PageGetMaxOffsetNumber(page)); OffsetNumberNext(PageGetMaxOffsetNumber(page));
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -137,13 +136,13 @@ gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate)
GistEntryVector *evec; GistEntryVector *evec;
int i; int i;
GISTENTRY centry[INDEX_MAX_KEYS]; GISTENTRY centry[INDEX_MAX_KEYS];
IndexTuple res; IndexTuple res;
evec = (GistEntryVector *) palloc(((len == 1) ? 2 : len) * sizeof(GISTENTRY) + GEVHDRSZ); evec = (GistEntryVector *) palloc(((len == 1) ? 2 : len) * sizeof(GISTENTRY) + GEVHDRSZ);
for(i = 0; i<len; i++) for (i = 0; i < len; i++)
if ( GistTupleIsInvalid( itvec[i] ) ) if (GistTupleIsInvalid(itvec[i]))
return gist_form_invalid_tuple( InvalidBlockNumber ); return gist_form_invalid_tuple(InvalidBlockNumber);
for (i = 0; i < r->rd_att->natts; i++) for (i = 0; i < r->rd_att->natts; i++)
{ {
@ -155,6 +154,7 @@ gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate)
for (j = 0; j < len; j++) for (j = 0; j < len; j++)
{ {
bool IsNull; bool IsNull;
datum = index_getattr(itvec[j], i + 1, giststate->tupdesc, &IsNull); datum = index_getattr(itvec[j], i + 1, giststate->tupdesc, &IsNull);
if (IsNull) if (IsNull)
continue; continue;
@ -176,7 +176,7 @@ gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate)
} }
else else
{ {
int datumsize; int datumsize;
if (real_len == 1) if (real_len == 1)
{ {
@ -202,7 +202,7 @@ gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate)
} }
res = index_form_tuple(giststate->tupdesc, attr, isnull); res = index_form_tuple(giststate->tupdesc, attr, isnull);
GistTupleSetValid( res ); GistTupleSetValid(res);
return res; return res;
} }
@ -227,9 +227,9 @@ gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *gis
IndexTuple newtup = NULL; IndexTuple newtup = NULL;
int i; int i;
if ( GistTupleIsInvalid(oldtup) || GistTupleIsInvalid(addtup) ) if (GistTupleIsInvalid(oldtup) || GistTupleIsInvalid(addtup))
return gist_form_invalid_tuple( ItemPointerGetBlockNumber( &(oldtup->t_tid) ) ); return gist_form_invalid_tuple(ItemPointerGetBlockNumber(&(oldtup->t_tid)));
evec = palloc(2 * sizeof(GISTENTRY) + GEVHDRSZ); evec = palloc(2 * sizeof(GISTENTRY) + GEVHDRSZ);
evec->n = 2; evec->n = 2;
ev0p = &(evec->vector[0]); ev0p = &(evec->vector[0]);
@ -268,7 +268,7 @@ gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *gis
} }
else else
{ {
bool result; bool result;
FunctionCall3(&giststate->equalFn[i], FunctionCall3(&giststate->equalFn[i],
ev0p->key, ev0p->key,
@ -301,7 +301,7 @@ gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *gis
void void
gistunionsubkey(Relation r, GISTSTATE *giststate, IndexTuple *itvec, GIST_SPLITVEC *spl, bool isall) gistunionsubkey(Relation r, GISTSTATE *giststate, IndexTuple *itvec, GIST_SPLITVEC *spl, bool isall)
{ {
int lr; int lr;
for (lr = 0; lr < 2; lr++) for (lr = 0; lr < 2; lr++)
{ {
@ -309,7 +309,7 @@ gistunionsubkey(Relation r, GISTSTATE *giststate, IndexTuple *itvec, GIST_SPLITV
int i; int i;
Datum *attr; Datum *attr;
int len, int len,
*attrsize; *attrsize;
bool *isnull; bool *isnull;
GistEntryVector *evec; GistEntryVector *evec;
@ -354,7 +354,7 @@ gistunionsubkey(Relation r, GISTSTATE *giststate, IndexTuple *itvec, GIST_SPLITV
&(evec->vector[real_len]), &(evec->vector[real_len]),
datum, datum,
NULL, NULL, (OffsetNumber) 0, NULL, NULL, (OffsetNumber) 0,
ATTSIZE(datum, giststate->tupdesc, i + 1, IsNull), ATTSIZE(datum, giststate->tupdesc, i + 1, IsNull),
FALSE, IsNull); FALSE, IsNull);
real_len++; real_len++;
@ -402,14 +402,14 @@ gistfindgroup(GISTSTATE *giststate, GISTENTRY *valvec, GIST_SPLITVEC *spl)
int curid = 1; int curid = 1;
/* /*
* first key is always not null (see gistinsert), so we may not check * first key is always not null (see gistinsert), so we may not check for
* for nulls * nulls
*/ */
for (i = 0; i < spl->spl_nleft; i++) for (i = 0; i < spl->spl_nleft; i++)
{ {
int j; int j;
int len; int len;
bool result; bool result;
if (spl->spl_idgrp[spl->spl_left[i]]) if (spl->spl_idgrp[spl->spl_left[i]])
continue; continue;
@ -540,12 +540,12 @@ gistadjsubkey(Relation r,
for (j = 1; j < r->rd_att->natts; j++) for (j = 1; j < r->rd_att->natts; j++)
{ {
gistentryinit(entry, v->spl_lattr[j], r, NULL, gistentryinit(entry, v->spl_lattr[j], r, NULL,
(OffsetNumber) 0, v->spl_lattrsize[j], FALSE); (OffsetNumber) 0, v->spl_lattrsize[j], FALSE);
gistpenalty(giststate, j, &entry, v->spl_lisnull[j], gistpenalty(giststate, j, &entry, v->spl_lisnull[j],
&identry[j], isnull[j], &lpenalty); &identry[j], isnull[j], &lpenalty);
gistentryinit(entry, v->spl_rattr[j], r, NULL, gistentryinit(entry, v->spl_rattr[j], r, NULL,
(OffsetNumber) 0, v->spl_rattrsize[j], FALSE); (OffsetNumber) 0, v->spl_rattrsize[j], FALSE);
gistpenalty(giststate, j, &entry, v->spl_risnull[j], gistpenalty(giststate, j, &entry, v->spl_risnull[j],
&identry[j], isnull[j], &rpenalty); &identry[j], isnull[j], &rpenalty);
@ -555,8 +555,7 @@ gistadjsubkey(Relation r,
} }
/* /*
* add * add XXX: refactor this to avoid duplicating code
* XXX: refactor this to avoid duplicating code
*/ */
if (lpenalty < rpenalty) if (lpenalty < rpenalty)
{ {
@ -643,12 +642,13 @@ gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
{ {
int j; int j;
IndexTuple itup = (IndexTuple) PageGetItem(p, PageGetItemId(p, i)); IndexTuple itup = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
if ( !GistPageIsLeaf(p) && GistTupleIsInvalid(itup) ) { if (!GistPageIsLeaf(p) && GistTupleIsInvalid(itup))
{
ereport(LOG, ereport(LOG,
(errmsg("index \"%s\" needs VACUUM or REINDEX to finish crash recovery", (errmsg("index \"%s\" needs VACUUM or REINDEX to finish crash recovery",
RelationGetRelationName(r)))); RelationGetRelationName(r))));
continue; continue;
} }
sum_grow = 0; sum_grow = 0;
@ -683,7 +683,7 @@ gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
} }
} }
if ( which == InvalidOffsetNumber ) if (which == InvalidOffsetNumber)
which = FirstOffsetNumber; which = FirstOffsetNumber;
return which; return which;
@ -775,7 +775,8 @@ gistDeCompressAtt(GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p,
for (i = 0; i < r->rd_att->natts; i++) for (i = 0; i < r->rd_att->natts; i++)
{ {
Datum datum = index_getattr(tuple, i + 1, giststate->tupdesc, &isnull[i]); Datum datum = index_getattr(tuple, i + 1, giststate->tupdesc, &isnull[i]);
gistdentryinit(giststate, i, &attdata[i], gistdentryinit(giststate, i, &attdata[i],
datum, r, p, o, datum, r, p, o,
ATTSIZE(datum, giststate->tupdesc, i + 1, isnull[i]), ATTSIZE(datum, giststate->tupdesc, i + 1, isnull[i]),
@ -801,8 +802,8 @@ void
GISTInitBuffer(Buffer b, uint32 f) GISTInitBuffer(Buffer b, uint32 f)
{ {
GISTPageOpaque opaque; GISTPageOpaque opaque;
Page page; Page page;
Size pageSize; Size pageSize;
pageSize = BufferGetPageSize(b); pageSize = BufferGetPageSize(b);
page = BufferGetPage(b); page = BufferGetPage(b);
@ -811,15 +812,16 @@ GISTInitBuffer(Buffer b, uint32 f)
opaque = GistPageGetOpaque(page); opaque = GistPageGetOpaque(page);
opaque->flags = f; opaque->flags = f;
opaque->rightlink = InvalidBlockNumber; opaque->rightlink = InvalidBlockNumber;
memset( &(opaque->nsn), 0, sizeof(GistNSN) ); memset(&(opaque->nsn), 0, sizeof(GistNSN));
} }
void void
gistUserPicksplit(Relation r, GistEntryVector *entryvec, GIST_SPLITVEC *v, gistUserPicksplit(Relation r, GistEntryVector *entryvec, GIST_SPLITVEC *v,
IndexTuple *itup, int len, GISTSTATE *giststate) { IndexTuple *itup, int len, GISTSTATE *giststate)
{
/* /*
* now let the user-defined picksplit function set up the split * now let the user-defined picksplit function set up the split vector; in
* vector; in entryvec have no null value!! * entryvec have no null value!!
*/ */
FunctionCall2(&giststate->picksplitFn[0], FunctionCall2(&giststate->picksplitFn[0],
PointerGetDatum(entryvec), PointerGetDatum(entryvec),
@ -837,8 +839,8 @@ gistUserPicksplit(Relation r, GistEntryVector *entryvec, GIST_SPLITVEC *v,
v->spl_risnull[0] = false; v->spl_risnull[0] = false;
/* /*
* if index is multikey, then we must to try get smaller bounding box * if index is multikey, then we must to try get smaller bounding box for
* for subkey(s) * subkey(s)
*/ */
if (r->rd_att->natts > 1) if (r->rd_att->natts > 1)
{ {
@ -854,35 +856,42 @@ gistUserPicksplit(Relation r, GistEntryVector *entryvec, GIST_SPLITVEC *v,
gistunionsubkey(r, giststate, itup, v, false); gistunionsubkey(r, giststate, itup, v, false);
/* /*
* if possible, we insert equivalent tuples with control by * if possible, we insert equivalent tuples with control by penalty
* penalty for a subkey(s) * for a subkey(s)
*/ */
if (MaxGrpId > 1) if (MaxGrpId > 1)
gistadjsubkey(r, itup, len, v, giststate); gistadjsubkey(r, itup, len, v, giststate);
} }
} }
Buffer Buffer
gistNewBuffer(Relation r) { gistNewBuffer(Relation r)
Buffer buffer = InvalidBuffer; {
bool needLock; Buffer buffer = InvalidBuffer;
bool needLock;
while(true) { while (true)
{
BlockNumber blkno = GetFreeIndexPage(&r->rd_node); BlockNumber blkno = GetFreeIndexPage(&r->rd_node);
if (blkno == InvalidBlockNumber) if (blkno == InvalidBlockNumber)
break; break;
buffer = ReadBuffer(r, blkno); buffer = ReadBuffer(r, blkno);
if ( ConditionalLockBuffer(buffer) ) { if (ConditionalLockBuffer(buffer))
Page page = BufferGetPage(buffer); {
if ( GistPageIsDeleted( page ) ) { Page page = BufferGetPage(buffer);
GistPageSetNonDeleted( page );
if (GistPageIsDeleted(page))
{
GistPageSetNonDeleted(page);
return buffer; return buffer;
} else }
else
LockBuffer(buffer, GIST_UNLOCK); LockBuffer(buffer, GIST_UNLOCK);
} }
ReleaseBuffer( buffer ); ReleaseBuffer(buffer);
} }
needLock = !RELATION_IS_LOCAL(r); needLock = !RELATION_IS_LOCAL(r);
@ -895,6 +904,6 @@ gistNewBuffer(Relation r) {
if (needLock) if (needLock)
UnlockRelationForExtension(r, ExclusiveLock); UnlockRelationForExtension(r, ExclusiveLock);
return buffer; return buffer;
} }

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/gist/gistvacuum.c,v 1.8 2005/09/22 18:49:45 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gistvacuum.c,v 1.9 2005/09/22 20:44:36 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -25,162 +25,198 @@
#include "storage/freespace.h" #include "storage/freespace.h"
#include "storage/smgr.h" #include "storage/smgr.h"
/* filled by gistbulkdelete, cleared by gistvacuumpcleanup */ /* filled by gistbulkdelete, cleared by gistvacuumpcleanup */
static bool needFullVacuum = false; static bool needFullVacuum = false;
typedef struct { typedef struct
{
GISTSTATE giststate; GISTSTATE giststate;
Relation index; Relation index;
MemoryContext opCtx; MemoryContext opCtx;
IndexBulkDeleteResult *result; IndexBulkDeleteResult *result;
} GistVacuum; } GistVacuum;
typedef struct { typedef struct
IndexTuple *itup; {
int ituplen; IndexTuple *itup;
int ituplen;
bool emptypage; bool emptypage;
} ArrayTuple; } ArrayTuple;
static ArrayTuple static ArrayTuple
gistVacuumUpdate( GistVacuum *gv, BlockNumber blkno, bool needunion ) { gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion)
{
ArrayTuple res = {NULL, 0, false}; ArrayTuple res = {NULL, 0, false};
Buffer buffer; Buffer buffer;
Page page; Page page;
OffsetNumber i, maxoff; OffsetNumber i,
maxoff;
ItemId iid; ItemId iid;
int lenaddon=4, curlenaddon=0, ntodelete=0; int lenaddon = 4,
IndexTuple idxtuple, *addon=NULL; curlenaddon = 0,
bool needwrite=false; ntodelete = 0;
OffsetNumber todelete[MaxOffsetNumber]; IndexTuple idxtuple,
ItemPointerData *completed=NULL; *addon = NULL;
int ncompleted=0, lencompleted=16; bool needwrite = false;
OffsetNumber todelete[MaxOffsetNumber];
ItemPointerData *completed = NULL;
int ncompleted = 0,
lencompleted = 16;
buffer = ReadBuffer(gv->index, blkno); buffer = ReadBuffer(gv->index, blkno);
page = (Page) BufferGetPage(buffer); page = (Page) BufferGetPage(buffer);
maxoff = PageGetMaxOffsetNumber(page); maxoff = PageGetMaxOffsetNumber(page);
if ( GistPageIsLeaf(page) ) { if (GistPageIsLeaf(page))
if ( GistTuplesDeleted(page) ) { {
if (GistTuplesDeleted(page))
{
needunion = needwrite = true; needunion = needwrite = true;
GistClearTuplesDeleted(page); GistClearTuplesDeleted(page);
} }
} else { }
completed = (ItemPointerData*)palloc( sizeof(ItemPointerData)*lencompleted ); else
addon=(IndexTuple*)palloc(sizeof(IndexTuple)*lenaddon); {
completed = (ItemPointerData *) palloc(sizeof(ItemPointerData) * lencompleted);
addon = (IndexTuple *) palloc(sizeof(IndexTuple) * lenaddon);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) { for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
ArrayTuple chldtuple; {
bool needchildunion; ArrayTuple chldtuple;
bool needchildunion;
iid = PageGetItemId(page, i); iid = PageGetItemId(page, i);
idxtuple = (IndexTuple) PageGetItem(page, iid); idxtuple = (IndexTuple) PageGetItem(page, iid);
needchildunion = (GistTupleIsInvalid(idxtuple)) ? true : false; needchildunion = (GistTupleIsInvalid(idxtuple)) ? true : false;
if ( needchildunion ) if (needchildunion)
elog(DEBUG2, "gistVacuumUpdate: need union for block %u", elog(DEBUG2, "gistVacuumUpdate: need union for block %u",
ItemPointerGetBlockNumber(&(idxtuple->t_tid))); ItemPointerGetBlockNumber(&(idxtuple->t_tid)));
chldtuple = gistVacuumUpdate( gv, ItemPointerGetBlockNumber(&(idxtuple->t_tid)),
needchildunion );
if ( chldtuple.ituplen || chldtuple.emptypage ) {
PageIndexTupleDelete(page, i);
todelete[ ntodelete++ ] = i;
i--; maxoff--;
needwrite=needunion=true;
if ( chldtuple.ituplen ) { chldtuple = gistVacuumUpdate(gv, ItemPointerGetBlockNumber(&(idxtuple->t_tid)),
while( curlenaddon + chldtuple.ituplen >= lenaddon ) { needchildunion);
lenaddon*=2; if (chldtuple.ituplen || chldtuple.emptypage)
addon=(IndexTuple*)repalloc( addon, sizeof(IndexTuple)*lenaddon ); {
PageIndexTupleDelete(page, i);
todelete[ntodelete++] = i;
i--;
maxoff--;
needwrite = needunion = true;
if (chldtuple.ituplen)
{
while (curlenaddon + chldtuple.ituplen >= lenaddon)
{
lenaddon *= 2;
addon = (IndexTuple *) repalloc(addon, sizeof(IndexTuple) * lenaddon);
} }
memcpy( addon + curlenaddon, chldtuple.itup, chldtuple.ituplen * sizeof(IndexTuple) ); memcpy(addon + curlenaddon, chldtuple.itup, chldtuple.ituplen * sizeof(IndexTuple));
curlenaddon += chldtuple.ituplen; curlenaddon += chldtuple.ituplen;
if ( chldtuple.ituplen > 1 ) { if (chldtuple.ituplen > 1)
/* child was splitted, so we need mark completion insert(split) */ {
int j; /*
* child was splitted, so we need mark completion
* insert(split)
*/
int j;
while( ncompleted + chldtuple.ituplen > lencompleted ) { while (ncompleted + chldtuple.ituplen > lencompleted)
lencompleted*=2; {
completed = (ItemPointerData*)repalloc(completed, sizeof(ItemPointerData) * lencompleted); lencompleted *= 2;
} completed = (ItemPointerData *) repalloc(completed, sizeof(ItemPointerData) * lencompleted);
for(j=0;j<chldtuple.ituplen;j++) { }
ItemPointerCopy( &(chldtuple.itup[j]->t_tid), completed + ncompleted ); for (j = 0; j < chldtuple.ituplen; j++)
ncompleted++; {
ItemPointerCopy(&(chldtuple.itup[j]->t_tid), completed + ncompleted);
ncompleted++;
} }
} }
pfree( chldtuple.itup ); pfree(chldtuple.itup);
} }
} }
} }
if ( curlenaddon ) { if (curlenaddon)
{
/* insert updated tuples */ /* insert updated tuples */
if (gistnospace(page, addon, curlenaddon)) { if (gistnospace(page, addon, curlenaddon))
{
/* there is no space on page to insert tuples */ /* there is no space on page to insert tuples */
IndexTuple *vec; IndexTuple *vec;
SplitedPageLayout *dist=NULL,*ptr; SplitedPageLayout *dist = NULL,
int i; *ptr;
MemoryContext oldCtx = MemoryContextSwitchTo(gv->opCtx); int i;
MemoryContext oldCtx = MemoryContextSwitchTo(gv->opCtx);
vec = gistextractbuffer(buffer, &(res.ituplen)); vec = gistextractbuffer(buffer, &(res.ituplen));
vec = gistjoinvector(vec, &(res.ituplen), addon, curlenaddon); vec = gistjoinvector(vec, &(res.ituplen), addon, curlenaddon);
res.itup = gistSplit(gv->index, buffer, vec, &(res.ituplen), &dist, &(gv->giststate)); res.itup = gistSplit(gv->index, buffer, vec, &(res.ituplen), &dist, &(gv->giststate));
MemoryContextSwitchTo(oldCtx); MemoryContextSwitchTo(oldCtx);
vec = (IndexTuple*)palloc( sizeof(IndexTuple) * res.ituplen ); vec = (IndexTuple *) palloc(sizeof(IndexTuple) * res.ituplen);
for(i=0;i<res.ituplen;i++) { for (i = 0; i < res.ituplen; i++)
vec[i] = (IndexTuple)palloc( IndexTupleSize(res.itup[i]) ); {
memcpy( vec[i], res.itup[i], IndexTupleSize(res.itup[i]) ); vec[i] = (IndexTuple) palloc(IndexTupleSize(res.itup[i]));
memcpy(vec[i], res.itup[i], IndexTupleSize(res.itup[i]));
} }
res.itup = vec; res.itup = vec;
if ( !gv->index->rd_istemp ) { if (!gv->index->rd_istemp)
XLogRecPtr recptr; {
XLogRecData *rdata; XLogRecPtr recptr;
ItemPointerData key; /* set key for incomplete insert */ XLogRecData *rdata;
char *xlinfo; ItemPointerData key; /* set key for incomplete
* insert */
char *xlinfo;
ItemPointerSet(&key, blkno, TUPLE_IS_VALID); ItemPointerSet(&key, blkno, TUPLE_IS_VALID);
rdata = formSplitRdata(gv->index->rd_node, blkno, rdata = formSplitRdata(gv->index->rd_node, blkno,
&key, dist); &key, dist);
xlinfo = rdata->data; xlinfo = rdata->data;
START_CRIT_SECTION(); START_CRIT_SECTION();
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_SPLIT, rdata); recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_SPLIT, rdata);
ptr = dist; ptr = dist;
while(ptr) { while (ptr)
{
PageSetLSN(BufferGetPage(ptr->buffer), recptr); PageSetLSN(BufferGetPage(ptr->buffer), recptr);
PageSetTLI(BufferGetPage(ptr->buffer), ThisTimeLineID); PageSetTLI(BufferGetPage(ptr->buffer), ThisTimeLineID);
ptr=ptr->next; ptr = ptr->next;
} }
END_CRIT_SECTION(); END_CRIT_SECTION();
pfree( xlinfo ); pfree(xlinfo);
pfree( rdata ); pfree(rdata);
} else { }
else
{
ptr = dist; ptr = dist;
while(ptr) { while (ptr)
{
PageSetLSN(BufferGetPage(ptr->buffer), XLogRecPtrForTemp); PageSetLSN(BufferGetPage(ptr->buffer), XLogRecPtrForTemp);
ptr=ptr->next; ptr = ptr->next;
} }
} }
ptr = dist; ptr = dist;
while(ptr) { while (ptr)
if ( BufferGetBlockNumber(ptr->buffer) != blkno ) {
LockBuffer( ptr->buffer, GIST_UNLOCK ); if (BufferGetBlockNumber(ptr->buffer) != blkno)
LockBuffer(ptr->buffer, GIST_UNLOCK);
WriteBuffer(ptr->buffer); WriteBuffer(ptr->buffer);
ptr=ptr->next; ptr = ptr->next;
} }
if ( blkno == GIST_ROOT_BLKNO ) { if (blkno == GIST_ROOT_BLKNO)
ItemPointerData key; /* set key for incomplete insert */ {
ItemPointerData key; /* set key for incomplete
* insert */
ItemPointerSet(&key, blkno, TUPLE_IS_VALID); ItemPointerSet(&key, blkno, TUPLE_IS_VALID);
@ -191,82 +227,98 @@ gistVacuumUpdate( GistVacuum *gv, BlockNumber blkno, bool needunion ) {
WriteNoReleaseBuffer(buffer); WriteNoReleaseBuffer(buffer);
} }
needwrite=false; needwrite = false;
MemoryContextReset(gv->opCtx); MemoryContextReset(gv->opCtx);
needunion = false; /* gistSplit already forms unions */ needunion = false; /* gistSplit already forms unions */
} else { }
else
{
/* enough free space */ /* enough free space */
gistfillbuffer(gv->index, page, addon, curlenaddon, InvalidOffsetNumber); gistfillbuffer(gv->index, page, addon, curlenaddon, InvalidOffsetNumber);
} }
} }
} }
if ( needunion ) { if (needunion)
/* forms union for page or check empty*/ {
if ( PageIsEmpty(page) ) { /* forms union for page or check empty */
if ( blkno == GIST_ROOT_BLKNO ) { if (PageIsEmpty(page))
needwrite=true; {
GistPageSetLeaf( page ); if (blkno == GIST_ROOT_BLKNO)
} else { {
needwrite=true; needwrite = true;
res.emptypage=true; GistPageSetLeaf(page);
GistPageSetDeleted( page ); }
else
{
needwrite = true;
res.emptypage = true;
GistPageSetDeleted(page);
gv->result->pages_deleted++; gv->result->pages_deleted++;
} }
} else { }
IndexTuple *vec, tmp; else
int veclen=0; {
IndexTuple *vec,
tmp;
int veclen = 0;
MemoryContext oldCtx = MemoryContextSwitchTo(gv->opCtx); MemoryContext oldCtx = MemoryContextSwitchTo(gv->opCtx);
vec = gistextractbuffer(buffer, &veclen); vec = gistextractbuffer(buffer, &veclen);
tmp = gistunion(gv->index, vec, veclen, &(gv->giststate)); tmp = gistunion(gv->index, vec, veclen, &(gv->giststate));
MemoryContextSwitchTo(oldCtx); MemoryContextSwitchTo(oldCtx);
res.itup=(IndexTuple*)palloc( sizeof(IndexTuple) ); res.itup = (IndexTuple *) palloc(sizeof(IndexTuple));
res.ituplen = 1; res.ituplen = 1;
res.itup[0] = (IndexTuple)palloc( IndexTupleSize(tmp) ); res.itup[0] = (IndexTuple) palloc(IndexTupleSize(tmp));
memcpy( res.itup[0], tmp, IndexTupleSize(tmp) ); memcpy(res.itup[0], tmp, IndexTupleSize(tmp));
ItemPointerSetBlockNumber(&(res.itup[0]->t_tid), blkno); ItemPointerSetBlockNumber(&(res.itup[0]->t_tid), blkno);
GistTupleSetValid( res.itup[0] ); GistTupleSetValid(res.itup[0]);
MemoryContextReset(gv->opCtx); MemoryContextReset(gv->opCtx);
} }
} }
if ( needwrite ) { if (needwrite)
if ( !gv->index->rd_istemp ) { {
if (!gv->index->rd_istemp)
{
XLogRecData *rdata; XLogRecData *rdata;
XLogRecPtr recptr; XLogRecPtr recptr;
char *xlinfo; char *xlinfo;
rdata = formUpdateRdata(gv->index->rd_node, blkno, todelete, ntodelete, rdata = formUpdateRdata(gv->index->rd_node, blkno, todelete, ntodelete,
res.emptypage, addon, curlenaddon, NULL ); res.emptypage, addon, curlenaddon, NULL);
xlinfo = rdata->data; xlinfo = rdata->data;
START_CRIT_SECTION(); START_CRIT_SECTION();
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_ENTRY_UPDATE, rdata); recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_ENTRY_UPDATE, rdata);
PageSetLSN(page, recptr); PageSetLSN(page, recptr);
PageSetTLI(page, ThisTimeLineID); PageSetTLI(page, ThisTimeLineID);
END_CRIT_SECTION(); END_CRIT_SECTION();
pfree( xlinfo ); pfree(xlinfo);
pfree( rdata ); pfree(rdata);
} else }
else
PageSetLSN(page, XLogRecPtrForTemp); PageSetLSN(page, XLogRecPtrForTemp);
WriteBuffer( buffer ); WriteBuffer(buffer);
} else }
ReleaseBuffer( buffer ); else
ReleaseBuffer(buffer);
if ( ncompleted && !gv->index->rd_istemp ) if (ncompleted && !gv->index->rd_istemp)
gistxlogInsertCompletion( gv->index->rd_node, completed, ncompleted ); gistxlogInsertCompletion(gv->index->rd_node, completed, ncompleted);
for(i=0;i<curlenaddon;i++) for (i = 0; i < curlenaddon; i++)
pfree( addon[i] ); pfree(addon[i]);
if (addon) pfree(addon); if (addon)
if (completed) pfree(completed); pfree(addon);
if (completed)
pfree(completed);
return res; return res;
} }
@ -278,17 +330,23 @@ gistVacuumUpdate( GistVacuum *gv, BlockNumber blkno, bool needunion ) {
*/ */
Datum Datum
gistvacuumcleanup(PG_FUNCTION_ARGS) { gistvacuumcleanup(PG_FUNCTION_ARGS)
{
Relation rel = (Relation) PG_GETARG_POINTER(0); Relation rel = (Relation) PG_GETARG_POINTER(0);
IndexVacuumCleanupInfo *info = (IndexVacuumCleanupInfo *) PG_GETARG_POINTER(1); IndexVacuumCleanupInfo *info = (IndexVacuumCleanupInfo *) PG_GETARG_POINTER(1);
IndexBulkDeleteResult *stats = (IndexBulkDeleteResult *) PG_GETARG_POINTER(2); IndexBulkDeleteResult *stats = (IndexBulkDeleteResult *) PG_GETARG_POINTER(2);
BlockNumber npages, blkno; BlockNumber npages,
BlockNumber nFreePages, *freePages, maxFreePages; blkno;
BlockNumber lastBlock = GIST_ROOT_BLKNO, lastFilledBlock = GIST_ROOT_BLKNO; BlockNumber nFreePages,
bool needLock; *freePages,
maxFreePages;
BlockNumber lastBlock = GIST_ROOT_BLKNO,
lastFilledBlock = GIST_ROOT_BLKNO;
bool needLock;
/* gistVacuumUpdate may cause hard work */ /* gistVacuumUpdate may cause hard work */
if ( info->vacuum_full ) { if (info->vacuum_full)
{
GistVacuum gv; GistVacuum gv;
ArrayTuple res; ArrayTuple res;
@ -300,17 +358,20 @@ gistvacuumcleanup(PG_FUNCTION_ARGS) {
gv.result = stats; gv.result = stats;
/* walk through the entire index for update tuples */ /* walk through the entire index for update tuples */
res = gistVacuumUpdate( &gv, GIST_ROOT_BLKNO, false ); res = gistVacuumUpdate(&gv, GIST_ROOT_BLKNO, false);
/* cleanup */ /* cleanup */
if (res.itup) { if (res.itup)
int i; {
for(i=0;i<res.ituplen;i++) int i;
pfree( res.itup[i] );
pfree( res.itup ); for (i = 0; i < res.ituplen; i++)
pfree(res.itup[i]);
pfree(res.itup);
} }
freeGISTstate(&(gv.giststate)); freeGISTstate(&(gv.giststate));
MemoryContextDelete(gv.opCtx); MemoryContextDelete(gv.opCtx);
} else if (needFullVacuum) }
else if (needFullVacuum)
ereport(NOTICE, ereport(NOTICE,
(errmsg("index \"%s\" needs VACUUM FULL or REINDEX to finish crash recovery", (errmsg("index \"%s\" needs VACUUM FULL or REINDEX to finish crash recovery",
RelationGetRelationName(rel)))); RelationGetRelationName(rel))));
@ -318,8 +379,8 @@ gistvacuumcleanup(PG_FUNCTION_ARGS) {
needFullVacuum = false; needFullVacuum = false;
needLock = !RELATION_IS_LOCAL(rel); needLock = !RELATION_IS_LOCAL(rel);
if ( info->vacuum_full ) if (info->vacuum_full)
needLock = false; /* relation locked with AccessExclusiveLock */ needLock = false; /* relation locked with AccessExclusiveLock */
/* try to find deleted pages */ /* try to find deleted pages */
if (needLock) if (needLock)
@ -329,45 +390,52 @@ gistvacuumcleanup(PG_FUNCTION_ARGS) {
UnlockRelationForExtension(rel, ExclusiveLock); UnlockRelationForExtension(rel, ExclusiveLock);
maxFreePages = npages; maxFreePages = npages;
if ( maxFreePages > MaxFSMPages ) if (maxFreePages > MaxFSMPages)
maxFreePages = MaxFSMPages; maxFreePages = MaxFSMPages;
nFreePages = 0; nFreePages = 0;
freePages = (BlockNumber*) palloc (sizeof(BlockNumber) * maxFreePages); freePages = (BlockNumber *) palloc(sizeof(BlockNumber) * maxFreePages);
for(blkno=GIST_ROOT_BLKNO+1;blkno<npages;blkno++) { for (blkno = GIST_ROOT_BLKNO + 1; blkno < npages; blkno++)
Buffer buffer = ReadBuffer(rel, blkno); {
Page page; Buffer buffer = ReadBuffer(rel, blkno);
Page page;
LockBuffer( buffer, GIST_SHARE ); LockBuffer(buffer, GIST_SHARE);
page=(Page)BufferGetPage(buffer); page = (Page) BufferGetPage(buffer);
if ( GistPageIsDeleted(page) ) { if (GistPageIsDeleted(page))
if (nFreePages < maxFreePages) { {
freePages[ nFreePages ] = blkno; if (nFreePages < maxFreePages)
{
freePages[nFreePages] = blkno;
nFreePages++; nFreePages++;
} }
} else }
else
lastFilledBlock = blkno; lastFilledBlock = blkno;
LockBuffer( buffer, GIST_UNLOCK ); LockBuffer(buffer, GIST_UNLOCK);
ReleaseBuffer(buffer); ReleaseBuffer(buffer);
} }
lastBlock = npages-1; lastBlock = npages - 1;
if ( info->vacuum_full && nFreePages>0 ) { /* try to truncate index */ if (info->vacuum_full && nFreePages > 0)
int i; { /* try to truncate index */
for(i=0;i<nFreePages;i++) int i;
if ( freePages[i] >= lastFilledBlock ) {
for (i = 0; i < nFreePages; i++)
if (freePages[i] >= lastFilledBlock)
{
nFreePages = i; nFreePages = i;
break; break;
} }
if ( lastBlock > lastFilledBlock ) if (lastBlock > lastFilledBlock)
RelationTruncate( rel, lastFilledBlock+1 ); RelationTruncate(rel, lastFilledBlock + 1);
stats->pages_removed = lastBlock - lastFilledBlock; stats->pages_removed = lastBlock - lastFilledBlock;
} }
RecordIndexFreeSpace( &rel->rd_node, nFreePages, freePages ); RecordIndexFreeSpace(&rel->rd_node, nFreePages, freePages);
pfree( freePages ); pfree(freePages);
/* return statistics */ /* return statistics */
stats->pages_free = nFreePages; stats->pages_free = nFreePages;
@ -378,33 +446,37 @@ gistvacuumcleanup(PG_FUNCTION_ARGS) {
UnlockRelationForExtension(rel, ExclusiveLock); UnlockRelationForExtension(rel, ExclusiveLock);
if (info->vacuum_full) if (info->vacuum_full)
UnlockRelation(rel, AccessExclusiveLock); UnlockRelation(rel, AccessExclusiveLock);
PG_RETURN_POINTER(stats); PG_RETURN_POINTER(stats);
} }
typedef struct GistBDItem { typedef struct GistBDItem
{
GistNSN parentlsn; GistNSN parentlsn;
BlockNumber blkno; BlockNumber blkno;
struct GistBDItem *next; struct GistBDItem *next;
} GistBDItem; } GistBDItem;
static void static void
pushStackIfSplited(Page page, GistBDItem *stack) { pushStackIfSplited(Page page, GistBDItem *stack)
{
GISTPageOpaque opaque = GistPageGetOpaque(page); GISTPageOpaque opaque = GistPageGetOpaque(page);
if ( stack->blkno!=GIST_ROOT_BLKNO && !XLogRecPtrIsInvalid( stack->parentlsn ) && if (stack->blkno != GIST_ROOT_BLKNO && !XLogRecPtrIsInvalid(stack->parentlsn) &&
XLByteLT( stack->parentlsn, opaque->nsn) && XLByteLT(stack->parentlsn, opaque->nsn) &&
opaque->rightlink != InvalidBlockNumber /* sanity check */ ) { opaque->rightlink != InvalidBlockNumber /* sanity check */ )
{
/* split page detected, install right link to the stack */ /* split page detected, install right link to the stack */
GistBDItem *ptr = (GistBDItem*) palloc(sizeof(GistBDItem)); GistBDItem *ptr = (GistBDItem *) palloc(sizeof(GistBDItem));
ptr->blkno = opaque->rightlink; ptr->blkno = opaque->rightlink;
ptr->parentlsn = stack->parentlsn; ptr->parentlsn = stack->parentlsn;
ptr->next = stack->next; ptr->next = stack->next;
stack->next = ptr; stack->next = ptr;
} }
} }
/* /*
@ -416,38 +488,44 @@ pushStackIfSplited(Page page, GistBDItem *stack) {
* Result: a palloc'd struct containing statistical info for VACUUM displays. * Result: a palloc'd struct containing statistical info for VACUUM displays.
*/ */
Datum Datum
gistbulkdelete(PG_FUNCTION_ARGS) { gistbulkdelete(PG_FUNCTION_ARGS)
{
Relation rel = (Relation) PG_GETARG_POINTER(0); Relation rel = (Relation) PG_GETARG_POINTER(0);
IndexBulkDeleteCallback callback = (IndexBulkDeleteCallback) PG_GETARG_POINTER(1); IndexBulkDeleteCallback callback = (IndexBulkDeleteCallback) PG_GETARG_POINTER(1);
void* callback_state = (void *) PG_GETARG_POINTER(2); void *callback_state = (void *) PG_GETARG_POINTER(2);
IndexBulkDeleteResult *result = (IndexBulkDeleteResult*)palloc0(sizeof(IndexBulkDeleteResult)); IndexBulkDeleteResult *result = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
GistBDItem *stack, *ptr; GistBDItem *stack,
bool needLock; *ptr;
bool needLock;
stack = (GistBDItem*) palloc0(sizeof(GistBDItem));
stack = (GistBDItem *) palloc0(sizeof(GistBDItem));
stack->blkno = GIST_ROOT_BLKNO; stack->blkno = GIST_ROOT_BLKNO;
needFullVacuum = false; needFullVacuum = false;
while( stack ) { while (stack)
Buffer buffer = ReadBuffer(rel, stack->blkno); {
Page page; Buffer buffer = ReadBuffer(rel, stack->blkno);
OffsetNumber i, maxoff; Page page;
OffsetNumber i,
maxoff;
IndexTuple idxtuple; IndexTuple idxtuple;
ItemId iid; ItemId iid;
LockBuffer(buffer, GIST_SHARE); LockBuffer(buffer, GIST_SHARE);
page = (Page) BufferGetPage(buffer); page = (Page) BufferGetPage(buffer);
if ( GistPageIsLeaf(page) ) { if (GistPageIsLeaf(page))
{
OffsetNumber todelete[MaxOffsetNumber]; OffsetNumber todelete[MaxOffsetNumber];
int ntodelete = 0; int ntodelete = 0;
LockBuffer(buffer, GIST_UNLOCK); LockBuffer(buffer, GIST_UNLOCK);
LockBuffer(buffer, GIST_EXCLUSIVE); LockBuffer(buffer, GIST_EXCLUSIVE);
page = (Page) BufferGetPage(buffer); page = (Page) BufferGetPage(buffer);
if ( stack->blkno==GIST_ROOT_BLKNO && !GistPageIsLeaf(page) ) { if (stack->blkno == GIST_ROOT_BLKNO && !GistPageIsLeaf(page))
{
/* the only root can become non-leaf during relock */ /* the only root can become non-leaf during relock */
LockBuffer(buffer, GIST_UNLOCK); LockBuffer(buffer, GIST_UNLOCK);
ReleaseBuffer(buffer); ReleaseBuffer(buffer);
@ -455,37 +533,46 @@ gistbulkdelete(PG_FUNCTION_ARGS) {
continue; continue;
} }
/* check for split proceeded after look at parent, /*
we should check it after relock */ * check for split proceeded after look at parent, we should check
* it after relock
*/
pushStackIfSplited(page, stack); pushStackIfSplited(page, stack);
maxoff = PageGetMaxOffsetNumber(page); maxoff = PageGetMaxOffsetNumber(page);
for(i=FirstOffsetNumber;i<=maxoff;i=OffsetNumberNext(i)) { for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
iid = PageGetItemId(page, i); {
iid = PageGetItemId(page, i);
idxtuple = (IndexTuple) PageGetItem(page, iid); idxtuple = (IndexTuple) PageGetItem(page, iid);
if ( callback(&(idxtuple->t_tid), callback_state) ) { if (callback(&(idxtuple->t_tid), callback_state))
{
PageIndexTupleDelete(page, i); PageIndexTupleDelete(page, i);
todelete[ ntodelete ] = i; todelete[ntodelete] = i;
i--; maxoff--; ntodelete++; i--;
maxoff--;
ntodelete++;
result->tuples_removed += 1; result->tuples_removed += 1;
Assert( maxoff == PageGetMaxOffsetNumber(page) ); Assert(maxoff == PageGetMaxOffsetNumber(page));
} else }
else
result->num_index_tuples += 1; result->num_index_tuples += 1;
} }
if ( ntodelete ) { if (ntodelete)
{
GistMarkTuplesDeleted(page); GistMarkTuplesDeleted(page);
if (!rel->rd_istemp ) { if (!rel->rd_istemp)
{
XLogRecData *rdata; XLogRecData *rdata;
XLogRecPtr recptr; XLogRecPtr recptr;
gistxlogEntryUpdate *xlinfo; gistxlogEntryUpdate *xlinfo;
rdata = formUpdateRdata(rel->rd_node, stack->blkno, todelete, ntodelete, rdata = formUpdateRdata(rel->rd_node, stack->blkno, todelete, ntodelete,
false, NULL, 0, NULL); false, NULL, 0, NULL);
xlinfo = (gistxlogEntryUpdate*)rdata->data; xlinfo = (gistxlogEntryUpdate *) rdata->data;
START_CRIT_SECTION(); START_CRIT_SECTION();
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_ENTRY_UPDATE, rdata); recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_ENTRY_UPDATE, rdata);
@ -493,39 +580,43 @@ gistbulkdelete(PG_FUNCTION_ARGS) {
PageSetTLI(page, ThisTimeLineID); PageSetTLI(page, ThisTimeLineID);
END_CRIT_SECTION(); END_CRIT_SECTION();
pfree( xlinfo ); pfree(xlinfo);
pfree( rdata ); pfree(rdata);
} else }
else
PageSetLSN(page, XLogRecPtrForTemp); PageSetLSN(page, XLogRecPtrForTemp);
WriteNoReleaseBuffer( buffer ); WriteNoReleaseBuffer(buffer);
} }
} else { }
else
{
/* check for split proceeded after look at parent */ /* check for split proceeded after look at parent */
pushStackIfSplited(page, stack); pushStackIfSplited(page, stack);
maxoff = PageGetMaxOffsetNumber(page); maxoff = PageGetMaxOffsetNumber(page);
for(i=FirstOffsetNumber;i<=maxoff;i=OffsetNumberNext(i)) { for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
iid = PageGetItemId(page, i); iid = PageGetItemId(page, i);
idxtuple = (IndexTuple) PageGetItem(page, iid); idxtuple = (IndexTuple) PageGetItem(page, iid);
ptr = (GistBDItem*) palloc(sizeof(GistBDItem)); ptr = (GistBDItem *) palloc(sizeof(GistBDItem));
ptr->blkno = ItemPointerGetBlockNumber( &(idxtuple->t_tid) ); ptr->blkno = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
ptr->parentlsn = PageGetLSN( page ); ptr->parentlsn = PageGetLSN(page);
ptr->next = stack->next; ptr->next = stack->next;
stack->next = ptr; stack->next = ptr;
if ( GistTupleIsInvalid(idxtuple) ) if (GistTupleIsInvalid(idxtuple))
needFullVacuum = true; needFullVacuum = true;
} }
} }
LockBuffer( buffer, GIST_UNLOCK ); LockBuffer(buffer, GIST_UNLOCK);
ReleaseBuffer( buffer ); ReleaseBuffer(buffer);
ptr = stack->next; ptr = stack->next;
pfree( stack ); pfree(stack);
stack = ptr; stack = ptr;
vacuum_delay_point(); vacuum_delay_point();
@ -539,6 +630,5 @@ gistbulkdelete(PG_FUNCTION_ARGS) {
if (needLock) if (needLock)
UnlockRelationForExtension(rel, ExclusiveLock); UnlockRelationForExtension(rel, ExclusiveLock);
PG_RETURN_POINTER( result ); PG_RETURN_POINTER(result);
} }

File diff suppressed because it is too large Load Diff