diff options
author | Michael Paquier <michael@paquier.xyz> | 2022-04-14 15:08:03 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2022-04-14 15:08:03 +0900 |
commit | cd4868a5700fadf5a840d44686658517433b338c (patch) | |
tree | 3b44292dea4d834b47726434a4082c687c0c764c /contrib/pageinspect | |
parent | a00fd066b1b632e675bae74841a87de1ffc1cd33 (diff) | |
download | postgresql-cd4868a5700fadf5a840d44686658517433b338c.tar.gz postgresql-cd4868a5700fadf5a840d44686658517433b338c.zip |
pageinspect: Fix handling of all-zero pages
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
Diffstat (limited to 'contrib/pageinspect')
-rw-r--r-- | contrib/pageinspect/brinfuncs.c | 22 | ||||
-rw-r--r-- | contrib/pageinspect/btreefuncs.c | 6 | ||||
-rw-r--r-- | contrib/pageinspect/expected/brin.out | 25 | ||||
-rw-r--r-- | contrib/pageinspect/expected/btree.out | 6 | ||||
-rw-r--r-- | contrib/pageinspect/expected/gin.out | 14 | ||||
-rw-r--r-- | contrib/pageinspect/expected/gist.out | 18 | ||||
-rw-r--r-- | contrib/pageinspect/expected/hash.out | 12 | ||||
-rw-r--r-- | contrib/pageinspect/expected/page.out | 20 | ||||
-rw-r--r-- | contrib/pageinspect/fsmfuncs.c | 4 | ||||
-rw-r--r-- | contrib/pageinspect/ginfuncs.c | 13 | ||||
-rw-r--r-- | contrib/pageinspect/gistfuncs.c | 12 | ||||
-rw-r--r-- | contrib/pageinspect/rawpage.c | 3 | ||||
-rw-r--r-- | contrib/pageinspect/sql/brin.sql | 7 | ||||
-rw-r--r-- | contrib/pageinspect/sql/btree.sql | 4 | ||||
-rw-r--r-- | contrib/pageinspect/sql/gin.sql | 6 | ||||
-rw-r--r-- | contrib/pageinspect/sql/gist.sql | 6 | ||||
-rw-r--r-- | contrib/pageinspect/sql/hash.sql | 7 | ||||
-rw-r--r-- | contrib/pageinspect/sql/page.sql | 6 |
18 files changed, 191 insertions, 0 deletions
diff --git a/contrib/pageinspect/brinfuncs.c b/contrib/pageinspect/brinfuncs.c index 0387b2847a8..c2a37277e08 100644 --- a/contrib/pageinspect/brinfuncs.c +++ b/contrib/pageinspect/brinfuncs.c @@ -58,6 +58,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, @@ -95,6 +98,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, @@ -156,6 +162,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. @@ -331,6 +344,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"); @@ -382,6 +398,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; diff --git a/contrib/pageinspect/btreefuncs.c b/contrib/pageinspect/btreefuncs.c index 3daa31c84da..62f2c1b3159 100644 --- a/contrib/pageinspect/btreefuncs.c +++ b/contrib/pageinspect/btreefuncs.c @@ -611,6 +611,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 */ diff --git a/contrib/pageinspect/expected/brin.out b/contrib/pageinspect/expected/brin.out index 62ee783b604..d19cdc3b957 100644 --- a/contrib/pageinspect/expected/brin.out +++ b/contrib/pageinspect/expected/brin.out @@ -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; diff --git a/contrib/pageinspect/expected/btree.out b/contrib/pageinspect/expected/btree.out index 89d21609907..035a81a7592 100644 --- a/contrib/pageinspect/expected/btree.out +++ b/contrib/pageinspect/expected/btree.out @@ -99,4 +99,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; diff --git a/contrib/pageinspect/expected/gin.out b/contrib/pageinspect/expected/gin.out index e9fdb4cf20f..ff1da6a5a17 100644 --- a/contrib/pageinspect/expected/gin.out +++ b/contrib/pageinspect/expected/gin.out @@ -54,4 +54,18 @@ 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 | + DROP TABLE test1; diff --git a/contrib/pageinspect/expected/gist.out b/contrib/pageinspect/expected/gist.out index c18a7091b25..a51bded0cb6 100644 --- a/contrib/pageinspect/expected/gist.out +++ b/contrib/pageinspect/expected/gist.out @@ -87,4 +87,22 @@ ERROR: input page is not a valid GiST page SELECT gist_page_items_bytea(get_raw_page('test_gist_btree', 0)); ERROR: input page is not a valid GiST page \set VERBOSITY default +-- Tests with all-zero pages. +SHOW block_size \gset +SELECT gist_page_items_bytea(decode(repeat('00', :block_size), 'hex')); + gist_page_items_bytea +----------------------- +(0 rows) + +SELECT gist_page_items(decode(repeat('00', :block_size), 'hex'), 'test_gist_idx'::regclass); + gist_page_items +----------------- +(0 rows) + +SELECT gist_page_opaque_info(decode(repeat('00', :block_size), 'hex')); + gist_page_opaque_info +----------------------- + +(1 row) + DROP TABLE test_gist; diff --git a/contrib/pageinspect/expected/hash.out b/contrib/pageinspect/expected/hash.out index 96c95114570..5d6a5188345 100644 --- a/contrib/pageinspect/expected/hash.out +++ b/contrib/pageinspect/expected/hash.out @@ -190,4 +190,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; diff --git a/contrib/pageinspect/expected/page.out b/contrib/pageinspect/expected/page.out index 9bbdda7f180..3bdc37bbf59 100644 --- a/contrib/pageinspect/expected/page.out +++ b/contrib/pageinspect/expected/page.out @@ -218,3 +218,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) + diff --git a/contrib/pageinspect/fsmfuncs.c b/contrib/pageinspect/fsmfuncs.c index b914da1d4a9..2b9679e4541 100644 --- a/contrib/pageinspect/fsmfuncs.c +++ b/contrib/pageinspect/fsmfuncs.c @@ -46,6 +46,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); diff --git a/contrib/pageinspect/ginfuncs.c b/contrib/pageinspect/ginfuncs.c index 1c56fa18cdc..31aca7b0006 100644 --- a/contrib/pageinspect/ginfuncs.c +++ b/contrib/pageinspect/ginfuncs.c @@ -49,6 +49,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), @@ -58,6 +61,7 @@ gin_metapage_info(PG_FUNCTION_ARGS) (int) PageGetSpecialSize(page)))); opaq = GinPageGetOpaque(page); + if (opaq->flags != GIN_META) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), @@ -115,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 +207,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), diff --git a/contrib/pageinspect/gistfuncs.c b/contrib/pageinspect/gistfuncs.c index b2bbf4f6cbc..9c29fbc7aa6 100644 --- a/contrib/pageinspect/gistfuncs.c +++ b/contrib/pageinspect/gistfuncs.c @@ -55,6 +55,9 @@ gist_page_opaque_info(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(GISTPageOpaqueData))) ereport(ERROR, @@ -130,6 +133,9 @@ gist_page_items_bytea(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(GISTPageOpaqueData))) ereport(ERROR, @@ -220,6 +226,12 @@ gist_page_items(PG_FUNCTION_ARGS) page = get_page_from_raw(raw_page); + if (PageIsNew(page)) + { + index_close(indexRel, AccessShareLock); + PG_RETURN_NULL(); + } + /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */ if (GistPageIsDeleted(page)) elog(NOTICE, "page is deleted"); diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c index 92ffb2d930e..730a46b1d84 100644 --- a/contrib/pageinspect/rawpage.c +++ b/contrib/pageinspect/rawpage.c @@ -352,6 +352,9 @@ page_checksum_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version) page = get_page_from_raw(raw_page); + if (PageIsNew(page)) + PG_RETURN_NULL(); + PG_RETURN_INT16(pg_checksum_page((char *) page, blkno)); } diff --git a/contrib/pageinspect/sql/brin.sql b/contrib/pageinspect/sql/brin.sql index dc5d1661b6d..45098c1ef5e 100644 --- a/contrib/pageinspect/sql/brin.sql +++ b/contrib/pageinspect/sql/brin.sql @@ -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; diff --git a/contrib/pageinspect/sql/btree.sql b/contrib/pageinspect/sql/btree.sql index 44d83f90bac..1f554f0f678 100644 --- a/contrib/pageinspect/sql/btree.sql +++ b/contrib/pageinspect/sql/btree.sql @@ -44,4 +44,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; diff --git a/contrib/pageinspect/sql/gin.sql b/contrib/pageinspect/sql/gin.sql index 6499b5c72b7..b57466d7ebf 100644 --- a/contrib/pageinspect/sql/gin.sql +++ b/contrib/pageinspect/sql/gin.sql @@ -32,4 +32,10 @@ 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')); + DROP TABLE test1; diff --git a/contrib/pageinspect/sql/gist.sql b/contrib/pageinspect/sql/gist.sql index e76f6fa8d1c..1edf2b307f6 100644 --- a/contrib/pageinspect/sql/gist.sql +++ b/contrib/pageinspect/sql/gist.sql @@ -44,4 +44,10 @@ SELECT gist_page_items_bytea(get_raw_page('test_gist', 0)); SELECT gist_page_items_bytea(get_raw_page('test_gist_btree', 0)); \set VERBOSITY default +-- Tests with all-zero pages. +SHOW block_size \gset +SELECT gist_page_items_bytea(decode(repeat('00', :block_size), 'hex')); +SELECT gist_page_items(decode(repeat('00', :block_size), 'hex'), 'test_gist_idx'::regclass); +SELECT gist_page_opaque_info(decode(repeat('00', :block_size), 'hex')); + DROP TABLE test_gist; diff --git a/contrib/pageinspect/sql/hash.sql b/contrib/pageinspect/sql/hash.sql index ccc984c0866..320fb9fa9f1 100644 --- a/contrib/pageinspect/sql/hash.sql +++ b/contrib/pageinspect/sql/hash.sql @@ -98,4 +98,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; diff --git a/contrib/pageinspect/sql/page.sql b/contrib/pageinspect/sql/page.sql index 38b16815412..b5c41cc8ac5 100644 --- a/contrib/pageinspect/sql/page.sql +++ b/contrib/pageinspect/sql/page.sql @@ -91,3 +91,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); |