On Wed, 14 Jan 2026 at 16:40, Álvaro Herrera <[email protected]> wrote:
> On 2026-Jan-14, Japin Li wrote:
>
>> -#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
>> -#define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
>> +#define IS_BTREE(r) (IS_INDEX(r) && (r)->rd_rel->relam == BTREE_AM_OID)
>
> I find this coding rather pointless.  You can more easily do something
> like
>
> #define IS_BTREE(r) ((r)->rd_rel->relkind == RELKIND_INDEX && 
> (r)->rd_rel->relam == BTREE_AM_OID)
>
> and get rid of the IS_INDEX macro completely, if it's not used anywhere
> else.  Same for all the other index AMs.
>

Thanks for the review.

I've fixed it according to your suggestion.

> -- 
> Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/

-- 
Regards,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.

>From 95bc5110a0dd80511397131cf83399bbf7e24a45 Mon Sep 17 00:00:00 2001
From: Japin Li <[email protected]>
Date: Wed, 14 Jan 2026 09:55:46 +0800
Subject: [PATCH v3] Check relkind for both BRIN and GIST indexes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Previously, only the access method was checked for BRIN and GIST indexes,
allowing non-index relations to be passed.

This commit also removes the pointless IS_INDEX macro.

Suggested-by: Japin Li <[email protected]>
Suggested-by: Andrey Borodin <[email protected]>
Reviewed-by: Andreas Karlsson <[email protected]>
Reviewed-by: Kirill Reshke <[email protected]>
Reviewed-by: Álvaro Herrera <[email protected]>

Discussion: https://postgr.es/m/meapr01mb3031a889d4b3f610e9d2a3afb6...@meapr01mb3031.ausprd01.prod.outlook.com
Discussion: https://postgr.es/m/[email protected]
---
 contrib/pageinspect/brinfuncs.c  | 2 +-
 contrib/pageinspect/btreefuncs.c | 7 +++----
 contrib/pageinspect/gistfuncs.c  | 2 +-
 contrib/pageinspect/hashfuncs.c  | 5 ++---
 4 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/contrib/pageinspect/brinfuncs.c b/contrib/pageinspect/brinfuncs.c
index 26cf78252ed..6761a109745 100644
--- a/contrib/pageinspect/brinfuncs.c
+++ b/contrib/pageinspect/brinfuncs.c
@@ -28,7 +28,7 @@ PG_FUNCTION_INFO_V1(brin_page_items);
 PG_FUNCTION_INFO_V1(brin_metapage_info);
 PG_FUNCTION_INFO_V1(brin_revmap_data);
 
-#define IS_BRIN(r) ((r)->rd_rel->relam == BRIN_AM_OID)
+#define IS_BRIN(r) ((r)->rd_rel->relkind == RELKIND_INDEX && (r)->rd_rel->relam == BRIN_AM_OID)
 
 typedef struct brin_column_state
 {
diff --git a/contrib/pageinspect/btreefuncs.c b/contrib/pageinspect/btreefuncs.c
index 0585b7cee40..9a3f08daceb 100644
--- a/contrib/pageinspect/btreefuncs.c
+++ b/contrib/pageinspect/btreefuncs.c
@@ -49,8 +49,7 @@ PG_FUNCTION_INFO_V1(bt_page_stats_1_9);
 PG_FUNCTION_INFO_V1(bt_page_stats);
 PG_FUNCTION_INFO_V1(bt_multi_page_stats);
 
-#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
-#define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
+#define IS_BTREE(r) ((r)->rd_rel->relkind == RELKIND_INDEX && (r)->rd_rel->relam == BTREE_AM_OID)
 
 /* ------------------------------------------------
  * structure for single btree page statistics
@@ -225,7 +224,7 @@ check_relation_block_range(Relation rel, int64 blkno)
 static void
 bt_index_block_validate(Relation rel, int64 blkno)
 {
-	if (!IS_INDEX(rel) || !IS_BTREE(rel))
+	if (!IS_BTREE(rel))
 		ereport(ERROR,
 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
 				 errmsg("\"%s\" is not a %s index",
@@ -858,7 +857,7 @@ bt_metap(PG_FUNCTION_ARGS)
 	relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
 	rel = relation_openrv(relrv, AccessShareLock);
 
-	if (!IS_INDEX(rel) || !IS_BTREE(rel))
+	if (!IS_BTREE(rel))
 		ereport(ERROR,
 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
 				 errmsg("\"%s\" is not a %s index",
diff --git a/contrib/pageinspect/gistfuncs.c b/contrib/pageinspect/gistfuncs.c
index a205cb8a334..e472a46596f 100644
--- a/contrib/pageinspect/gistfuncs.c
+++ b/contrib/pageinspect/gistfuncs.c
@@ -30,7 +30,7 @@ PG_FUNCTION_INFO_V1(gist_page_opaque_info);
 PG_FUNCTION_INFO_V1(gist_page_items);
 PG_FUNCTION_INFO_V1(gist_page_items_bytea);
 
-#define IS_GIST(r) ((r)->rd_rel->relam == GIST_AM_OID)
+#define IS_GIST(r) ((r)->rd_rel->relkind == RELKIND_INDEX && (r)->rd_rel->relam == GIST_AM_OID)
 
 
 static Page verify_gist_page(bytea *raw_page);
diff --git a/contrib/pageinspect/hashfuncs.c b/contrib/pageinspect/hashfuncs.c
index 7fc97d043ce..d3242a386f1 100644
--- a/contrib/pageinspect/hashfuncs.c
+++ b/contrib/pageinspect/hashfuncs.c
@@ -28,8 +28,7 @@ PG_FUNCTION_INFO_V1(hash_page_items);
 PG_FUNCTION_INFO_V1(hash_bitmap_info);
 PG_FUNCTION_INFO_V1(hash_metapage_info);
 
-#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
-#define IS_HASH(r) ((r)->rd_rel->relam == HASH_AM_OID)
+#define IS_HASH(r) ((r)->rd_rel->relkind == RELKIND_INDEX && (r)->rd_rel->relam == HASH_AM_OID)
 
 /* ------------------------------------------------
  * structure for single hash page statistics
@@ -421,7 +420,7 @@ hash_bitmap_info(PG_FUNCTION_ARGS)
 	 */
 	indexRel = relation_open(indexRelid, AccessShareLock);
 
-	if (!IS_INDEX(indexRel) || !IS_HASH(indexRel))
+	if (!IS_HASH(indexRel))
 		ereport(ERROR,
 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
 				 errmsg("\"%s\" is not a %s index",
-- 
2.43.0

Reply via email to