Skip to content

Commit ca90252

Browse files
Reintroduce support for sequences in pgstattuple and pageinspect.
Commit 4b82664 restricted a number of functions provided by contrib modules to only relations that use the "heap" table access method. Sequences always use this table access method, but they do not advertise as such in the pg_class system catalog, so the aforementioned commit also (presumably unintentionally) removed support for sequences from some of these functions. This commit reintroduces said support for sequences to these functions and adds a couple of relevant tests. Co-authored-by: Ayush Vatsa Reviewed-by: Robert Haas, Michael Paquier, Matthias van de Meent Discussion: https://postgr.es/m/CACX%2BKaP3i%2Bi9tdPLjF5JCHVv93xobEdcd_eB%2B638VDvZ3i%3DcQA%40mail.gmail.com Backpatch-through: 12
1 parent abed06f commit ca90252

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

contrib/pageinspect/expected/page.out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,12 @@ SELECT page_checksum(decode(repeat('00', :block_size), 'hex'), 1);
232232

233233
(1 row)
234234

235+
-- tests for sequences
236+
create temporary sequence test_sequence;
237+
select tuple_data_split('test_sequence'::regclass, t_data, t_infomask, t_infomask2, t_bits)
238+
from heap_page_items(get_raw_page('test_sequence', 0));
239+
tuple_data_split
240+
-------------------------------------------------------
241+
{"\\x0100000000000000","\\x0000000000000000","\\x00"}
242+
(1 row)
243+

contrib/pageinspect/heapfuncs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,11 @@ tuple_data_split_internal(Oid relid, char *tupdata,
318318
raw_attrs = initArrayResult(BYTEAOID, CurrentMemoryContext, false);
319319
nattrs = tupdesc->natts;
320320

321-
if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
321+
/*
322+
* Sequences always use heap AM, but they don't show that in the catalogs.
323+
*/
324+
if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
325+
rel->rd_rel->relam != HEAP_TABLE_AM_OID)
322326
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
323327
errmsg("only heap AM is supported")));
324328

contrib/pageinspect/sql/page.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,8 @@ SHOW block_size \gset
9595
SELECT fsm_page_contents(decode(repeat('00', :block_size), 'hex'));
9696
SELECT page_header(decode(repeat('00', :block_size), 'hex'));
9797
SELECT page_checksum(decode(repeat('00', :block_size), 'hex'), 1);
98+
99+
-- tests for sequences
100+
create temporary sequence test_sequence;
101+
select tuple_data_split('test_sequence'::regclass, t_data, t_infomask, t_infomask2, t_bits)
102+
from heap_page_items(get_raw_page('test_sequence', 0));

contrib/pgstattuple/expected/pgstattuple.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,30 @@ select pgstathashindex('test_partition_hash_idx');
244244
(4,8,0,1,0,0,0,100)
245245
(1 row)
246246

247+
-- these should work for sequences
248+
create sequence test_sequence;
249+
select count(*) from pgstattuple('test_sequence');
250+
count
251+
-------
252+
1
253+
(1 row)
254+
255+
select pg_relpages('test_sequence');
256+
pg_relpages
257+
-------------
258+
1
259+
(1 row)
260+
261+
-- these should fail for sequences
262+
select pgstatindex('test_sequence');
263+
ERROR: relation "test_sequence" is not a btree index
264+
select pgstatginindex('test_sequence');
265+
ERROR: relation "test_sequence" is not a GIN index
266+
select pgstathashindex('test_sequence');
267+
ERROR: relation "test_sequence" is not a hash index
268+
select pgstattuple_approx('test_sequence');
269+
ERROR: "test_sequence" is not a table or materialized view
270+
drop sequence test_sequence;
247271
drop table test_partitioned;
248272
drop view test_view;
249273
drop foreign table test_foreign_table;

contrib/pgstattuple/pgstattuple.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,11 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
335335
pgstattuple_type stat = {0};
336336
SnapshotData SnapshotDirty;
337337

338-
if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
338+
/*
339+
* Sequences always use heap AM, but they don't show that in the catalogs.
340+
*/
341+
if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
342+
rel->rd_rel->relam != HEAP_TABLE_AM_OID)
339343
ereport(ERROR,
340344
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
341345
errmsg("only heap AM is supported")));

contrib/pgstattuple/sql/pgstattuple.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ create index test_partition_hash_idx on test_partition using hash (a);
114114
select pgstatindex('test_partition_idx');
115115
select pgstathashindex('test_partition_hash_idx');
116116

117+
-- these should work for sequences
118+
create sequence test_sequence;
119+
select count(*) from pgstattuple('test_sequence');
120+
select pg_relpages('test_sequence');
121+
122+
-- these should fail for sequences
123+
select pgstatindex('test_sequence');
124+
select pgstatginindex('test_sequence');
125+
select pgstathashindex('test_sequence');
126+
select pgstattuple_approx('test_sequence');
127+
128+
drop sequence test_sequence;
117129
drop table test_partitioned;
118130
drop view test_view;
119131
drop foreign table test_foreign_table;

0 commit comments

Comments
 (0)