This is an automated email from the ASF dual-hosted git repository.

leborchuk pushed a commit to branch REL_2_STABLE
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 3712923e3e525b39e935529a2f2f10213481f72a
Author: Michael Paquier <[email protected]>
AuthorDate: Tue Dec 19 15:20:52 2023 +0900

    pgstattuple: Fix failure with pgstathashindex() for partitioned indexes
    
    As coded, the function relied on index_open() when opening an index
    relation, allowing partitioned indexes to be processed by
    pgstathashindex().  This was leading to a "could not open file" error
    because partitioned indexes have no physical files, or to a crash with
    an assertion failure (like on HEAD).
    
    This issue is fixed by applying the same checks as the other stat
    functions for indexes, with a lookup at both RELKIND_INDEX and the index
    AM expected.
    
    Author: Alexander Lakhin
    Discussion: https://postgr.es/m/[email protected]
    Backpatch-through: 12
    (cherry picked from commit f729fdab450f3429530e7566d50bd0fcf8343966)
---
 contrib/pgstattuple/expected/pgstattuple.out | 11 +++++++----
 contrib/pgstattuple/pgstatindex.c            |  5 ++---
 contrib/pgstattuple/sql/pgstattuple.sql      |  2 ++
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/contrib/pgstattuple/expected/pgstattuple.out 
b/contrib/pgstattuple/expected/pgstattuple.out
index c8542e75416..204b7fab135 100644
--- a/contrib/pgstattuple/expected/pgstattuple.out
+++ b/contrib/pgstattuple/expected/pgstattuple.out
@@ -155,6 +155,7 @@ create table test_partitioned (a int) partition by range 
(a);
 NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' 
as the Apache Cloudberry data distribution key for this table.
 HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make 
sure column(s) chosen are the optimal data distribution key to minimize skew.
 create index test_partitioned_index on test_partitioned(a);
+create index test_partitioned_hash_index on test_partitioned using hash(a);
 -- these should all fail
 select pgstattuple('test_partitioned');
 ERROR:  "test_partitioned" (partitioned table) is not supported
@@ -169,7 +170,9 @@ ERROR:  relation "test_partitioned" is not a btree index
 select pgstatginindex('test_partitioned');
 ERROR:  relation "test_partitioned" is not a GIN index
 select pgstathashindex('test_partitioned');
-ERROR:  "test_partitioned" is not an index
+ERROR:  relation "test_partitioned" is not a hash index
+select pgstathashindex('test_partitioned_hash_index');
+ERROR:  relation "test_partitioned_hash_index" is not a hash index
 create view test_view as select 1;
 -- these should all fail
 select pgstattuple('test_view');
@@ -183,7 +186,7 @@ ERROR:  relation "test_view" is not a btree index
 select pgstatginindex('test_view');
 ERROR:  relation "test_view" is not a GIN index
 select pgstathashindex('test_view');
-ERROR:  "test_view" is not an index
+ERROR:  relation "test_view" is not a hash index
 create foreign data wrapper dummy;
 create server dummy_server foreign data wrapper dummy;
 create foreign table test_foreign_table () server dummy_server;
@@ -199,7 +202,7 @@ ERROR:  relation "test_foreign_table" is not a btree index
 select pgstatginindex('test_foreign_table');
 ERROR:  relation "test_foreign_table" is not a GIN index
 select pgstathashindex('test_foreign_table');
-ERROR:  "test_foreign_table" is not an index
+ERROR:  relation "test_foreign_table" is not a hash index
 -- a partition of a partitioned table should work though
 create table test_partition partition of test_partitioned for values from (1) 
to (100);
 NOTICE:  table has parent, setting distribution columns to match parent table
@@ -246,7 +249,7 @@ ERROR:  relation "test_partition" is not a btree index
 select pgstatginindex('test_partition');
 ERROR:  relation "test_partition" is not a GIN index
 select pgstathashindex('test_partition');
-ERROR:  "test_partition" is not an index
+ERROR:  relation "test_partition" is not a hash index
 -- an actual index of a partitioned table should work though
 create index test_partition_idx on test_partition(a);
 create index test_partition_hash_idx on test_partition using hash (a);
diff --git a/contrib/pgstattuple/pgstatindex.c 
b/contrib/pgstattuple/pgstatindex.c
index fdfaef925cd..770ca2e32b5 100644
--- a/contrib/pgstattuple/pgstatindex.c
+++ b/contrib/pgstattuple/pgstatindex.c
@@ -619,10 +619,9 @@ pgstathashindex(PG_FUNCTION_ARGS)
        float8          free_percent;
        uint64          total_space;
 
-       rel = index_open(relid, AccessShareLock);
+       rel = relation_open(relid, AccessShareLock);
 
-       /* index_open() checks that it's an index */
-       if (!IS_HASH(rel))
+       if (!IS_INDEX(rel) || !IS_HASH(rel))
                ereport(ERROR,
                                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                 errmsg("relation \"%s\" is not a hash index",
diff --git a/contrib/pgstattuple/sql/pgstattuple.sql 
b/contrib/pgstattuple/sql/pgstattuple.sql
index e08b9febc59..3d9914cfca0 100644
--- a/contrib/pgstattuple/sql/pgstattuple.sql
+++ b/contrib/pgstattuple/sql/pgstattuple.sql
@@ -65,6 +65,7 @@ select pgstatginindex('test_hashidx');
 -- check that using any of these functions with unsupported relations will fail
 create table test_partitioned (a int) partition by range (a);
 create index test_partitioned_index on test_partitioned(a);
+create index test_partitioned_hash_index on test_partitioned using hash(a);
 -- these should all fail
 select pgstattuple('test_partitioned');
 select pgstattuple('test_partitioned_index');
@@ -73,6 +74,7 @@ select pg_relpages('test_partitioned');
 select pgstatindex('test_partitioned');
 select pgstatginindex('test_partitioned');
 select pgstathashindex('test_partitioned');
+select pgstathashindex('test_partitioned_hash_index');
 
 create view test_view as select 1;
 -- these should all fail


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to