From 18f69a7269a32206ba6fea58508ae30e49605340 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Sat, 8 Nov 2025 03:35:35 +0000
Subject: [PATCH v1 1/8] Use RegProcedureIsValid() in various places

Let's use RegProcedureIsValid() instead of:

- direct comparisons with InvalidOid
- direct comparisons with literal 0

This makes the code more consistent (same idea as a2b02293bc6).
---
 src/backend/access/gin/ginutil.c     | 8 ++++----
 src/backend/optimizer/util/plancat.c | 6 +++---
 src/backend/utils/adt/regproc.c      | 4 ++--
 src/backend/utils/adt/selfuncs.c     | 2 +-
 4 files changed, 10 insertions(+), 10 deletions(-)
  65.1% src/backend/access/gin/
  16.8% src/backend/optimizer/util/
  17.9% src/backend/utils/adt/

diff --git a/src/backend/access/gin/ginutil.c b/src/backend/access/gin/ginutil.c
index 78f7b7a2495..49631200a5a 100644
--- a/src/backend/access/gin/ginutil.c
+++ b/src/backend/access/gin/ginutil.c
@@ -134,7 +134,7 @@ initGinState(GinState *state, Relation index)
 		 * If the compare proc isn't specified in the opclass definition, look
 		 * up the index key type's default btree comparator.
 		 */
-		if (index_getprocid(index, i + 1, GIN_COMPARE_PROC) != InvalidOid)
+		if (RegProcedureIsValid(index_getprocid(index, i + 1, GIN_COMPARE_PROC)))
 		{
 			fmgr_info_copy(&(state->compareFn[i]),
 						   index_getprocinfo(index, i + 1, GIN_COMPARE_PROC),
@@ -168,14 +168,14 @@ initGinState(GinState *state, Relation index)
 		 * Check opclass capability to do tri-state or binary logic consistent
 		 * check.
 		 */
-		if (index_getprocid(index, i + 1, GIN_TRICONSISTENT_PROC) != InvalidOid)
+		if (RegProcedureIsValid(index_getprocid(index, i + 1, GIN_TRICONSISTENT_PROC)))
 		{
 			fmgr_info_copy(&(state->triConsistentFn[i]),
 						   index_getprocinfo(index, i + 1, GIN_TRICONSISTENT_PROC),
 						   CurrentMemoryContext);
 		}
 
-		if (index_getprocid(index, i + 1, GIN_CONSISTENT_PROC) != InvalidOid)
+		if (RegProcedureIsValid(index_getprocid(index, i + 1, GIN_CONSISTENT_PROC)))
 		{
 			fmgr_info_copy(&(state->consistentFn[i]),
 						   index_getprocinfo(index, i + 1, GIN_CONSISTENT_PROC),
@@ -193,7 +193,7 @@ initGinState(GinState *state, Relation index)
 		/*
 		 * Check opclass capability to do partial match.
 		 */
-		if (index_getprocid(index, i + 1, GIN_COMPARE_PARTIAL_PROC) != InvalidOid)
+		if (RegProcedureIsValid(index_getprocid(index, i + 1, GIN_COMPARE_PARTIAL_PROC)))
 		{
 			fmgr_info_copy(&(state->comparePartialFn[i]),
 						   index_getprocinfo(index, i + 1, GIN_COMPARE_PARTIAL_PROC),
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index d950bd93002..a01f781bca8 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -2088,7 +2088,7 @@ restriction_selectivity(PlannerInfo *root,
 	 * if the oprrest procedure is missing for whatever reason, use a
 	 * selectivity of 0.5
 	 */
-	if (!oprrest)
+	if (!RegProcedureIsValid(oprrest))
 		return (Selectivity) 0.5;
 
 	result = DatumGetFloat8(OidFunctionCall4Coll(oprrest,
@@ -2128,7 +2128,7 @@ join_selectivity(PlannerInfo *root,
 	 * if the oprjoin procedure is missing for whatever reason, use a
 	 * selectivity of 0.5
 	 */
-	if (!oprjoin)
+	if (!RegProcedureIsValid(oprjoin))
 		return (Selectivity) 0.5;
 
 	result = DatumGetFloat8(OidFunctionCall5Coll(oprjoin,
@@ -2167,7 +2167,7 @@ function_selectivity(PlannerInfo *root,
 	SupportRequestSelectivity req;
 	SupportRequestSelectivity *sresult;
 
-	if (!prosupport)
+	if (!RegProcedureIsValid(prosupport))
 		return (Selectivity) -1;	/* no support function */
 
 	req.type = T_SupportRequestSelectivity;
diff --git a/src/backend/utils/adt/regproc.c b/src/backend/utils/adt/regproc.c
index e5c2246f2c9..7ce61efd947 100644
--- a/src/backend/utils/adt/regproc.c
+++ b/src/backend/utils/adt/regproc.c
@@ -142,7 +142,7 @@ regprocout(PG_FUNCTION_ARGS)
 	char	   *result;
 	HeapTuple	proctup;
 
-	if (proid == InvalidOid)
+	if (!RegProcedureIsValid(proid))
 	{
 		result = pstrdup("-");
 		PG_RETURN_CSTRING(result);
@@ -443,7 +443,7 @@ regprocedureout(PG_FUNCTION_ARGS)
 	RegProcedure proid = PG_GETARG_OID(0);
 	char	   *result;
 
-	if (proid == InvalidOid)
+	if (!RegProcedureIsValid(proid))
 		result = pstrdup("-");
 	else
 		result = format_procedure(proid);
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index cb23ad52782..911376f79e8 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -1904,7 +1904,7 @@ scalararraysel(PlannerInfo *root,
 		oprsel = get_oprjoin(operator);
 	else
 		oprsel = get_oprrest(operator);
-	if (!oprsel)
+	if (!RegProcedureIsValid(oprsel))
 		return (Selectivity) 0.5;
 	fmgr_info(oprsel, &oprselproc);
 
-- 
2.34.1

