From d51682891d1a98c565850c68906d7e2f049e9c65 Mon Sep 17 00:00:00 2001 From: Ayush Vatsa Date: Thu, 29 Aug 2024 21:40:29 +0530 Subject: [PATCH v1] Introduced support for sequences back in pgstattuple extension --- contrib/pgstattuple/expected/pgstattuple.out | 24 ++++++++++++++++++++ contrib/pgstattuple/pgstattuple.c | 8 ++++++- contrib/pgstattuple/sql/pgstattuple.sql | 10 ++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/contrib/pgstattuple/expected/pgstattuple.out b/contrib/pgstattuple/expected/pgstattuple.out index 283856e109..1e32370830 100644 --- a/contrib/pgstattuple/expected/pgstattuple.out +++ b/contrib/pgstattuple/expected/pgstattuple.out @@ -273,6 +273,30 @@ select pgstathashindex('test_partition_hash_idx'); (4,8,0,1,0,0,0,100) (1 row) +-- test on sequence +-- only pgstattuple and pg_relpages should work +create sequence serial; +select count(*) from pgstattuple('serial'); + count +------- + 1 +(1 row) + +select pgstatindex('serial'); +ERROR: relation "serial" is not a btree index +select pgstatginindex('serial'); +ERROR: relation "serial" is not a GIN index +select pgstathashindex('serial'); +ERROR: relation "serial" is not a hash index +select pg_relpages('serial'); + pg_relpages +------------- + 1 +(1 row) + +select count(*) from pgstattuple_approx('serial'); +ERROR: relation "serial" is of wrong relation kind +DETAIL: This operation is not supported for sequences. drop table test_partitioned; drop view test_view; drop foreign table test_foreign_table; diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c index 3bd8b96197..d4fc891877 100644 --- a/contrib/pgstattuple/pgstattuple.c +++ b/contrib/pgstattuple/pgstattuple.c @@ -323,7 +323,13 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo) pgstattuple_type stat = {0}; SnapshotData SnapshotDirty; - if (rel->rd_rel->relam != HEAP_TABLE_AM_OID) + /* + * Sequences implicitly use the heap AM, even though it's not explicitly + * recorded in the catalogs. For other relation kinds, verify that the AM + * is heap; otherwise, raise an error. + */ + if (rel->rd_rel->relam != HEAP_TABLE_AM_OID && + rel->rd_rel->relkind != RELKIND_SEQUENCE) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("only heap AM is supported"))); diff --git a/contrib/pgstattuple/sql/pgstattuple.sql b/contrib/pgstattuple/sql/pgstattuple.sql index b08c31c21b..e4bf77eb32 100644 --- a/contrib/pgstattuple/sql/pgstattuple.sql +++ b/contrib/pgstattuple/sql/pgstattuple.sql @@ -119,6 +119,16 @@ create index test_partition_hash_idx on test_partition using hash (a); select pgstatindex('test_partition_idx'); select pgstathashindex('test_partition_hash_idx'); +-- test on sequence +-- only pgstattuple and pg_relpages should work +create sequence serial; +select count(*) from pgstattuple('serial'); +select pgstatindex('serial'); +select pgstatginindex('serial'); +select pgstathashindex('serial'); +select pg_relpages('serial'); +select count(*) from pgstattuple_approx('serial'); + drop table test_partitioned; drop view test_view; drop foreign table test_foreign_table; -- 2.41.0