Re: [PATCH] pageinspect: validate line pointers before using them

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: shihao zhong <zhong950419(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, Baji Shaik <baji(dot)pgdev(at)gmail(dot)com>, Greg Burd <greg(at)burd(dot)me>
Subject: Re: [PATCH] pageinspect: validate line pointers before using them
Date: 2026-12-03 23:45:17
Message-ID: CAN4CZFNYggjwuu2haZdp+V7mTbxAAYxdLJ0czEgPt88r4xLKwA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello

1.

CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES (1), (2), (3);
-- Valid pd_lower (36), damaged pd_upper (20):
SELECT lp, lp_off, lp_flags, lp_len
FROM heap_page_items(set_byte(set_byte(get_raw_page('t1', 0), 14,
20), 15, 0));

This is only one example, but the question is generic: instead of
broad refusal, should functions only validate what they require? The
above snippet could work on that specific corrupted page.

2. stored_value_fits / verify_brin_tuple / verify_gist_tuple / ...: is
reimplementing core internals inside contrib/pageinspect a good idea,
or is it something that is fragile, and will diverge/break sooner or
later?

3. The patch only fixes some crashes, leaves many others open. I think
this, together with the above 2 questions the usefulness of it. LLMs
can easily spot remaining failures and create scenarios for them, for
example:

3.a.

CREATE TABLE test1 (x int, y int[]);
INSERT INTO test1 SELECT x, ARRAY[1,10] FROM generate_series(1,10000) x;
CREATE INDEX test1_y_idx ON test1 USING gin (y) WITH (fastupdate = off);
-- 7 continuation bytes (0x80) in cur->bytes:
WITH p AS (SELECT get_raw_page('test1_y_idx',
(pg_relation_size('test1_y_idx') /
current_setting('block_size')::bigint)::int - 1) AS raw)
SELECT g.first_tid, g.nbytes FROM p, LATERAL gin_leafpage_items(
set_byte(set_byte(set_byte(set_byte(set_byte(set_byte(set_byte(raw,
40, 128), 41, 128), 42, 128), 43, 128), 44, 128), 45, 128), 46, 128)
) AS g;

3.b.

CREATE TABLE t_gist_align (a int4range, b int4range, c int4range);
INSERT INTO t_gist_align VALUES ('empty', int4range(1, 2), int4range(3, 4));
CREATE INDEX t_gist_align_idx ON t_gist_align USING gist (a, b, c);
WITH p AS (
SELECT get_raw_page('t_gist_align_idx', 0) AS raw,
(SELECT upper FROM
page_header(get_raw_page('t_gist_align_idx', 0))) AS u
)
SELECT g.* FROM p, LATERAL gist_page_items(
set_byte(set_byte(set_byte(set_byte(set_byte(set_byte(
set_byte(set_byte(set_byte(set_byte(set_byte(set_byte(raw,
u+8+6, 24), u+8+7, 0), u+8+8, 0), u+8+9, 0), u+8+10, 0), u+8+11, 127),
u+8+12, 13), u+8+13, 64), u+8+14, 15), u+8+15, 0), u+8+16, 0), u+8+17, 1),
't_gist_align_idx') AS g;

3.c.

-- pd_upper=1 (!PageIsNew), pd_special=65528 (0xFFF8),
pd_pagesize_version=4 (pagesize=0)
-- PageGetSpecialSize() computes (uint16)(0 - 65528) = 8 ==
MAXALIGN(sizeof(Opaque)):
SELECT brin_page_type(set_byte(set_byte(set_byte(set_byte(set_byte(
decode(repeat('00', 8192), 'hex'), 14, 1), 16, 248), 17, 255), 18,
4), 19, 0));
SELECT gin_metapage_info(set_byte(set_byte(set_byte(set_byte(set_byte(
decode(repeat('00', 8192), 'hex'), 14, 1), 16, 248), 17, 255), 18,
4), 19, 0));
SELECT gin_page_opaque_info(set_byte(set_byte(set_byte(set_byte(set_byte(
decode(repeat('00', 8192), 'hex'), 14, 1), 16, 248), 17, 255), 18,
4), 19, 0));
-- All three crash with: TRAP: failed Assert("((const PageHeaderData
*) page)->pd_special <= BLCKSZ"), bufpage.h:370

etc, there are many more along these.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-12-03 23:55:47 Re: pg_dump writes a subscription's default slot name, which can't be restored
Previous Message Adesh Thakare 2026-12-03 23:40:34 [PG19] pg_signal_autovacuum_worker can't signal parallel autovacuum workers