Skip to content

Commit 05036a3

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 b0c3061 commit 05036a3

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

contrib/pageinspect/expected/page.out

+9
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,12 @@ SELECT page_checksum(decode(repeat('00', :block_size), 'hex'), 1);
239239

240240
(1 row)
241241

242+
-- tests for sequences
243+
create temporary sequence test_sequence;
244+
select tuple_data_split('test_sequence'::regclass, t_data, t_infomask, t_infomask2, t_bits)
245+
from heap_page_items(get_raw_page('test_sequence', 0));
246+
tuple_data_split
247+
-------------------------------------------------------
248+
{"\\x0100000000000000","\\x0000000000000000","\\x00"}
249+
(1 row)
250+

contrib/pageinspect/heapfuncs.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ tuple_data_split_internal(Oid relid, char *tupdata,
320320
raw_attrs = initArrayResult(BYTEAOID, CurrentMemoryContext, false);
321321
nattrs = tupdesc->natts;
322322

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

contrib/pageinspect/sql/page.sql

+5
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ SHOW block_size \gset
9898
SELECT fsm_page_contents(decode(repeat('00', :block_size), 'hex'));
9999
SELECT page_header(decode(repeat('00', :block_size), 'hex'));
100100
SELECT page_checksum(decode(repeat('00', :block_size), 'hex'), 1);
101+
102+
-- tests for sequences
103+
create temporary sequence test_sequence;
104+
select tuple_data_split('test_sequence'::regclass, t_data, t_infomask, t_infomask2, t_bits)
105+
from heap_page_items(get_raw_page('test_sequence', 0));

contrib/pgstattuple/expected/pgstattuple.out

+25
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,31 @@ select pgstathashindex('test_partition_hash_idx');
273273
(4,8,0,1,0,0,0,100)
274274
(1 row)
275275

276+
-- these should work for sequences
277+
create sequence test_sequence;
278+
select count(*) from pgstattuple('test_sequence');
279+
count
280+
-------
281+
1
282+
(1 row)
283+
284+
select pg_relpages('test_sequence');
285+
pg_relpages
286+
-------------
287+
1
288+
(1 row)
289+
290+
-- these should fail for sequences
291+
select pgstatindex('test_sequence');
292+
ERROR: relation "test_sequence" is not a btree index
293+
select pgstatginindex('test_sequence');
294+
ERROR: relation "test_sequence" is not a GIN index
295+
select pgstathashindex('test_sequence');
296+
ERROR: relation "test_sequence" is not a hash index
297+
select pgstattuple_approx('test_sequence');
298+
ERROR: relation "test_sequence" is of wrong relation kind
299+
DETAIL: This operation is not supported for sequences.
300+
drop sequence test_sequence;
276301
drop table test_partitioned;
277302
drop view test_view;
278303
drop foreign table test_foreign_table;

contrib/pgstattuple/pgstattuple.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,11 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
323323
pgstattuple_type stat = {0};
324324
SnapshotData SnapshotDirty;
325325

326-
if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
326+
/*
327+
* Sequences always use heap AM, but they don't show that in the catalogs.
328+
*/
329+
if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
330+
rel->rd_rel->relam != HEAP_TABLE_AM_OID)
327331
ereport(ERROR,
328332
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
329333
errmsg("only heap AM is supported")));

contrib/pgstattuple/sql/pgstattuple.sql

+12
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ create index test_partition_hash_idx on test_partition using hash (a);
119119
select pgstatindex('test_partition_idx');
120120
select pgstathashindex('test_partition_hash_idx');
121121

122+
-- these should work for sequences
123+
create sequence test_sequence;
124+
select count(*) from pgstattuple('test_sequence');
125+
select pg_relpages('test_sequence');
126+
127+
-- these should fail for sequences
128+
select pgstatindex('test_sequence');
129+
select pgstatginindex('test_sequence');
130+
select pgstathashindex('test_sequence');
131+
select pgstattuple_approx('test_sequence');
132+
133+
drop sequence test_sequence;
122134
drop table test_partitioned;
123135
drop view test_view;
124136
drop foreign table test_foreign_table;

0 commit comments

Comments
 (0)