public inbox for [email protected]  
help / color / mirror / Atom feed
From: Japin Li <[email protected]>
To: PostgreSQL Hackers <[email protected]>
Subject: Add IS_INDEX macro to brin and gist index
Date: Wed, 14 Jan 2026 09:56:43 +0800
Message-ID: <MEAPR01MB3031A889D4B3F610E9D2A3AFB68FA@MEAPR01MB3031.ausprd01.prod.outlook.com> (raw)


Hi, hackers,

While working on pageinspect [0], I noticed that brin_page_items() and
gist_page_items() only checked the access method (IS_BRIN/IS_GIST) but did
not verify that the passed relation is actually an index relation.

To make the check more robust and consistent with other pageinspect index
functions (like btreefuncs.c, hashfuncs.c, etc.), the attached patch:

1. Defines a local helper macro IS_INDEX(r) in both brinfuncs.c and gistfuncs.c.

2. Updates the error check to require both: the relation must be an index and
   use the expected access method.

The change is very small, low-risk, and only affects two functions in
contrib/pageinspect.

[0]: https://www.postgresql.org/message-id/[email protected]...

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


Attachments:

  [text/x-patch] v1-0001-Add-IS_INDEX-macro-to-brin-and-gist-index.patch (1.9K, ../MEAPR01MB3031A889D4B3F610E9D2A3AFB68FA@MEAPR01MB3031.ausprd01.prod.outlook.com/2-v1-0001-Add-IS_INDEX-macro-to-brin-and-gist-index.patch)
  download | inline diff:
From a63f79687c392f0d339350bc85e0eeb213b29cdb Mon Sep 17 00:00:00 2001
From: Japin Li <[email protected]>
Date: Wed, 14 Jan 2026 09:55:46 +0800
Subject: [PATCH v1] Add IS_INDEX macro to brin and gist index

---
 contrib/pageinspect/brinfuncs.c | 3 ++-
 contrib/pageinspect/gistfuncs.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/contrib/pageinspect/brinfuncs.c b/contrib/pageinspect/brinfuncs.c
index 26cf78252ed..4be2225e429 100644
--- a/contrib/pageinspect/brinfuncs.c
+++ b/contrib/pageinspect/brinfuncs.c
@@ -28,6 +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_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
 #define IS_BRIN(r) ((r)->rd_rel->relam == BRIN_AM_OID)
 
 typedef struct brin_column_state
@@ -164,7 +165,7 @@ brin_page_items(PG_FUNCTION_ARGS)
 
 	indexRel = index_open(indexRelid, AccessShareLock);
 
-	if (!IS_BRIN(indexRel))
+	if (!IS_INDEX(indexRel) || !IS_BRIN(indexRel))
 		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 e7759488c36..7062b7b423f 100644
--- a/contrib/pageinspect/gistfuncs.c
+++ b/contrib/pageinspect/gistfuncs.c
@@ -30,6 +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_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
 #define IS_GIST(r) ((r)->rd_rel->relam == GIST_AM_OID)
 
 
@@ -217,7 +218,7 @@ gist_page_items(PG_FUNCTION_ARGS)
 	/* Open the relation */
 	indexRel = index_open(indexRelid, AccessShareLock);
 
-	if (!IS_GIST(indexRel))
+	if (!IS_INDEX(indexRel) || !IS_GIST(indexRel))
 		ereport(ERROR,
 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
 				 errmsg("\"%s\" is not a %s index",
-- 
2.43.0



view thread (2+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Add IS_INDEX macro to brin and gist index
  In-Reply-To: <MEAPR01MB3031A889D4B3F610E9D2A3AFB68FA@MEAPR01MB3031.ausprd01.prod.outlook.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox