On 2017/09/13 13:05, Tom Lane wrote:
> Amit Langote <langote_amit...@lab.ntt.co.jp> writes:
>> On 2017/09/12 23:27, Amit Kapila wrote:
>>> I think one point which might be missed is that the patch needs to
>>> modify pd_lower for all usages of metapage, not only when it is first
>>> time initialized.
> 
>> Maybe I'm missing something, but isn't the metadata size fixed and hence
>> pd_lower won't change once it's initialized?  Maybe, it's not true for all
>> index types?
> 
> No, the point is that you might be dealing with an index recently
> pg_upgraded from v10 or before, which does not have the correct
> value for pd_lower on that page.  This has to be coped with.

Ah, got it.  Thanks for the explanation.

I updated the patches so that the metapage's pd_lower is set to the
correct value just before *every* point where we are about to insert a
full page image of the metapage into WAL.  That's in addition to doing the
same in various metapage init routines, which the original patch did
already anyway.  I guess this now ensures that wal_consistency_checking
masking of these metapages as standard layout pages always works, even for
pre-v11 indexes that were upgraded.

Also, we now pass the metapage buffer as containing a page of standard
layout to XLogRegisterBuffer(), so that any hole in it is compressed when
actually writing to WAL.

Thanks,
Amit
From 607b4ab062652e7ffc0f95338c9265b09be18b56 Mon Sep 17 00:00:00 2001
From: amit <amitlangot...@gmail.com>
Date: Fri, 23 Jun 2017 11:20:41 +0900
Subject: [PATCH 1/3] Set pd_lower correctly in the GIN metapage.

Also tell xlog.c to treat the metapage like a standard page, so any
hole in it is compressed.
---
 src/backend/access/gin/ginfast.c   | 22 ++++++++++++++++++++--
 src/backend/access/gin/gininsert.c |  4 ++--
 src/backend/access/gin/ginutil.c   | 19 ++++++++++++++++++-
 src/backend/access/gin/ginxlog.c   | 24 +++++++++---------------
 4 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/src/backend/access/gin/ginfast.c b/src/backend/access/gin/ginfast.c
index 59e435465a..d96529cf72 100644
--- a/src/backend/access/gin/ginfast.c
+++ b/src/backend/access/gin/ginfast.c
@@ -399,6 +399,15 @@ ginHeapTupleFastInsert(GinState *ginstate, 
GinTupleCollector *collector)
        /*
         * Write metabuffer, make xlog entry
         */
+
+       /*
+        * Set pd_lower just past the end of the metadata.  This is not 
essential
+        * but it makes the page look compressible to xlog.c, because we pass 
the
+        * buffer containing this page to XLogRegisterBuffer() as a page with
+        * standard layout.
+        */
+       ((PageHeader) metapage)->pd_lower =
+                       ((char *) metadata + sizeof(GinMetaPageData)) - (char 
*) metapage;
        MarkBufferDirty(metabuffer);
 
        if (needWal)
@@ -407,7 +416,7 @@ ginHeapTupleFastInsert(GinState *ginstate, 
GinTupleCollector *collector)
 
                memcpy(&data.metadata, metadata, sizeof(GinMetaPageData));
 
-               XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT);
+               XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT | 
REGBUF_STANDARD);
                XLogRegisterData((char *) &data, sizeof(ginxlogUpdateMeta));
 
                recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE);
@@ -572,6 +581,14 @@ shiftList(Relation index, Buffer metabuffer, BlockNumber 
newHead,
                        metadata->nPendingHeapTuples = 0;
                }
 
+               /*
+                * Set pd_lower just past the end of the metadata.  This is not
+                * essential but it makes the page look compressible to xlog.c,
+                * because we pass the buffer containing this page to
+                * XLogRegisterBuffer() as page with standard layout.
+                */
+               ((PageHeader) metapage)->pd_lower =
+                       ((char *) metadata + sizeof(GinMetaPageData)) - (char 
*) metapage;
                MarkBufferDirty(metabuffer);
 
                for (i = 0; i < data.ndeleted; i++)
@@ -586,7 +603,8 @@ shiftList(Relation index, Buffer metabuffer, BlockNumber 
newHead,
                        XLogRecPtr      recptr;
 
                        XLogBeginInsert();
-                       XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT);
+                       XLogRegisterBuffer(0, metabuffer,
+                                                          REGBUF_WILL_INIT | 
REGBUF_STANDARD);
                        for (i = 0; i < data.ndeleted; i++)
                                XLogRegisterBuffer(i + 1, buffers[i], 
REGBUF_WILL_INIT);
 
diff --git a/src/backend/access/gin/gininsert.c 
b/src/backend/access/gin/gininsert.c
index 5378011f50..c9aa4ee147 100644
--- a/src/backend/access/gin/gininsert.c
+++ b/src/backend/access/gin/gininsert.c
@@ -348,7 +348,7 @@ ginbuild(Relation heap, Relation index, IndexInfo 
*indexInfo)
                Page            page;
 
                XLogBeginInsert();
-               XLogRegisterBuffer(0, MetaBuffer, REGBUF_WILL_INIT);
+               XLogRegisterBuffer(0, MetaBuffer, REGBUF_WILL_INIT | 
REGBUF_STANDARD);
                XLogRegisterBuffer(1, RootBuffer, REGBUF_WILL_INIT);
 
                recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_CREATE_INDEX);
@@ -447,7 +447,7 @@ ginbuildempty(Relation index)
        START_CRIT_SECTION();
        GinInitMetabuffer(MetaBuffer);
        MarkBufferDirty(MetaBuffer);
-       log_newpage_buffer(MetaBuffer, false);
+       log_newpage_buffer(MetaBuffer, true);
        GinInitBuffer(RootBuffer, GIN_LEAF);
        MarkBufferDirty(RootBuffer);
        log_newpage_buffer(RootBuffer, false);
diff --git a/src/backend/access/gin/ginutil.c b/src/backend/access/gin/ginutil.c
index 136ea27718..d680849e9d 100644
--- a/src/backend/access/gin/ginutil.c
+++ b/src/backend/access/gin/ginutil.c
@@ -374,6 +374,15 @@ GinInitMetabuffer(Buffer b)
        metadata->nDataPages = 0;
        metadata->nEntries = 0;
        metadata->ginVersion = GIN_CURRENT_VERSION;
+
+       /*
+        * Set pd_lower just past the end of the metadata.  This is not 
essential
+        * but it makes the page look compressible to xlog.c, because we pass 
the
+        * buffer containing this page to XLogRegisterBuffer() as a page with
+        * standard layout.
+        */
+       ((PageHeader) page)->pd_lower =
+                       ((char *) metadata + sizeof(GinMetaPageData)) - (char 
*) page;
 }
 
 /*
@@ -676,6 +685,14 @@ ginUpdateStats(Relation index, const GinStatsData *stats)
        metadata->nDataPages = stats->nDataPages;
        metadata->nEntries = stats->nEntries;
 
+       /*
+        * Set pd_lower just past the end of the metadata.  This is not 
essential
+        * but it makes the page look compressible to xlog.c, because we pass 
the
+        * buffer containing this page to XLogRegisterBuffer() as page with
+        * standard layout.
+        */
+       ((PageHeader) metapage)->pd_lower =
+               ((char *) metadata + sizeof(GinMetaPageData)) - (char *) 
metapage;
        MarkBufferDirty(metabuffer);
 
        if (RelationNeedsWAL(index))
@@ -690,7 +707,7 @@ ginUpdateStats(Relation index, const GinStatsData *stats)
 
                XLogBeginInsert();
                XLogRegisterData((char *) &data, sizeof(ginxlogUpdateMeta));
-               XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT);
+               XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT | 
REGBUF_STANDARD);
 
                recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE);
                PageSetLSN(metapage, recptr);
diff --git a/src/backend/access/gin/ginxlog.c b/src/backend/access/gin/ginxlog.c
index 7ba04e324f..f5c11b2d9a 100644
--- a/src/backend/access/gin/ginxlog.c
+++ b/src/backend/access/gin/ginxlog.c
@@ -514,7 +514,7 @@ ginRedoUpdateMetapage(XLogReaderState *record)
        Assert(BufferGetBlockNumber(metabuffer) == GIN_METAPAGE_BLKNO);
        metapage = BufferGetPage(metabuffer);
 
-       GinInitPage(metapage, GIN_META, BufferGetPageSize(metabuffer));
+       GinInitMetabuffer(metabuffer);
        memcpy(GinPageGetMeta(metapage), &data->metadata, 
sizeof(GinMetaPageData));
        PageSetLSN(metapage, lsn);
        MarkBufferDirty(metabuffer);
@@ -656,7 +656,7 @@ ginRedoDeleteListPages(XLogReaderState *record)
        Assert(BufferGetBlockNumber(metabuffer) == GIN_METAPAGE_BLKNO);
        metapage = BufferGetPage(metabuffer);
 
-       GinInitPage(metapage, GIN_META, BufferGetPageSize(metabuffer));
+       GinInitMetabuffer(metabuffer);
 
        memcpy(GinPageGetMeta(metapage), &data->metadata, 
sizeof(GinMetaPageData));
        PageSetLSN(metapage, lsn);
@@ -768,6 +768,7 @@ void
 gin_mask(char *pagedata, BlockNumber blkno)
 {
        Page            page = (Page) pagedata;
+       PageHeader      pagehdr = (PageHeader) page;
        GinPageOpaque opaque;
 
        mask_page_lsn(page);
@@ -776,18 +777,11 @@ gin_mask(char *pagedata, BlockNumber blkno)
        mask_page_hint_bits(page);
 
        /*
-        * GIN metapage doesn't use pd_lower/pd_upper. Other page types do. 
Hence,
-        * we need to apply masking for those pages.
+        * For GIN_DELETED page, the page is initialized to empty. Hence, mask
+        * the page content.
         */
-       if (opaque->flags != GIN_META)
-       {
-               /*
-                * For GIN_DELETED page, the page is initialized to empty. 
Hence, mask
-                * the page content.
-                */
-               if (opaque->flags & GIN_DELETED)
-                       mask_page_content(page);
-               else
-                       mask_unused_space(page);
-       }
+       if (opaque->flags & GIN_DELETED)
+               mask_page_content(page);
+       else if (pagehdr->pd_lower != 0)
+               mask_unused_space(page);
 }
-- 
2.11.0

From 202ef67cf10060e09865fd2ce2e77f4145d7be64 Mon Sep 17 00:00:00 2001
From: amit <amitlangot...@gmail.com>
Date: Mon, 26 Jun 2017 15:13:32 +0900
Subject: [PATCH 2/3] Set pd_lower correctly in the BRIN index metapage

Also tell xlog.c to treat the metapage like a standard page, so any
hole in it is compressed.
---
 src/backend/access/brin/brin.c         |  4 ++--
 src/backend/access/brin/brin_pageops.c |  9 +++++++++
 src/backend/access/brin/brin_revmap.c  | 11 ++++++++++-
 src/backend/access/brin/brin_xlog.c    | 18 ++++++++++++++++--
 4 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index b3aa6d1ced..e6909d7aea 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -685,7 +685,7 @@ brinbuild(Relation heap, Relation index, IndexInfo 
*indexInfo)
 
                XLogBeginInsert();
                XLogRegisterData((char *) &xlrec, SizeOfBrinCreateIdx);
-               XLogRegisterBuffer(0, meta, REGBUF_WILL_INIT);
+               XLogRegisterBuffer(0, meta, REGBUF_WILL_INIT | REGBUF_STANDARD);
 
                recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_CREATE_INDEX);
 
@@ -742,7 +742,7 @@ brinbuildempty(Relation index)
        brin_metapage_init(BufferGetPage(metabuf), BrinGetPagesPerRange(index),
                                           BRIN_CURRENT_VERSION);
        MarkBufferDirty(metabuf);
-       log_newpage_buffer(metabuf, false);
+       log_newpage_buffer(metabuf, true);
        END_CRIT_SECTION();
 
        UnlockReleaseBuffer(metabuf);
diff --git a/src/backend/access/brin/brin_pageops.c 
b/src/backend/access/brin/brin_pageops.c
index 80f803e438..92903f38c7 100644
--- a/src/backend/access/brin/brin_pageops.c
+++ b/src/backend/access/brin/brin_pageops.c
@@ -491,6 +491,15 @@ brin_metapage_init(Page page, BlockNumber pagesPerRange, 
uint16 version)
         * revmap page to be created when the index is.
         */
        metadata->lastRevmapPage = 0;
+
+       /*
+        * Set pd_lower just past the end of the metadata.  This is not 
essential
+        * but it makes the page look compressible to xlog.c, because we pass 
the
+        * buffer containing this page to XLogRegisterBuffer() as a page with
+        * standard layout.
+        */
+       ((PageHeader) page)->pd_lower =
+               ((char *) metadata + sizeof(BrinMetaPageData)) - (char *) page;
 }
 
 /*
diff --git a/src/backend/access/brin/brin_revmap.c 
b/src/backend/access/brin/brin_revmap.c
index 22f2076887..4b056c68a2 100644
--- a/src/backend/access/brin/brin_revmap.c
+++ b/src/backend/access/brin/brin_revmap.c
@@ -624,6 +624,15 @@ revmap_physical_extend(BrinRevmap *revmap)
        MarkBufferDirty(buf);
 
        metadata->lastRevmapPage = mapBlk;
+
+       /*
+        * Set pd_lower just past the end of the metadata.  This is not 
essential
+        * but it makes the page look compressible to xlog.c, because we pass 
the
+        * buffer containing this page to XLogRegisterBuffer() as a page with
+        * standard layout.
+        */
+       ((PageHeader) metapage)->pd_lower =
+               ((char *) metadata + sizeof(BrinMetaPageData)) - (char *) 
metapage;
        MarkBufferDirty(revmap->rm_metaBuf);
 
        if (RelationNeedsWAL(revmap->rm_irel))
@@ -635,7 +644,7 @@ revmap_physical_extend(BrinRevmap *revmap)
 
                XLogBeginInsert();
                XLogRegisterData((char *) &xlrec, SizeOfBrinRevmapExtend);
-               XLogRegisterBuffer(0, revmap->rm_metaBuf, 0);
+               XLogRegisterBuffer(0, revmap->rm_metaBuf, REGBUF_STANDARD);
 
                XLogRegisterBuffer(1, buf, REGBUF_WILL_INIT);
 
diff --git a/src/backend/access/brin/brin_xlog.c 
b/src/backend/access/brin/brin_xlog.c
index dff7198a39..e94b0033bf 100644
--- a/src/backend/access/brin/brin_xlog.c
+++ b/src/backend/access/brin/brin_xlog.c
@@ -234,6 +234,15 @@ brin_xlog_revmap_extend(XLogReaderState *record)
                metadata->lastRevmapPage = xlrec->targetBlk;
 
                PageSetLSN(metapg, lsn);
+
+               /*
+                * Set pd_lower just past the end of the metadata.  This is not
+                * essential but it makes the page look compressible to xlog.c, 
because
+                * we pass the buffer containing this page to 
XLogRegisterBuffer() as a
+                * page with standard layout.
+                */
+               ((PageHeader) metapg)->pd_lower =
+                       ((char *) metadata + sizeof(BrinMetaPageData)) - (char 
*) metapg;
                MarkBufferDirty(metabuf);
        }
 
@@ -331,14 +340,19 @@ void
 brin_mask(char *pagedata, BlockNumber blkno)
 {
        Page            page = (Page) pagedata;
+       PageHeader      pagehdr = (PageHeader) page;
 
        mask_page_lsn(page);
 
        mask_page_hint_bits(page);
 
-       if (BRIN_IS_REGULAR_PAGE(page))
+       /*
+        * Regular brin pages contain unused space which needs to be masked.
+        * Similarly for meta pages, but only if pd_lower has been set.
+        */
+       if (BRIN_IS_REGULAR_PAGE(page) ||
+               (BRIN_IS_META_PAGE(page) && pagehdr->pd_lower != 0))
        {
-               /* Regular brin pages contain unused space which needs to be 
masked. */
                mask_unused_space(page);
        }
 }
-- 
2.11.0

From 8617179f96ad3d8cba99b365c836dff5a525c843 Mon Sep 17 00:00:00 2001
From: amit <amitlangot...@gmail.com>
Date: Mon, 26 Jun 2017 15:23:34 +0900
Subject: [PATCH 3/3] Set pd_lower correctly in the SP-GiST index metapage

Also tell xlog.c to treat the metapage like a standard page, so any
hole in it is compressed.
---
 src/backend/access/spgist/spginsert.c |  4 ++--
 src/backend/access/spgist/spgutils.c  | 19 +++++++++++++++++++
 src/backend/access/spgist/spgxlog.c   |  8 +++-----
 3 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/src/backend/access/spgist/spginsert.c 
b/src/backend/access/spgist/spginsert.c
index e4b2c29b0e..80b82e1602 100644
--- a/src/backend/access/spgist/spginsert.c
+++ b/src/backend/access/spgist/spginsert.c
@@ -110,7 +110,7 @@ spgbuild(Relation heap, Relation index, IndexInfo 
*indexInfo)
                 * Replay will re-initialize the pages, so don't take full pages
                 * images.  No other data to log.
                 */
-               XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT);
+               XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT | 
REGBUF_STANDARD);
                XLogRegisterBuffer(1, rootbuffer, REGBUF_WILL_INIT | 
REGBUF_STANDARD);
                XLogRegisterBuffer(2, nullbuffer, REGBUF_WILL_INIT | 
REGBUF_STANDARD);
 
@@ -173,7 +173,7 @@ spgbuildempty(Relation index)
        smgrwrite(index->rd_smgr, INIT_FORKNUM, SPGIST_METAPAGE_BLKNO,
                          (char *) page, true);
        log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
-                               SPGIST_METAPAGE_BLKNO, page, false);
+                               SPGIST_METAPAGE_BLKNO, page, true);
 
        /* Likewise for the root page. */
        SpGistInitPage(page, SPGIST_LEAF);
diff --git a/src/backend/access/spgist/spgutils.c 
b/src/backend/access/spgist/spgutils.c
index 22f64b0103..9048c08f1c 100644
--- a/src/backend/access/spgist/spgutils.c
+++ b/src/backend/access/spgist/spgutils.c
@@ -256,15 +256,25 @@ SpGistUpdateMetaPage(Relation index)
        if (cache != NULL)
        {
                Buffer          metabuffer;
+               Page            metapage;
                SpGistMetaPageData *metadata;
 
                metabuffer = ReadBuffer(index, SPGIST_METAPAGE_BLKNO);
+               metapage = BufferGetPage(metabuffer);
 
                if (ConditionalLockBuffer(metabuffer))
                {
                        metadata = SpGistPageGetMeta(BufferGetPage(metabuffer));
                        metadata->lastUsedPages = cache->lastUsedPages;
 
+                       /*
+                        * Set pd_lower just past the end of the metadata.  
This is not
+                        * essential but it makes the page look compressible to 
xlog.c,
+                        * because we pass the buffer containing this page to
+                        * XLogRegisterBuffer() as page with standard layout.
+                        */
+                       ((PageHeader) metapage)->pd_lower = ((char *) metadata +
+                                                       
sizeof(SpGistMetaPageData)) - (char *) metapage;
                        MarkBufferDirty(metabuffer);
                        UnlockReleaseBuffer(metabuffer);
                }
@@ -534,6 +544,15 @@ SpGistInitMetapage(Page page)
        /* initialize last-used-page cache to empty */
        for (i = 0; i < SPGIST_CACHED_PAGES; i++)
                metadata->lastUsedPages.cachedPage[i].blkno = 
InvalidBlockNumber;
+
+       /*
+        * Set pd_lower just past the end of the metadata.  This is not 
essential
+        * but it makes the page look compressible to xlog.c, because we pass 
the
+        * buffer containing this page to XLogRegisterBuffer() as page with
+        * standard layout.
+        */
+       ((PageHeader) page)->pd_lower =
+               ((char *) metadata + sizeof(SpGistMetaPageData)) - (char *) 
page;
 }
 
 /*
diff --git a/src/backend/access/spgist/spgxlog.c 
b/src/backend/access/spgist/spgxlog.c
index c440d21715..bf209416f2 100644
--- a/src/backend/access/spgist/spgxlog.c
+++ b/src/backend/access/spgist/spgxlog.c
@@ -1033,15 +1033,13 @@ void
 spg_mask(char *pagedata, BlockNumber blkno)
 {
        Page            page = (Page) pagedata;
+       PageHeader      pagehdr = (PageHeader) page;
 
        mask_page_lsn(page);
 
        mask_page_hint_bits(page);
 
-       /*
-        * Any SpGist page other than meta contains unused space which needs to 
be
-        * masked.
-        */
-       if (!SpGistPageIsMeta(page))
+       /* Mask the unused space, provided the page's pd_lower is set. */
+       if (pagehdr->pd_lower != 0)
                mask_unused_space(page);
 }
-- 
2.11.0

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to