pageinspect: Fix handling of all-zero pages
authorMichael Paquier <[email protected]>
Thu, 14 Apr 2022 06:09:46 +0000 (15:09 +0900)
committerMichael Paquier <[email protected]>
Thu, 14 Apr 2022 06:09:46 +0000 (15:09 +0900)
Getting from get_raw_page() an all-zero page is considered as a valid
case by the buffer manager and it can happen for example when finding a
corrupted page with zero_damaged_pages enabled (using zero_damaged_pages
to look at corrupted pages happens), or after a crash when a relation
file is extended before any WAL for its new data is generated (before a
vacuum or autovacuum job comes in to do some cleanup).

However, all the functions of pageinspect, as of the index AMs (except
hash that has its own idea of new pages), heap, the FSM or the page
header have never worked with all-zero pages, causing various crashes
when going through the page internals.

This commit changes all the pageinspect functions to be compliant with
all-zero pages, where the choice is made to return NULL or no rows for
SRFs when finding a new page.  get_raw_page() still works the same way,
returning a batch of zeros in the bytea of the page retrieved.  A hard
error could be used but NULL, while more invasive, is useful when
scanning relation files in full to get a batch of results for a single
relation in one query.  Tests are added for all the code paths
impacted.

Reported-by: Daria Lepikhova
Author: Michael Paquier
Discussion: https://postgr.es/m/561e187b-3549-c8d5-03f5-525c14e65bd0@postgrespro.ru
Backpatch-through: 10

15 files changed:
contrib/pageinspect/brinfuncs.c
contrib/pageinspect/btreefuncs.c
contrib/pageinspect/expected/brin.out
contrib/pageinspect/expected/btree.out
contrib/pageinspect/expected/gin.out
contrib/pageinspect/expected/hash.out
contrib/pageinspect/expected/page.out
contrib/pageinspect/fsmfuncs.c
contrib/pageinspect/ginfuncs.c
contrib/pageinspect/rawpage.c
contrib/pageinspect/sql/brin.sql
contrib/pageinspect/sql/btree.sql
contrib/pageinspect/sql/gin.sql
contrib/pageinspect/sql/hash.sql
contrib/pageinspect/sql/page.sql

index 3472e0ebb123ecf866697b00d1f1b857332afdf5..dc0e0390ece47740e6a8fedc6779146bf0e22c8d 100644 (file)
@@ -60,6 +60,9 @@ brin_page_type(PG_FUNCTION_ARGS)
 
    page = get_page_from_raw(raw_page);
 
+   if (PageIsNew(page))
+       PG_RETURN_NULL();
+
    /* verify the special space has the expected size */
    if (PageGetSpecialSize(page) != MAXALIGN(sizeof(BrinSpecialSpace)))
            ereport(ERROR,
@@ -97,6 +100,9 @@ verify_brin_page(bytea *raw_page, uint16 type, const char *strtype)
 {
    Page        page = get_page_from_raw(raw_page);
 
+   if (PageIsNew(page))
+       return page;
+
    /* verify the special space has the expected size */
    if (PageGetSpecialSize(page) != MAXALIGN(sizeof(BrinSpecialSpace)))
            ereport(ERROR,
@@ -184,6 +190,13 @@ brin_page_items(PG_FUNCTION_ARGS)
    /* minimally verify the page we got */
    page = verify_brin_page(raw_page, BRIN_PAGETYPE_REGULAR, "regular");
 
+   if (PageIsNew(page))
+   {
+       brin_free_desc(bdesc);
+       index_close(indexRel, AccessShareLock);
+       PG_RETURN_NULL();
+   }
+
    /*
     * Initialize output functions for all indexed datatypes; simplifies
     * calling them later.
@@ -350,6 +363,9 @@ brin_metapage_info(PG_FUNCTION_ARGS)
 
    page = verify_brin_page(raw_page, BRIN_PAGETYPE_META, "metapage");
 
+   if (PageIsNew(page))
+       PG_RETURN_NULL();
+
    /* Build a tuple descriptor for our result type */
    if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
        elog(ERROR, "return type must be a row type");
@@ -401,6 +417,12 @@ brin_revmap_data(PG_FUNCTION_ARGS)
        /* minimally verify the page we got */
        page = verify_brin_page(raw_page, BRIN_PAGETYPE_REVMAP, "revmap");
 
+       if (PageIsNew(page))
+       {
+           MemoryContextSwitchTo(mctx);
+           PG_RETURN_NULL();
+       }
+
        state = palloc(sizeof(*state));
        state->tids = ((RevmapContents *) PageGetContents(page))->rm_tids;
        state->idx = 0;
index 59d1f54e2373f969f80298e2043286a3bea04961..1f5a3334e7be4736e2b68d37bd7f122fc330cf74 100644 (file)
@@ -447,6 +447,12 @@ bt_page_items_bytea(PG_FUNCTION_ARGS)
 
        uargs->page = get_page_from_raw(raw_page);
 
+       if (PageIsNew(uargs->page))
+       {
+           MemoryContextSwitchTo(mctx);
+           PG_RETURN_NULL();
+       }
+
        uargs->offset = FirstOffsetNumber;
 
        /* verify the special space has the expected size */
index 62ee783b604d1b08a2ee15392f1a6f6a4c682c9f..d19cdc3b957f8fe1910a3e7e66679331504a17c7 100644 (file)
@@ -62,4 +62,29 @@ ERROR:  input page is not a valid BRIN page
 SELECT * FROM brin_revmap_data(get_raw_page('test1', 0));
 ERROR:  input page is not a valid BRIN page
 \set VERBOSITY default
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT brin_page_type(decode(repeat('00', :block_size), 'hex'));
+ brin_page_type 
+----------------
+(1 row)
+
+SELECT brin_page_items(decode(repeat('00', :block_size), 'hex'), 'test1_a_idx');
+ brin_page_items 
+-----------------
+(0 rows)
+
+SELECT brin_metapage_info(decode(repeat('00', :block_size), 'hex'));
+ brin_metapage_info 
+--------------------
+(1 row)
+
+SELECT brin_revmap_data(decode(repeat('00', :block_size), 'hex'));
+ brin_revmap_data 
+------------------
+(1 row)
+
 DROP TABLE test1;
index d01d08bd7d217139c0ea211e85dd80231c9b2b9a..4015dadfc3c0ba28c8c2315f91ab3b64741d245a 100644 (file)
@@ -84,4 +84,10 @@ ERROR:  input page is not a valid btree page
 SELECT bt_page_items(get_raw_page('test1_a_brin', 0));
 ERROR:  input page is not a valid btree page
 \set VERBOSITY default
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT bt_page_items(decode(repeat('00', :block_size), 'hex'));
+-[ RECORD 1 ]-+-
+bt_page_items | 
+
 DROP TABLE test1;
index c1f93ffaa199b7ef6c07ab9b0381504327215e6e..5f49ff577bf8c745f8dd8c761259cc013829cd83 100644 (file)
@@ -54,3 +54,17 @@ ERROR:  input page is not a valid GIN data leaf page
 SELECT * FROM gin_leafpage_items(get_raw_page('test1', 0));
 ERROR:  input page is not a valid GIN data leaf page
 \set VERBOSITY default
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT gin_leafpage_items(decode(repeat('00', :block_size), 'hex'));
+-[ RECORD 1 ]------+-
+gin_leafpage_items | 
+
+SELECT gin_metapage_info(decode(repeat('00', :block_size), 'hex'));
+-[ RECORD 1 ]-----+-
+gin_metapage_info | 
+
+SELECT gin_page_opaque_info(decode(repeat('00', :block_size), 'hex'));
+-[ RECORD 1 ]--------+-
+gin_page_opaque_info | 
+
index 7db8abe0998839386bcc5835081a6cbed61293fb..82f18752882e3a05d694ff9d7222a918fc2a3220 100644 (file)
@@ -186,4 +186,16 @@ ERROR:  input page is not a valid hash page
 SELECT hash_page_type(get_raw_page('test_hash', 0));
 ERROR:  input page is not a valid hash page
 \set VERBOSITY default
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT hash_metapage_info(decode(repeat('00', :block_size), 'hex'));
+ERROR:  page is not a hash meta page
+SELECT hash_page_items(decode(repeat('00', :block_size), 'hex'));
+ERROR:  page is not a hash bucket or overflow page
+SELECT hash_page_stats(decode(repeat('00', :block_size), 'hex'));
+ERROR:  page is not a hash bucket or overflow page
+SELECT hash_page_type(decode(repeat('00', :block_size), 'hex'));
+-[ RECORD 1 ]--+-------
+hash_page_type | unused
+
 DROP TABLE test_hash;
index f62a3f7bd56e44a0157021e63d2b5e5a0dcd4bb4..4666d6c688372626dd405a11d92daf2fff22f470 100644 (file)
@@ -120,3 +120,23 @@ ERROR:  invalid page size
 SELECT page_header('ccc'::bytea);
 ERROR:  invalid page size
 \set VERBOSITY default
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT fsm_page_contents(decode(repeat('00', :block_size), 'hex'));
+ fsm_page_contents 
+-------------------
+(1 row)
+
+SELECT page_header(decode(repeat('00', :block_size), 'hex'));
+      page_header      
+-----------------------
+ (0/0,0,0,0,0,0,0,0,0)
+(1 row)
+
+SELECT page_checksum(decode(repeat('00', :block_size), 'hex'), 1);
+ page_checksum 
+---------------
+              
+(1 row)
+
index 51a252c87e380dd59e6f6d918a3a1ec05440fea9..4db3be4c30eeea637900625b599ebc823bc4bf13 100644 (file)
@@ -47,6 +47,10 @@ fsm_page_contents(PG_FUNCTION_ARGS)
                 (errmsg("must be superuser to use raw page functions"))));
 
    page = get_page_from_raw(raw_page);
+
+   if (PageIsNew(page))
+       PG_RETURN_NULL();
+
    fsmpage = (FSMPage) PageGetContents(page);
 
    initStringInfo(&sinfo);
index e9ed8bf8c966fe61d9b34521f8e0a77a2d67bb7f..f6de80749c74d7b98ecfc2a48a2d5db62d26e97a 100644 (file)
@@ -50,6 +50,9 @@ gin_metapage_info(PG_FUNCTION_ARGS)
 
    page = get_page_from_raw(raw_page);
 
+   if (PageIsNew(page))
+       PG_RETURN_NULL();
+
    if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -116,6 +119,9 @@ gin_page_opaque_info(PG_FUNCTION_ARGS)
 
    page = get_page_from_raw(raw_page);
 
+   if (PageIsNew(page))
+       PG_RETURN_NULL();
+
    if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -200,6 +206,12 @@ gin_leafpage_items(PG_FUNCTION_ARGS)
 
        page = get_page_from_raw(raw_page);
 
+       if (PageIsNew(page))
+       {
+           MemoryContextSwitchTo(mctx);
+           PG_RETURN_NULL();
+       }
+
        if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
index 6b3e99f8b2b629636545f6a8baf443025dce546d..c70cf61c6ae42e0f2265203ab9b444accae573f5 100644 (file)
@@ -292,5 +292,8 @@ page_checksum(PG_FUNCTION_ARGS)
 
    page = get_page_from_raw(raw_page);
 
+   if (PageIsNew(page))
+       PG_RETURN_NULL();
+
    PG_RETURN_INT16(pg_checksum_page((char *) page, blkno));
 }
index dc5d1661b6d03c3e5ccf4aa17651e03d4df2e981..45098c1ef5e4baa34e5f4e9dc0ad79e59c846dbc 100644 (file)
@@ -27,4 +27,11 @@ SELECT * FROM brin_metapage_info(get_raw_page('test1', 0));
 SELECT * FROM brin_revmap_data(get_raw_page('test1', 0));
 \set VERBOSITY default
 
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT brin_page_type(decode(repeat('00', :block_size), 'hex'));
+SELECT brin_page_items(decode(repeat('00', :block_size), 'hex'), 'test1_a_idx');
+SELECT brin_metapage_info(decode(repeat('00', :block_size), 'hex'));
+SELECT brin_revmap_data(decode(repeat('00', :block_size), 'hex'));
+
 DROP TABLE test1;
index cde0083586cb523b107209a1f228d975b2ceac29..75587c1aca1f5fa058626a01c4a08abf24fbb462 100644 (file)
@@ -41,4 +41,8 @@ SELECT bt_page_items(get_raw_page('test1', 0));
 SELECT bt_page_items(get_raw_page('test1_a_brin', 0));
 \set VERBOSITY default
 
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT bt_page_items(decode(repeat('00', :block_size), 'hex'));
+
 DROP TABLE test1;
index 342354a481f780a46e33680f04a88396da8828f1..b0c20c03073990d8ea0b185cb56d7e583f8e02a0 100644 (file)
@@ -31,3 +31,9 @@ SELECT * FROM gin_metapage_info(get_raw_page('test1', 0));
 SELECT * FROM gin_page_opaque_info(get_raw_page('test1', 0));
 SELECT * FROM gin_leafpage_items(get_raw_page('test1', 0));
 \set VERBOSITY default
+
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT gin_leafpage_items(decode(repeat('00', :block_size), 'hex'));
+SELECT gin_metapage_info(decode(repeat('00', :block_size), 'hex'));
+SELECT gin_page_opaque_info(decode(repeat('00', :block_size), 'hex'));
index 8fd209c815b5f030b895dbac8ff988e5a536dab5..3acdc7d9554d79fdacc9ae8c5a1d2bf01780c870 100644 (file)
@@ -96,4 +96,11 @@ SELECT hash_page_stats(get_raw_page('test_hash', 0));
 SELECT hash_page_type(get_raw_page('test_hash', 0));
 \set VERBOSITY default
 
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT hash_metapage_info(decode(repeat('00', :block_size), 'hex'));
+SELECT hash_page_items(decode(repeat('00', :block_size), 'hex'));
+SELECT hash_page_stats(decode(repeat('00', :block_size), 'hex'));
+SELECT hash_page_type(decode(repeat('00', :block_size), 'hex'));
+
 DROP TABLE test_hash;
index 068dc582efc30ca1583792c4173c29667ce49ea9..7ded5ef1f4cba29e2ab165a49383ea37d19ef96b 100644 (file)
@@ -58,3 +58,9 @@ SELECT fsm_page_contents('aaa'::bytea);
 SELECT page_checksum('bbb'::bytea, 0);
 SELECT page_header('ccc'::bytea);
 \set VERBOSITY default
+
+-- Tests with all-zero pages.
+SHOW block_size \gset
+SELECT fsm_page_contents(decode(repeat('00', :block_size), 'hex'));
+SELECT page_header(decode(repeat('00', :block_size), 'hex'));
+SELECT page_checksum(decode(repeat('00', :block_size), 'hex'), 1);