Commit 8876e37d authored by Teodor Sigaev's avatar Teodor Sigaev

Reduce size of critial section during vacuum full, critical

sections now isn't nested. All user-defined functions now is
called outside critsections. Small improvements in WAL
protocol.

TODO: improve XLOG replay
parent 815f5840
...@@ -8,7 +8,7 @@ ...@@ -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/gist.c,v 1.134 2006/05/10 23:18:38 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gist.c,v 1.135 2006/05/17 16:34:59 teodor Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -347,7 +347,7 @@ gistplacetopage(GISTInsertState *state, GISTSTATE *giststate) ...@@ -347,7 +347,7 @@ gistplacetopage(GISTInsertState *state, GISTSTATE *giststate)
* Form index tuples vector to split: * Form index tuples vector to split:
* remove old tuple if t's needed and add new tuples to vector * remove old tuple if t's needed and add new tuples to vector
*/ */
itvec = gistextractbuffer(state->stack->buffer, &tlen); itvec = gistextractpage(state->stack->page, &tlen);
if ( !is_leaf ) { if ( !is_leaf ) {
/* on inner page we should remove old tuple */ /* on inner page we should remove old tuple */
int pos = state->stack->childoffnum - FirstOffsetNumber; int pos = state->stack->childoffnum - FirstOffsetNumber;
...@@ -501,7 +501,7 @@ gistplacetopage(GISTInsertState *state, GISTSTATE *giststate) ...@@ -501,7 +501,7 @@ gistplacetopage(GISTInsertState *state, GISTSTATE *giststate)
} }
rdata = formUpdateRdata(state->r->rd_node, state->stack->buffer, rdata = formUpdateRdata(state->r->rd_node, state->stack->buffer,
offs, noffs, false, offs, noffs,
state->itup, state->ituplen, state->itup, state->ituplen,
&(state->key)); &(state->key));
...@@ -1157,7 +1157,7 @@ gistnewroot(Relation r, Buffer buffer, IndexTuple *itup, int len, ItemPointer ke ...@@ -1157,7 +1157,7 @@ gistnewroot(Relation r, Buffer buffer, IndexTuple *itup, int len, ItemPointer ke
XLogRecData *rdata; XLogRecData *rdata;
rdata = formUpdateRdata(r->rd_node, buffer, rdata = formUpdateRdata(r->rd_node, buffer,
NULL, 0, false, NULL, 0,
itup, len, key); itup, len, key);
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_NEW_ROOT, rdata); recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_NEW_ROOT, rdata);
......
...@@ -8,7 +8,7 @@ ...@@ -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.11 2006/05/10 09:19:54 teodor Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gistutil.c,v 1.12 2006/05/17 16:34:59 teodor Exp $
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -112,18 +112,17 @@ gistfitpage(IndexTuple *itvec, int len) { ...@@ -112,18 +112,17 @@ gistfitpage(IndexTuple *itvec, int len) {
* Read buffer into itup vector * Read buffer into itup vector
*/ */
IndexTuple * IndexTuple *
gistextractbuffer(Buffer buffer, int *len /* out */ ) gistextractpage(Page page, int *len /* out */ )
{ {
OffsetNumber i, OffsetNumber i,
maxoff; maxoff;
IndexTuple *itvec; IndexTuple *itvec;
Page p = (Page) BufferGetPage(buffer);
maxoff = PageGetMaxOffsetNumber(p); maxoff = PageGetMaxOffsetNumber(page);
*len = maxoff; *len = maxoff;
itvec = palloc(sizeof(IndexTuple) * maxoff); itvec = palloc(sizeof(IndexTuple) * maxoff);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
itvec[i - FirstOffsetNumber] = (IndexTuple) PageGetItem(p, PageGetItemId(p, i)); itvec[i - FirstOffsetNumber] = (IndexTuple) PageGetItem(page, PageGetItemId(page, i));
return itvec; return itvec;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -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.20 2006/05/10 09:19:54 teodor Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gistvacuum.c,v 1.21 2006/05/17 16:34:59 teodor Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -47,134 +47,122 @@ typedef struct ...@@ -47,134 +47,122 @@ typedef struct
bool emptypage; bool emptypage;
} ArrayTuple; } ArrayTuple;
/*
* Make union of keys on page
*/
static IndexTuple
PageMakeUnionKey(GistVacuum *gv, Buffer buffer) {
Page page = BufferGetPage( buffer );
IndexTuple *vec,
tmp, res;
int veclen = 0;
MemoryContext oldCtx = MemoryContextSwitchTo(gv->opCtx);
static ArrayTuple vec = gistextractpage(page, &veclen);
gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion) /* we call gistunion() in temprorary context because user-defined functions called in gistunion()
{ may do not free all memory */
ArrayTuple res = {NULL, 0, false}; tmp = gistunion(gv->index, vec, veclen, &(gv->giststate));
MemoryContextSwitchTo(oldCtx);
res = (IndexTuple) palloc(IndexTupleSize(tmp));
memcpy(res, tmp, IndexTupleSize(tmp));
ItemPointerSetBlockNumber(&(res->t_tid), BufferGetBlockNumber(buffer));
GistTupleSetValid(res);
MemoryContextReset(gv->opCtx);
return res;
}
static void
gistDeleteSubtree( GistVacuum *gv, BlockNumber blkno ) {
Buffer buffer; Buffer buffer;
Page page; Page page;
OffsetNumber i,
maxoff;
ItemId iid;
int lenaddon = 4,
curlenaddon = 0,
ntodelete = 0;
IndexTuple idxtuple,
*addon = NULL;
bool needwrite = false;
OffsetNumber todelete[MaxOffsetNumber];
ItemPointerData *completed = NULL;
int ncompleted = 0,
lencompleted = 16;
vacuum_delay_point();
buffer = ReadBuffer(gv->index, blkno); buffer = ReadBuffer(gv->index, blkno);
LockBuffer(buffer, GIST_EXCLUSIVE); LockBuffer(buffer, GIST_EXCLUSIVE);
gistcheckpage(gv->index, buffer);
page = (Page) BufferGetPage(buffer); page = (Page) BufferGetPage(buffer);
maxoff = PageGetMaxOffsetNumber(page);
/* if ( !GistPageIsLeaf(page) ) {
* XXX need to reduce scope of changes to page so we can make this int i;
* critical section less extensive
*/
START_CRIT_SECTION();
if (GistPageIsLeaf(page)) for (i = FirstOffsetNumber; i <= PageGetMaxOffsetNumber(page); i = OffsetNumberNext(i)) {
{ ItemId iid = PageGetItemId(page, i);
if (GistTuplesDeleted(page)) IndexTuple idxtuple = (IndexTuple) PageGetItem(page, iid);
needunion = needwrite = true; gistDeleteSubtree(gv, ItemPointerGetBlockNumber(&(idxtuple->t_tid)));
}
} }
else
{
completed = (ItemPointerData *) palloc(sizeof(ItemPointerData) * lencompleted);
addon = (IndexTuple *) palloc(sizeof(IndexTuple) * lenaddon);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) START_CRIT_SECTION();
{
ArrayTuple chldtuple;
bool needchildunion;
iid = PageGetItemId(page, i); MarkBufferDirty(buffer);
idxtuple = (IndexTuple) PageGetItem(page, iid);
needchildunion = (GistTupleIsInvalid(idxtuple)) ? true : false;
if (needchildunion) page = (Page) BufferGetPage(buffer);
elog(DEBUG2, "gistVacuumUpdate: need union for block %u", GistPageSetDeleted(page);
ItemPointerGetBlockNumber(&(idxtuple->t_tid))); gv->result->std.pages_deleted++;
chldtuple = gistVacuumUpdate(gv, ItemPointerGetBlockNumber(&(idxtuple->t_tid)), if (!gv->index->rd_istemp)
needchildunion);
if (chldtuple.ituplen || chldtuple.emptypage)
{ {
PageIndexTupleDelete(page, i); XLogRecData rdata;
todelete[ntodelete++] = i; XLogRecPtr recptr;
i--; gistxlogPageDelete xlrec;
maxoff--;
needwrite = needunion = true;
if (chldtuple.ituplen) xlrec.node = gv->index->rd_node;
{ xlrec.blkno = blkno;
while (curlenaddon + chldtuple.ituplen >= lenaddon)
{ rdata.buffer = InvalidBuffer;
lenaddon *= 2; rdata.data = (char *) &xlrec;
addon = (IndexTuple *) repalloc(addon, sizeof(IndexTuple) * lenaddon); rdata.len = sizeof(gistxlogPageDelete);
rdata.next = NULL;
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_DELETE, &rdata);
PageSetLSN(page, recptr);
PageSetTLI(page, ThisTimeLineID);
} }
else
PageSetLSN(page, XLogRecPtrForTemp);
memcpy(addon + curlenaddon, chldtuple.itup, chldtuple.ituplen * sizeof(IndexTuple)); END_CRIT_SECTION();
curlenaddon += chldtuple.ituplen; UnlockReleaseBuffer(buffer);
}
if (chldtuple.ituplen > 1) static Page
{ GistPageGetCopyPage( Page page ) {
/* Size pageSize = PageGetPageSize( page );
* child was split, so we need mark completion Page tmppage;
* insert(split)
*/
int j;
while (ncompleted + chldtuple.ituplen > lencompleted) tmppage=(Page)palloc( pageSize );
{ memcpy( tmppage, page, pageSize );
lencompleted *= 2;
completed = (ItemPointerData *) repalloc(completed, sizeof(ItemPointerData) * lencompleted);
}
for (j = 0; j < chldtuple.ituplen; j++)
{
ItemPointerCopy(&(chldtuple.itup[j]->t_tid), completed + ncompleted);
ncompleted++;
}
}
pfree(chldtuple.itup);
}
}
}
if (curlenaddon) return tmppage;
{ }
/* insert updated tuples */
if (gistnospace(page, addon, curlenaddon, InvalidOffsetNumber)) static ArrayTuple
{ vacuumSplitPage(GistVacuum *gv, Page tempPage, Buffer buffer, IndexTuple *addon, int curlenaddon) {
/* there is no space on page to insert tuples */ ArrayTuple res = {NULL, 0, false};
IndexTuple *vec; IndexTuple *vec;
SplitedPageLayout *dist = NULL, SplitedPageLayout *dist = NULL,
*ptr; *ptr;
int i, veclen=0; int i, veclen=0;
BlockNumber blkno = BufferGetBlockNumber(buffer);
MemoryContext oldCtx = MemoryContextSwitchTo(gv->opCtx); MemoryContext oldCtx = MemoryContextSwitchTo(gv->opCtx);
vec = gistextractbuffer(buffer, &veclen); vec = gistextractpage(tempPage, &veclen);
vec = gistjoinvector(vec, &veclen, addon, curlenaddon); vec = gistjoinvector(vec, &veclen, addon, curlenaddon);
dist = gistSplit(gv->index, page, vec, veclen, &(gv->giststate)); dist = gistSplit(gv->index, tempPage, vec, veclen, &(gv->giststate));
MemoryContextSwitchTo(oldCtx); MemoryContextSwitchTo(oldCtx);
if (blkno != GIST_ROOT_BLKNO) { if (blkno != GIST_ROOT_BLKNO) {
/* if non-root split then we should not allocate new buffer */ /* if non-root split then we should not allocate new buffer */
dist->buffer = buffer; dist->buffer = buffer;
dist->page = BufferGetPage(dist->buffer); dist->page = tempPage;
/* during vacuum we never split leaf page */
GistPageGetOpaque(dist->page)->flags = 0; GistPageGetOpaque(dist->page)->flags = 0;
} } else
pfree(tempPage);
res.itup = (IndexTuple *) palloc(sizeof(IndexTuple) * veclen); res.itup = (IndexTuple *) palloc(sizeof(IndexTuple) * veclen);
res.ituplen = 0; res.ituplen = 0;
...@@ -201,8 +189,19 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion) ...@@ -201,8 +189,19 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion)
res.itup[ res.ituplen ] = (IndexTuple)palloc(IndexTupleSize(ptr->itup)); res.itup[ res.ituplen ] = (IndexTuple)palloc(IndexTupleSize(ptr->itup));
memcpy( res.itup[ res.ituplen ], ptr->itup, IndexTupleSize(ptr->itup) ); memcpy( res.itup[ res.ituplen ], ptr->itup, IndexTupleSize(ptr->itup) );
res.ituplen++; res.ituplen++;
}
START_CRIT_SECTION();
for (ptr = dist; ptr; ptr = ptr->next) {
MarkBufferDirty(ptr->buffer); MarkBufferDirty(ptr->buffer);
GistPageGetOpaque(ptr->page)->rightlink = InvalidBlockNumber;
}
/* restore splitted non-root page */
if (blkno != GIST_ROOT_BLKNO) {
PageRestoreTempPage( dist->page, BufferGetPage( dist->buffer ) );
dist->page = BufferGetPage( dist->buffer );
} }
if (!gv->index->rd_istemp) if (!gv->index->rd_istemp)
...@@ -232,10 +231,8 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion) ...@@ -232,10 +231,8 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion)
else else
{ {
for (ptr = dist; ptr; ptr = ptr->next) for (ptr = dist; ptr; ptr = ptr->next)
{
PageSetLSN(BufferGetPage(ptr->buffer), XLogRecPtrForTemp); PageSetLSN(BufferGetPage(ptr->buffer), XLogRecPtrForTemp);
} }
}
for (ptr = dist; ptr; ptr = ptr->next) for (ptr = dist; ptr; ptr = ptr->next)
{ {
...@@ -251,66 +248,172 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion) ...@@ -251,66 +248,172 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion)
ItemPointerSet(&key, blkno, TUPLE_IS_VALID); ItemPointerSet(&key, blkno, TUPLE_IS_VALID);
oldCtx = MemoryContextSwitchTo(gv->opCtx);
gistnewroot(gv->index, buffer, res.itup, res.ituplen, &key); gistnewroot(gv->index, buffer, res.itup, res.ituplen, &key);
MemoryContextSwitchTo(oldCtx);
} }
needwrite = false; END_CRIT_SECTION();
MemoryContextReset(gv->opCtx); MemoryContextReset(gv->opCtx);
needunion = false; /* gistSplit already forms unions */ return res;
}
static ArrayTuple
gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion)
{
ArrayTuple res = {NULL, 0, false};
Buffer buffer;
Page page, tempPage = NULL;
OffsetNumber i,
maxoff;
ItemId iid;
int lenaddon = 4,
curlenaddon = 0,
nOffToDelete = 0,
nBlkToDelete = 0;
IndexTuple idxtuple,
*addon = NULL;
bool needwrite = false;
OffsetNumber offToDelete[MaxOffsetNumber];
BlockNumber blkToDelete[MaxOffsetNumber];
ItemPointerData *completed = NULL;
int ncompleted = 0,
lencompleted = 16;
vacuum_delay_point();
buffer = ReadBuffer(gv->index, blkno);
LockBuffer(buffer, GIST_EXCLUSIVE);
gistcheckpage(gv->index, buffer);
page = (Page) BufferGetPage(buffer);
maxoff = PageGetMaxOffsetNumber(page);
if (GistPageIsLeaf(page))
{
if (GistTuplesDeleted(page))
needunion = needwrite = true;
} }
else else
{ {
/* enough free space */ completed = (ItemPointerData *) palloc(sizeof(ItemPointerData) * lencompleted);
gistfillbuffer(gv->index, page, addon, curlenaddon, InvalidOffsetNumber); addon = (IndexTuple *) palloc(sizeof(IndexTuple) * lenaddon);
}
} /* get copy of page to work */
} tempPage = GistPageGetCopyPage(page);
if (needunion) for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{ {
/* forms union for page or check empty */ ArrayTuple chldtuple;
if (PageIsEmpty(page)) bool needchildunion;
iid = PageGetItemId(tempPage, i);
idxtuple = (IndexTuple) PageGetItem(tempPage, iid);
needchildunion = (GistTupleIsInvalid(idxtuple)) ? true : false;
if (needchildunion)
elog(DEBUG2, "gistVacuumUpdate: need union for block %u",
ItemPointerGetBlockNumber(&(idxtuple->t_tid)));
chldtuple = gistVacuumUpdate(gv, ItemPointerGetBlockNumber(&(idxtuple->t_tid)),
needchildunion);
if (chldtuple.ituplen || chldtuple.emptypage)
{ {
if (blkno == GIST_ROOT_BLKNO) /* update tuple or/and inserts new */
if ( chldtuple.emptypage )
blkToDelete[nBlkToDelete++] = ItemPointerGetBlockNumber(&(idxtuple->t_tid));
offToDelete[nOffToDelete++] = i;
PageIndexTupleDelete(tempPage, i);
i--;
maxoff--;
needwrite = needunion = true;
if (chldtuple.ituplen)
{ {
needwrite = true;
GistPageSetLeaf(page); Assert( chldtuple.emptypage == false );
while (curlenaddon + chldtuple.ituplen >= lenaddon)
{
lenaddon *= 2;
addon = (IndexTuple *) repalloc(addon, sizeof(IndexTuple) * lenaddon);
} }
else
memcpy(addon + curlenaddon, chldtuple.itup, chldtuple.ituplen * sizeof(IndexTuple));
curlenaddon += chldtuple.ituplen;
if (chldtuple.ituplen > 1)
{ {
needwrite = true; /*
res.emptypage = true; * child was split, so we need mark completion
GistPageSetDeleted(page); * insert(split)
gv->result->std.pages_deleted++; */
int j;
while (ncompleted + chldtuple.ituplen > 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);
ncompleted++;
} }
else }
pfree(chldtuple.itup);
}
}
}
Assert( maxoff == PageGetMaxOffsetNumber(tempPage) );
if (curlenaddon)
{ {
IndexTuple *vec, /* insert updated tuples */
tmp; if (gistnospace(tempPage, addon, curlenaddon, InvalidOffsetNumber)) {
int veclen = 0; /* there is no space on page to insert tuples */
MemoryContext oldCtx = MemoryContextSwitchTo(gv->opCtx); res = vacuumSplitPage(gv, tempPage, buffer, addon, curlenaddon);
tempPage=NULL; /* vacuumSplitPage() free tempPage */
needwrite = needunion = false; /* gistSplit already forms unions and writes pages */
} else
/* enough free space */
gistfillbuffer(gv->index, tempPage, addon, curlenaddon, InvalidOffsetNumber);
}
}
vec = gistextractbuffer(buffer, &veclen); /*
tmp = gistunion(gv->index, vec, veclen, &(gv->giststate)); * If page is empty, we should remove pointer to it before
MemoryContextSwitchTo(oldCtx); * deleting page (except root)
*/
res.itup = (IndexTuple *) palloc(sizeof(IndexTuple)); if ( blkno != GIST_ROOT_BLKNO && ( PageIsEmpty(page) || (tempPage && PageIsEmpty(tempPage)) ) ) {
res.ituplen = 1; /*
res.itup[0] = (IndexTuple) palloc(IndexTupleSize(tmp)); * New version of page is empty, so leave it unchanged,
memcpy(res.itup[0], tmp, IndexTupleSize(tmp)); * upper call will mark our page as deleted.
* In case of page split we never will be here...
*
* If page was empty it can't become non-empty during processing
*/
res.emptypage = true;
UnlockReleaseBuffer(buffer);
} else {
/* write page and remove its childs if it need */
ItemPointerSetBlockNumber(&(res.itup[0]->t_tid), blkno); START_CRIT_SECTION();
GistTupleSetValid(res.itup[0]);
MemoryContextReset(gv->opCtx); if ( tempPage && needwrite ) {
PageRestoreTempPage(tempPage, page);
tempPage = NULL;
} }
/* Empty index */
if (PageIsEmpty(page) && blkno == GIST_ROOT_BLKNO )
{
needwrite = true;
GistPageSetLeaf(page);
} }
if (needwrite) if (needwrite)
{ {
MarkBufferDirty(buffer); MarkBufferDirty(buffer);
...@@ -323,9 +426,9 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion) ...@@ -323,9 +426,9 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion)
char *xlinfo; char *xlinfo;
rdata = formUpdateRdata(gv->index->rd_node, buffer, rdata = formUpdateRdata(gv->index->rd_node, buffer,
todelete, ntodelete, res.emptypage, offToDelete, nOffToDelete,
addon, curlenaddon, NULL); addon, curlenaddon, NULL);
xlinfo = rdata->data; xlinfo = rdata->next->data;
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE, rdata); recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE, rdata);
PageSetLSN(page, recptr); PageSetLSN(page, recptr);
...@@ -340,10 +443,23 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion) ...@@ -340,10 +443,23 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion)
END_CRIT_SECTION(); END_CRIT_SECTION();
if ( needunion && !PageIsEmpty(page) )
{
res.itup = (IndexTuple *) palloc(sizeof(IndexTuple));
res.ituplen = 1;
res.itup[0] = PageMakeUnionKey(gv, buffer);
}
UnlockReleaseBuffer(buffer); UnlockReleaseBuffer(buffer);
/* delete empty children, now we havn't any links to pointed subtrees */
for(i=0;i<nBlkToDelete;i++)
gistDeleteSubtree(gv, blkToDelete[i]);
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]);
...@@ -351,6 +467,9 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion) ...@@ -351,6 +467,9 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion)
pfree(addon); pfree(addon);
if (completed) if (completed)
pfree(completed); pfree(completed);
if (tempPage)
pfree(tempPage);
return res; return res;
} }
...@@ -627,10 +746,10 @@ gistbulkdelete(PG_FUNCTION_ARGS) ...@@ -627,10 +746,10 @@ gistbulkdelete(PG_FUNCTION_ARGS)
gistxlogPageUpdate *xlinfo; gistxlogPageUpdate *xlinfo;
rdata = formUpdateRdata(rel->rd_node, buffer, rdata = formUpdateRdata(rel->rd_node, buffer,
todelete, ntodelete, false, todelete, ntodelete,
NULL, 0, NULL, 0,
NULL); NULL);
xlinfo = (gistxlogPageUpdate *) rdata->data; xlinfo = (gistxlogPageUpdate *) rdata->next->data;
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE, rdata); recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE, rdata);
PageSetLSN(page, recptr); PageSetLSN(page, recptr);
......
...@@ -8,7 +8,7 @@ ...@@ -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/gistxlog.c,v 1.16 2006/05/10 09:19:54 teodor Exp $ * $PostgreSQL: pgsql/src/backend/access/gist/gistxlog.c,v 1.17 2006/05/17 16:34:59 teodor Exp $
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -209,18 +209,6 @@ gistRedoPageUpdateRecord(XLogRecPtr lsn, XLogRecord *record, bool isnewroot) ...@@ -209,18 +209,6 @@ gistRedoPageUpdateRecord(XLogRecPtr lsn, XLogRecord *record, bool isnewroot)
return; return;
} }
if (xlrec.data->isemptypage)
{
while (!PageIsEmpty(page))
PageIndexTupleDelete(page, FirstOffsetNumber);
if (xlrec.data->blkno == GIST_ROOT_BLKNO)
GistPageSetLeaf(page);
else
GistPageSetDeleted(page);
}
else
{
if (isnewroot) if (isnewroot)
GISTInitBuffer(buffer, 0); GISTInitBuffer(buffer, 0);
else if (xlrec.data->ntodelete) else if (xlrec.data->ntodelete)
...@@ -243,7 +231,11 @@ gistRedoPageUpdateRecord(XLogRecPtr lsn, XLogRecord *record, bool isnewroot) ...@@ -243,7 +231,11 @@ gistRedoPageUpdateRecord(XLogRecPtr lsn, XLogRecord *record, bool isnewroot)
*/ */
if (GistPageIsLeaf(page) && xlrec.len == 0 && xlrec.data->ntodelete == 0) if (GistPageIsLeaf(page) && xlrec.len == 0 && xlrec.data->ntodelete == 0)
GistClearTuplesDeleted(page); GistClearTuplesDeleted(page);
}
if ( !GistPageIsLeaf(page) && PageGetMaxOffsetNumber(page) == InvalidOffsetNumber && xldata->blkno == GIST_ROOT_BLKNO )
/* all links on non-leaf root page was deleted by vacuum full,
so root page becomes a leaf */
GistPageSetLeaf(page);
GistPageGetOpaque(page)->rightlink = InvalidBlockNumber; GistPageGetOpaque(page)->rightlink = InvalidBlockNumber;
PageSetLSN(page, lsn); PageSetLSN(page, lsn);
...@@ -252,6 +244,29 @@ gistRedoPageUpdateRecord(XLogRecPtr lsn, XLogRecord *record, bool isnewroot) ...@@ -252,6 +244,29 @@ gistRedoPageUpdateRecord(XLogRecPtr lsn, XLogRecord *record, bool isnewroot)
UnlockReleaseBuffer(buffer); UnlockReleaseBuffer(buffer);
} }
static void
gistRedoPageDeleteRecord(XLogRecPtr lsn, XLogRecord *record)
{
gistxlogPageDelete *xldata = (gistxlogPageDelete *) XLogRecGetData(record);
Relation reln;
Buffer buffer;
Page page;
reln = XLogOpenRelation(xldata->node);
buffer = XLogReadBuffer(reln, xldata->blkno, false);
if (!BufferIsValid(buffer))
return;
GISTInitBuffer( buffer, 0 );
page = (Page) BufferGetPage(buffer);
GistPageSetDeleted(page);
PageSetLSN(page, lsn);
PageSetTLI(page, ThisTimeLineID);
MarkBufferDirty(buffer);
UnlockReleaseBuffer(buffer);
}
static void static void
decodePageSplitRecord(PageSplitRecord *decoded, XLogRecord *record) decodePageSplitRecord(PageSplitRecord *decoded, XLogRecord *record)
{ {
...@@ -382,6 +397,9 @@ gist_redo(XLogRecPtr lsn, XLogRecord *record) ...@@ -382,6 +397,9 @@ gist_redo(XLogRecPtr lsn, XLogRecord *record)
case XLOG_GIST_PAGE_UPDATE: case XLOG_GIST_PAGE_UPDATE:
gistRedoPageUpdateRecord(lsn, record, false); gistRedoPageUpdateRecord(lsn, record, false);
break; break;
case XLOG_GIST_PAGE_DELETE:
gistRedoPageDeleteRecord(lsn, record);
break;
case XLOG_GIST_NEW_ROOT: case XLOG_GIST_NEW_ROOT:
gistRedoPageUpdateRecord(lsn, record, true); gistRedoPageUpdateRecord(lsn, record, true);
break; break;
...@@ -405,8 +423,10 @@ gist_redo(XLogRecPtr lsn, XLogRecord *record) ...@@ -405,8 +423,10 @@ gist_redo(XLogRecPtr lsn, XLogRecord *record)
static void static void
out_target(StringInfo buf, RelFileNode node, ItemPointerData key) out_target(StringInfo buf, RelFileNode node, ItemPointerData key)
{ {
appendStringInfo(buf, "rel %u/%u/%u; tid %u/%u", appendStringInfo(buf, "rel %u/%u/%u",
node.spcNode, node.dbNode, node.relNode, node.spcNode, node.dbNode, node.relNode);
if ( ItemPointerIsValid( &key ) )
appendStringInfo(buf, "; tid %u/%u",
ItemPointerGetBlockNumber(&key), ItemPointerGetBlockNumber(&key),
ItemPointerGetOffsetNumber(&key)); ItemPointerGetOffsetNumber(&key));
} }
...@@ -418,6 +438,14 @@ out_gistxlogPageUpdate(StringInfo buf, gistxlogPageUpdate *xlrec) ...@@ -418,6 +438,14 @@ out_gistxlogPageUpdate(StringInfo buf, gistxlogPageUpdate *xlrec)
appendStringInfo(buf, "; block number %u", xlrec->blkno); appendStringInfo(buf, "; block number %u", xlrec->blkno);
} }
static void
out_gistxlogPageDelete(StringInfo buf, gistxlogPageDelete *xlrec)
{
appendStringInfo(buf, "page_delete: rel %u/%u/%u; blkno %u",
xlrec->node.spcNode, xlrec->node.dbNode, xlrec->node.relNode,
xlrec->blkno);
}
static void static void
out_gistxlogPageSplit(StringInfo buf, gistxlogPageSplit *xlrec) out_gistxlogPageSplit(StringInfo buf, gistxlogPageSplit *xlrec)
{ {
...@@ -438,6 +466,9 @@ gist_desc(StringInfo buf, uint8 xl_info, char *rec) ...@@ -438,6 +466,9 @@ gist_desc(StringInfo buf, uint8 xl_info, char *rec)
appendStringInfo(buf, "page_update: "); appendStringInfo(buf, "page_update: ");
out_gistxlogPageUpdate(buf, (gistxlogPageUpdate *) rec); out_gistxlogPageUpdate(buf, (gistxlogPageUpdate *) rec);
break; break;
case XLOG_GIST_PAGE_DELETE:
out_gistxlogPageDelete(buf, (gistxlogPageDelete *) rec);
break;
case XLOG_GIST_NEW_ROOT: case XLOG_GIST_NEW_ROOT:
appendStringInfo(buf, "new_root: "); appendStringInfo(buf, "new_root: ");
out_target(buf, ((gistxlogPageUpdate *) rec)->node, ((gistxlogPageUpdate *) rec)->key); out_target(buf, ((gistxlogPageUpdate *) rec)->node, ((gistxlogPageUpdate *) rec)->key);
...@@ -643,7 +674,7 @@ gistContinueInsert(gistIncompleteInsert *insert) ...@@ -643,7 +674,7 @@ gistContinueInsert(gistIncompleteInsert *insert)
* we split root, just copy tuples from old root to new * we split root, just copy tuples from old root to new
* page * page
*/ */
parentitup = gistextractbuffer(buffers[numbuffer - 1], parentitup = gistextractpage(pages[numbuffer - 1],
&pituplen); &pituplen);
/* sanity check */ /* sanity check */
...@@ -796,7 +827,7 @@ formSplitRdata(RelFileNode node, BlockNumber blkno, bool page_is_leaf, ...@@ -796,7 +827,7 @@ formSplitRdata(RelFileNode node, BlockNumber blkno, bool page_is_leaf,
*/ */
XLogRecData * XLogRecData *
formUpdateRdata(RelFileNode node, Buffer buffer, formUpdateRdata(RelFileNode node, Buffer buffer,
OffsetNumber *todelete, int ntodelete, bool emptypage, OffsetNumber *todelete, int ntodelete,
IndexTuple *itup, int ituplen, ItemPointer key) IndexTuple *itup, int ituplen, ItemPointer key)
{ {
XLogRecData *rdata; XLogRecData *rdata;
...@@ -804,35 +835,37 @@ formUpdateRdata(RelFileNode node, Buffer buffer, ...@@ -804,35 +835,37 @@ formUpdateRdata(RelFileNode node, Buffer buffer,
int cur, int cur,
i; i;
/* ugly wart in API: emptypage causes us to ignore other inputs */ rdata = (XLogRecData *) palloc(sizeof(XLogRecData) * (3 + ituplen));
if (emptypage)
ntodelete = ituplen = 0;
rdata = (XLogRecData *) palloc(sizeof(XLogRecData) * (2 + ituplen));
xlrec = (gistxlogPageUpdate *) palloc(sizeof(gistxlogPageUpdate)); xlrec = (gistxlogPageUpdate *) palloc(sizeof(gistxlogPageUpdate));
xlrec->node = node; xlrec->node = node;
xlrec->blkno = BufferGetBlockNumber(buffer); xlrec->blkno = BufferGetBlockNumber(buffer);
xlrec->ntodelete = ntodelete; xlrec->ntodelete = ntodelete;
xlrec->isemptypage = emptypage;
if (key) if (key)
xlrec->key = *key; xlrec->key = *key;
else else
ItemPointerSetInvalid(&(xlrec->key)); ItemPointerSetInvalid(&(xlrec->key));
rdata[0].data = (char *) xlrec; rdata[0].buffer = buffer;
rdata[0].len = sizeof(gistxlogPageUpdate); rdata[0].buffer_std = true;
rdata[0].buffer = InvalidBuffer; rdata[0].data = NULL;
rdata[0].len = 0;
rdata[0].next = &(rdata[1]); rdata[0].next = &(rdata[1]);
rdata[1].data = (char *) todelete; rdata[1].data = (char *) xlrec;
rdata[1].len = MAXALIGN(sizeof(OffsetNumber) * ntodelete); rdata[1].len = sizeof(gistxlogPageUpdate);
rdata[1].buffer = buffer; rdata[1].buffer = InvalidBuffer;
rdata[1].buffer_std = true; rdata[1].next = &(rdata[2]);
rdata[1].next = NULL;
rdata[2].data = (char *) todelete;
rdata[2].len = MAXALIGN(sizeof(OffsetNumber) * ntodelete);
rdata[2].buffer = buffer;
rdata[2].buffer_std = true;
rdata[2].next = NULL;
/* new tuples */ /* new tuples */
cur = 2; cur = 3;
for (i = 0; i < ituplen; i++) for (i = 0; i < ituplen; i++)
{ {
rdata[cur - 1].next = &(rdata[cur]); rdata[cur - 1].next = &(rdata[cur]);
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/access/gist_private.h,v 1.13 2006/05/10 09:19:54 teodor Exp $ * $PostgreSQL: pgsql/src/include/access/gist_private.h,v 1.14 2006/05/17 16:34:59 teodor Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -85,20 +85,21 @@ extern const XLogRecPtr XLogRecPtrForTemp; ...@@ -85,20 +85,21 @@ extern const XLogRecPtr XLogRecPtrForTemp;
#define XLOG_GIST_PAGE_SPLIT 0x30 #define XLOG_GIST_PAGE_SPLIT 0x30
#define XLOG_GIST_INSERT_COMPLETE 0x40 #define XLOG_GIST_INSERT_COMPLETE 0x40
#define XLOG_GIST_CREATE_INDEX 0x50 #define XLOG_GIST_CREATE_INDEX 0x50
#define XLOG_GIST_PAGE_DELETE 0x60
typedef struct gistxlogPageUpdate typedef struct gistxlogPageUpdate
{ {
RelFileNode node; RelFileNode node;
BlockNumber blkno; BlockNumber blkno;
uint16 ntodelete;
bool isemptypage;
/* /*
* It used to identify completeness of insert. Sets to leaf itup * It used to identify completeness of insert. Sets to leaf itup
*/ */
ItemPointerData key; ItemPointerData key;
/* number of deleted offsets */
uint16 ntodelete;
/* /*
* follow: 1. todelete OffsetNumbers 2. tuples to insert * follow: 1. todelete OffsetNumbers 2. tuples to insert
*/ */
...@@ -131,6 +132,11 @@ typedef struct gistxlogInsertComplete ...@@ -131,6 +132,11 @@ typedef struct gistxlogInsertComplete
/* follows ItemPointerData key to clean */ /* follows ItemPointerData key to clean */
} gistxlogInsertComplete; } gistxlogInsertComplete;
typedef struct gistxlogPageDelete
{
RelFileNode node;
BlockNumber blkno;
} gistxlogPageDelete;
/* SplitedPageLayout - gistSplit function result */ /* SplitedPageLayout - gistSplit function result */
typedef struct SplitedPageLayout typedef struct SplitedPageLayout
...@@ -249,7 +255,7 @@ extern void gist_xlog_cleanup(void); ...@@ -249,7 +255,7 @@ extern void gist_xlog_cleanup(void);
extern IndexTuple gist_form_invalid_tuple(BlockNumber blkno); extern IndexTuple gist_form_invalid_tuple(BlockNumber blkno);
extern XLogRecData *formUpdateRdata(RelFileNode node, Buffer buffer, extern XLogRecData *formUpdateRdata(RelFileNode node, Buffer buffer,
OffsetNumber *todelete, int ntodelete, bool emptypage, OffsetNumber *todelete, int ntodelete,
IndexTuple *itup, int ituplen, ItemPointer key); IndexTuple *itup, int ituplen, ItemPointer key);
extern XLogRecData *formSplitRdata(RelFileNode node, extern XLogRecData *formSplitRdata(RelFileNode node,
...@@ -273,7 +279,7 @@ extern void gistcheckpage(Relation rel, Buffer buf); ...@@ -273,7 +279,7 @@ extern void gistcheckpage(Relation rel, Buffer buf);
extern Buffer gistNewBuffer(Relation r); extern Buffer gistNewBuffer(Relation r);
extern OffsetNumber gistfillbuffer(Relation r, Page page, IndexTuple *itup, extern OffsetNumber gistfillbuffer(Relation r, Page page, IndexTuple *itup,
int len, OffsetNumber off); int len, OffsetNumber off);
extern IndexTuple *gistextractbuffer(Buffer buffer, int *len /* out */ ); extern IndexTuple *gistextractpage(Page page, int *len /* out */ );
extern IndexTuple *gistjoinvector( extern IndexTuple *gistjoinvector(
IndexTuple *itvec, int *len, IndexTuple *itvec, int *len,
IndexTuple *additvec, int addlen); IndexTuple *additvec, int addlen);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment