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

Commit f610d4f

Browse files
committed
pageinspect: Fix failure with hash_bitmap_info() for partitioned indexes
This function reads directly a page from a relation, relying on index_open() to open the index to read from. Unfortunately, this would crash when using partitioned indexes, as these can be opened with index_open() but they have no physical pages. Alexander has fixed the module, while I have written the test. Author: Alexander Lakhin, Michael Paquier Discussion: https://postgr.es/m/18246-f4d9ff7cb3af77e6@postgresql.org Backpatch-through: 12
1 parent bd2d3c9 commit f610d4f

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

contrib/pageinspect/expected/hash.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
CREATE TABLE test_hash (a int, b text);
22
INSERT INTO test_hash VALUES (1, 'one');
33
CREATE INDEX test_hash_a_idx ON test_hash USING hash (a);
4+
CREATE TABLE test_hash_part (a int, b int) PARTITION BY RANGE (a);
5+
CREATE INDEX test_hash_part_idx ON test_hash_part USING hash(b);
46
\x
57
SELECT hash_page_type(get_raw_page('test_hash_a_idx', 0));
68
-[ RECORD 1 ]--+---------
@@ -40,6 +42,8 @@ SELECT * FROM hash_bitmap_info('test_hash_a_idx', 4);
4042
ERROR: invalid overflow block number 4
4143
SELECT * FROM hash_bitmap_info('test_hash_a_idx', 5);
4244
ERROR: invalid overflow block number 5
45+
SELECT * FROM hash_bitmap_info('test_hash_part_idx', 1); -- error
46+
ERROR: "test_hash_part_idx" is not a hash index
4347
SELECT magic, version, ntuples, bsize, bmsize, bmshift, maxbucket, highmask,
4448
lowmask, ovflpoint, firstfree, nmaps, procid, spares, mapp FROM
4549
hash_metapage_info(get_raw_page('test_hash_a_idx', 0));
@@ -199,3 +203,4 @@ SELECT hash_page_type(decode(repeat('00', :block_size), 'hex'));
199203
hash_page_type | unused
200204

201205
DROP TABLE test_hash;
206+
DROP TABLE test_hash_part;

contrib/pageinspect/hashfuncs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "access/hash.h"
1616
#include "access/htup_details.h"
17+
#include "access/relation.h"
1718
#include "catalog/pg_type.h"
1819
#include "catalog/pg_am.h"
1920
#include "funcapi.h"
@@ -27,6 +28,7 @@ PG_FUNCTION_INFO_V1(hash_page_items);
2728
PG_FUNCTION_INFO_V1(hash_bitmap_info);
2829
PG_FUNCTION_INFO_V1(hash_metapage_info);
2930

31+
#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
3032
#define IS_HASH(r) ((r)->rd_rel->relam == HASH_AM_OID)
3133

3234
/* ------------------------------------------------
@@ -420,9 +422,9 @@ hash_bitmap_info(PG_FUNCTION_ARGS)
420422
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
421423
(errmsg("must be superuser to use raw page functions"))));
422424

423-
indexRel = index_open(indexRelid, AccessShareLock);
425+
indexRel = relation_open(indexRelid, AccessShareLock);
424426

425-
if (!IS_HASH(indexRel))
427+
if (!IS_INDEX(indexRel) || !IS_HASH(indexRel))
426428
ereport(ERROR,
427429
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
428430
errmsg("\"%s\" is not a %s index",

contrib/pageinspect/sql/hash.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ CREATE TABLE test_hash (a int, b text);
22
INSERT INTO test_hash VALUES (1, 'one');
33
CREATE INDEX test_hash_a_idx ON test_hash USING hash (a);
44

5+
CREATE TABLE test_hash_part (a int, b int) PARTITION BY RANGE (a);
6+
CREATE INDEX test_hash_part_idx ON test_hash_part USING hash(b);
7+
58
\x
69

710
SELECT hash_page_type(get_raw_page('test_hash_a_idx', 0));
@@ -19,6 +22,7 @@ SELECT * FROM hash_bitmap_info('test_hash_a_idx', 2);
1922
SELECT * FROM hash_bitmap_info('test_hash_a_idx', 3);
2023
SELECT * FROM hash_bitmap_info('test_hash_a_idx', 4);
2124
SELECT * FROM hash_bitmap_info('test_hash_a_idx', 5);
25+
SELECT * FROM hash_bitmap_info('test_hash_part_idx', 1); -- error
2226

2327

2428
SELECT magic, version, ntuples, bsize, bmsize, bmshift, maxbucket, highmask,
@@ -104,3 +108,4 @@ SELECT hash_page_stats(decode(repeat('00', :block_size), 'hex'));
104108
SELECT hash_page_type(decode(repeat('00', :block_size), 'hex'));
105109

106110
DROP TABLE test_hash;
111+
DROP TABLE test_hash_part;

0 commit comments

Comments
 (0)