From: Tom Lane Date: Mon, 3 Nov 2008 20:47:49 +0000 (+0000) Subject: Clean up the messy semantics (not to mention inefficiency) of PageGetTempPage X-Git-Tag: REL8_4_BETA1~770 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b4eae023bb2b5e9bce96cd367d70c180eefe6bf9;p=postgresql Clean up the messy semantics (not to mention inefficiency) of PageGetTempPage by splitting it into three functions with better-defined behaviors. Zdenek Kotala --- diff --git a/src/backend/access/gin/gindatapage.c b/src/backend/access/gin/gindatapage.c index 33ed8928ff..7afe36eff5 100644 --- a/src/backend/access/gin/gindatapage.c +++ b/src/backend/access/gin/gindatapage.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/gin/gindatapage.c,v 1.11 2008/06/19 00:46:03 alvherre Exp $ + * $PostgreSQL: pgsql/src/backend/access/gin/gindatapage.c,v 1.12 2008/11/03 20:47:48 tgl Exp $ *------------------------------------------------------------------------- */ @@ -445,7 +445,7 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe char *ptr; OffsetNumber separator; ItemPointer bound; - Page lpage = GinPageGetCopyPage(BufferGetPage(lbuf)); + Page lpage = PageGetTempPageCopy(BufferGetPage(lbuf)); ItemPointerData oldbound = *GinDataPageGetRightBound(lpage); int sizeofitem = GinSizeOfItem(lpage); OffsetNumber maxoff = GinPageGetOpaque(lpage)->maxoff; diff --git a/src/backend/access/gin/ginentrypage.c b/src/backend/access/gin/ginentrypage.c index ef9c9ba2a4..ec899885ab 100644 --- a/src/backend/access/gin/ginentrypage.c +++ b/src/backend/access/gin/ginentrypage.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/gin/ginentrypage.c,v 1.17 2008/07/11 21:06:29 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/access/gin/ginentrypage.c,v 1.18 2008/11/03 20:47:48 tgl Exp $ *------------------------------------------------------------------------- */ @@ -458,7 +458,7 @@ entrySplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogR leftrightmost = NULL; static ginxlogSplit data; Page page; - Page lpage = GinPageGetCopyPage(BufferGetPage(lbuf)); + Page lpage = PageGetTempPageCopy(BufferGetPage(lbuf)); Page rpage = BufferGetPage(rbuf); Size pageSize = PageGetPageSize(lpage); diff --git a/src/backend/access/gin/ginutil.c b/src/backend/access/gin/ginutil.c index 587add92e9..c111c06039 100644 --- a/src/backend/access/gin/ginutil.c +++ b/src/backend/access/gin/ginutil.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/gin/ginutil.c,v 1.17 2008/09/30 10:52:10 heikki Exp $ + * $PostgreSQL: pgsql/src/backend/access/gin/ginutil.c,v 1.18 2008/11/03 20:47:48 tgl Exp $ *------------------------------------------------------------------------- */ @@ -310,21 +310,6 @@ extractEntriesSU(GinState *ginstate, OffsetNumber attnum, Datum value, int32 *ne return entries; } -/* - * It's analog of PageGetTempPage(), but copies whole page - */ -Page -GinPageGetCopyPage(Page page) -{ - Size pageSize = PageGetPageSize(page); - Page tmppage; - - tmppage = (Page) palloc(pageSize); - memcpy(tmppage, page, pageSize); - - return tmppage; -} - Datum ginoptions(PG_FUNCTION_ARGS) { diff --git a/src/backend/access/gin/ginvacuum.c b/src/backend/access/gin/ginvacuum.c index ae6459c9fa..c49926c3fd 100644 --- a/src/backend/access/gin/ginvacuum.c +++ b/src/backend/access/gin/ginvacuum.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/gin/ginvacuum.c,v 1.24 2008/10/31 15:04:59 heikki Exp $ + * $PostgreSQL: pgsql/src/backend/access/gin/ginvacuum.c,v 1.25 2008/11/03 20:47:48 tgl Exp $ *------------------------------------------------------------------------- */ @@ -546,7 +546,7 @@ ginVacuumEntryPage(GinVacuumState *gvs, Buffer buffer, BlockNumber *roots, uint3 * On first difference we create temporary page in memory * and copies content in to it. */ - tmppage = GinPageGetCopyPage(origpage); + tmppage = PageGetTempPageCopy(origpage); if (newN > 0) { diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c index 2f75c3fa2a..e1f068ee7b 100644 --- a/src/backend/access/gist/gist.c +++ b/src/backend/access/gist/gist.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/gist/gist.c,v 1.152 2008/09/30 10:52:10 heikki Exp $ + * $PostgreSQL: pgsql/src/backend/access/gist/gist.c,v 1.153 2008/11/03 20:47:48 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -343,7 +343,7 @@ gistplacetopage(GISTInsertState *state, GISTSTATE *giststate) * we must create temporary page to operate */ dist->buffer = state->stack->buffer; - dist->page = PageGetTempPage(BufferGetPage(dist->buffer), sizeof(GISTPageOpaqueData)); + dist->page = PageGetTempPageCopySpecial(BufferGetPage(dist->buffer)); /* clean all flags except F_LEAF */ GistPageGetOpaque(dist->page)->flags = (is_leaf) ? F_LEAF : 0; diff --git a/src/backend/access/gist/gistvacuum.c b/src/backend/access/gist/gistvacuum.c index 80710ae736..d933fea25b 100644 --- a/src/backend/access/gist/gistvacuum.c +++ b/src/backend/access/gist/gistvacuum.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/gist/gistvacuum.c,v 1.39 2008/10/31 15:04:59 heikki Exp $ + * $PostgreSQL: pgsql/src/backend/access/gist/gistvacuum.c,v 1.40 2008/11/03 20:47:48 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -144,18 +144,6 @@ gistDeleteSubtree(GistVacuum *gv, BlockNumber blkno) UnlockReleaseBuffer(buffer); } -static Page -GistPageGetCopyPage(Page page) -{ - Size pageSize = PageGetPageSize(page); - Page tmppage; - - tmppage = (Page) palloc(pageSize); - memcpy(tmppage, page, pageSize); - - return tmppage; -} - static ArrayTuple vacuumSplitPage(GistVacuum *gv, Page tempPage, Buffer buffer, IndexTuple *addon, int curlenaddon) { @@ -325,7 +313,7 @@ gistVacuumUpdate(GistVacuum *gv, BlockNumber blkno, bool needunion) addon = (IndexTuple *) palloc(sizeof(IndexTuple) * lenaddon); /* get copy of page to work */ - tempPage = GistPageGetCopyPage(page); + tempPage = PageGetTempPageCopy(page); for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) { diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c index 01e1ea4944..6842d5e8c9 100644 --- a/src/backend/access/nbtree/nbtinsert.c +++ b/src/backend/access/nbtree/nbtinsert.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.167 2008/06/11 08:38:56 heikki Exp $ + * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.168 2008/11/03 20:47:48 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -793,7 +793,7 @@ _bt_split(Relation rel, Buffer buf, OffsetNumber firstright, rbuf = _bt_getbuf(rel, P_NEW, BT_WRITE); origpage = BufferGetPage(buf); - leftpage = PageGetTempPage(origpage, sizeof(BTPageOpaqueData)); + leftpage = PageGetTempPage(origpage); rightpage = BufferGetPage(rbuf); _bt_pageinit(leftpage, BufferGetPageSize(buf)); diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c index abfc569399..3faae011b3 100644 --- a/src/backend/storage/page/bufpage.c +++ b/src/backend/storage/page/bufpage.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/storage/page/bufpage.c,v 1.80 2008/07/13 21:50:04 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/storage/page/bufpage.c,v 1.81 2008/11/03 20:47:48 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -254,28 +254,59 @@ PageAddItem(Page page, /* * PageGetTempPage - * Get a temporary page in local memory for special processing + * Get a temporary page in local memory for special processing. + * The returned page is not initialized at all; caller must do that. */ Page -PageGetTempPage(Page page, Size specialSize) +PageGetTempPage(Page page) +{ + Size pageSize; + Page temp; + + pageSize = PageGetPageSize(page); + temp = (Page) palloc(pageSize); + + return temp; +} + +/* + * PageGetTempPageCopy + * Get a temporary page in local memory for special processing. + * The page is initialized by copying the contents of the given page. + */ +Page +PageGetTempPageCopy(Page page) { Size pageSize; Page temp; - PageHeader thdr; pageSize = PageGetPageSize(page); temp = (Page) palloc(pageSize); - thdr = (PageHeader) temp; - /* copy old page in */ memcpy(temp, page, pageSize); - /* set high, low water marks */ - thdr->pd_lower = SizeOfPageHeaderData; - thdr->pd_upper = pageSize - MAXALIGN(specialSize); + return temp; +} + +/* + * PageGetTempPageCopySpecial + * Get a temporary page in local memory for special processing. + * The page is PageInit'd with the same special-space size as the + * given page, and the special space is copied from the given page. + */ +Page +PageGetTempPageCopySpecial(Page page) +{ + Size pageSize; + Page temp; + + pageSize = PageGetPageSize(page); + temp = (Page) palloc(pageSize); - /* clear out the middle */ - MemSet((char *) temp + thdr->pd_lower, 0, thdr->pd_upper - thdr->pd_lower); + PageInit(temp, pageSize, PageGetSpecialSize(page)); + memcpy(PageGetSpecialPointer(temp), + PageGetSpecialPointer(page), + PageGetSpecialSize(page)); return temp; } diff --git a/src/include/access/gin.h b/src/include/access/gin.h index 57cb8a345a..5151058514 100644 --- a/src/include/access/gin.h +++ b/src/include/access/gin.h @@ -4,7 +4,7 @@ * * Copyright (c) 2006-2008, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/include/access/gin.h,v 1.25 2008/10/20 13:39:44 teodor Exp $ + * $PostgreSQL: pgsql/src/include/access/gin.h,v 1.26 2008/11/03 20:47:49 tgl Exp $ *-------------------------------------------------------------------------- */ @@ -246,7 +246,6 @@ extern int compareAttEntries(GinState *ginstate, OffsetNumber attnum_a, Datum a, extern Datum *extractEntriesS(GinState *ginstate, OffsetNumber attnum, Datum value, int32 *nentries, bool *needUnique); extern Datum *extractEntriesSU(GinState *ginstate, OffsetNumber attnum, Datum value, int32 *nentries); -extern Page GinPageGetCopyPage(Page page); extern Datum gin_index_getattr(GinState *ginstate, IndexTuple tuple); extern OffsetNumber gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple); diff --git a/src/include/storage/bufpage.h b/src/include/storage/bufpage.h index 5a8c2b0d30..a5b88e78df 100644 --- a/src/include/storage/bufpage.h +++ b/src/include/storage/bufpage.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/storage/bufpage.h,v 1.83 2008/07/14 03:22:32 tgl Exp $ + * $PostgreSQL: pgsql/src/include/storage/bufpage.h,v 1.84 2008/11/03 20:47:49 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -362,7 +362,9 @@ extern void PageInit(Page page, Size pageSize, Size specialSize); extern bool PageHeaderIsValid(PageHeader page); extern OffsetNumber PageAddItem(Page page, Item item, Size size, OffsetNumber offsetNumber, bool overwrite, bool is_heap); -extern Page PageGetTempPage(Page page, Size specialSize); +extern Page PageGetTempPage(Page page); +extern Page PageGetTempPageCopy(Page page); +extern Page PageGetTempPageCopySpecial(Page page); extern void PageRestoreTempPage(Page tempPage, Page oldPage); extern void PageRepairFragmentation(Page page); extern Size PageGetFreeSpace(Page page);