Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 957ba9f

Browse files
committed
Detect version mismatch in brin_page_items
Commit dae761a modified brin_page_items() to return the new "empty" flag for each BRIN range. But the new output parameter was added in the middle, which may cause crashes when using the new binary with old function definition. The ideal solution would be to introduce API versioning similar to what pg_stat_statements does, but it's too late for that as PG17 was already released (so we can't introduce a new extension version). We could do something similar in brin_page_items() by checking the number of output columns (and ignoring the new flag), but it doesn't seem very nice. Instead, simply error out and suggest updating the extension to the latest version. pageinspect is a superuser-only extension, and there's not much reason to run an older version. Moreover, there's a precedent for this approach in 691e8b2. Reported by Ľuboslav Špilák, investigation and patch by me. Backpatch to 17, same as dae761a. Reported-by: Ľuboslav Špilák Reviewed-by: Michael Paquier, Hayato Kuroda, Peter Geoghegan Backpatch-through: 17 Discussion: https://postgr.es/m/VI1PR02MB63331C3D90E2104FD12399D38A5D2@VI1PR02MB6333.eurprd02.prod.outlook.com Discussion: https://postgr.es/m/flat/3385a58f-5484-49d0-b790-9a198a0bf236@vondra.me
1 parent 8cd44db commit 957ba9f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

contrib/pageinspect/brinfuncs.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ verify_brin_page(bytea *raw_page, uint16 type, const char *strtype)
117117
return page;
118118
}
119119

120+
/* Number of output arguments (columns) for brin_page_items() */
121+
#define BRIN_PAGE_ITEMS_V1_12 8
120122

121123
/*
122124
* Extract all item values from a BRIN index page
@@ -145,6 +147,21 @@ brin_page_items(PG_FUNCTION_ARGS)
145147

146148
InitMaterializedSRF(fcinfo, 0);
147149

150+
/*
151+
* Version 1.12 added a new output column for the empty range flag. But as
152+
* it was added in the middle, it may cause crashes with function
153+
* definitions from older versions of the extension.
154+
*
155+
* There is no way to reliably avoid the problems created by the old
156+
* function definition at this point, so insist that the user update the
157+
* extension.
158+
*/
159+
if (rsinfo->setDesc->natts < BRIN_PAGE_ITEMS_V1_12)
160+
ereport(ERROR,
161+
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
162+
errmsg("function has wrong number of declared columns"),
163+
errhint("To resolve the problem, update the \"pageinspect\" extension to the latest version.")));
164+
148165
indexRel = index_open(indexRelid, AccessShareLock);
149166

150167
if (!IS_BRIN(indexRel))

contrib/pageinspect/expected/oldextversions.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,19 @@ SELECT pagesize, version FROM page_header(get_raw_page('test1', 0));
5252
8192 | 4
5353
(1 row)
5454

55+
-- brin_page_items() added a new "empty" flag in 1.12, make sure we detect
56+
-- an old function definition
57+
ALTER EXTENSION pageinspect UPDATE TO '1.11';
58+
CREATE INDEX test_1_a_brin_idx ON test1 USING BRIN (a);
59+
SELECT * FROM brin_page_items(get_raw_page('test_1_a_brin_idx', 2), 'test_1_a_brin_idx');
60+
ERROR: function has wrong number of declared columns
61+
HINT: To resolve the problem, update the "pageinspect" extension to the latest version.
62+
ALTER EXTENSION pageinspect UPDATE TO '1.12';
63+
SELECT * FROM brin_page_items(get_raw_page('test_1_a_brin_idx', 2), 'test_1_a_brin_idx');
64+
itemoffset | blknum | attnum | allnulls | hasnulls | placeholder | empty | value
65+
------------+--------+--------+----------+----------+-------------+-------+------------------------------------------
66+
1 | 0 | 1 | f | f | f | f | {72057594037927937 .. 72057594037927937}
67+
(1 row)
68+
5569
DROP TABLE test1;
5670
DROP EXTENSION pageinspect;

contrib/pageinspect/sql/oldextversions.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,14 @@ ALTER EXTENSION pageinspect UPDATE TO '1.9';
2222
\df page_header
2323
SELECT pagesize, version FROM page_header(get_raw_page('test1', 0));
2424

25+
-- brin_page_items() added a new "empty" flag in 1.12, make sure we detect
26+
-- an old function definition
27+
ALTER EXTENSION pageinspect UPDATE TO '1.11';
28+
CREATE INDEX test_1_a_brin_idx ON test1 USING BRIN (a);
29+
SELECT * FROM brin_page_items(get_raw_page('test_1_a_brin_idx', 2), 'test_1_a_brin_idx');
30+
31+
ALTER EXTENSION pageinspect UPDATE TO '1.12';
32+
SELECT * FROM brin_page_items(get_raw_page('test_1_a_brin_idx', 2), 'test_1_a_brin_idx');
33+
2534
DROP TABLE test1;
2635
DROP EXTENSION pageinspect;

0 commit comments

Comments
 (0)