Hi all,
Please find attached the patch that re-enables
support for sequences within the pgstattuple extension.
I have also included the necessary test cases for
sequences, implemented in the form of regress tests.

Here is the commitfest link for the same -
https://commitfest.postgresql.org/49/5215/

Regards
Ayush Vatsa
AWS
From 95ab0aa019e1cfec73bc94448faeafeac67b434e Mon Sep 17 00:00:00 2001
From: Ayush Vatsa <ayuvatsa@amazon.com>
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            |  6 ++++-
 contrib/pgstattuple/sql/pgstattuple.sql      | 10 ++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/contrib/pgstattuple/expected/pgstattuple.out b/contrib/pgstattuple/expected/pgstattuple.out
index 283856e109..1e79fd9036 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 * from pgstattuple('serial');
+ table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent 
+-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
+      8192 |           1 |        41 |           0.5 |                0 |              0 |                  0 |       8104 |        98.93
+(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 * 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..13c70c4152 100644
--- a/contrib/pgstattuple/pgstattuple.c
+++ b/contrib/pgstattuple/pgstattuple.c
@@ -323,7 +323,11 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
 	pgstattuple_type stat = {0};
 	SnapshotData SnapshotDirty;
 
-	if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
+	/**
+	 * Sequences don't fall under heap AM but are still
+	 * allowed for obtaining tuple-level statistics.
+	 */
+	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..ea121df0c7 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 * from pgstattuple('serial');
+select pgstatindex('serial');
+select pgstatginindex('serial');
+select pgstathashindex('serial');
+select pg_relpages('serial');
+select * from pgstattuple_approx('serial');
+
 drop table test_partitioned;
 drop view test_view;
 drop foreign table test_foreign_table;
-- 
2.41.0

Reply via email to