From d1b9e3bddd6939bfe64b0f4b486fd2caa4d30786 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Mon, 17 Nov 2025 09:07:52 +0000
Subject: [PATCH v1 6/8] Use OidIsValid() in various places for edge cases

Let's use OidIsValid() instead of:

- direct comparisons with InvalidOid
- direct comparisons with literal 0
---
 contrib/btree_gin/btree_gin.c         | 2 +-
 src/backend/commands/tablecmds.c      | 2 +-
 src/backend/optimizer/path/indxpath.c | 2 +-
 src/backend/partitioning/partprune.c  | 2 +-
 src/backend/utils/cache/inval.c       | 4 ++--
 src/include/replication/slot.h        | 4 ++--
 6 files changed, 8 insertions(+), 8 deletions(-)
  10.5% contrib/btree_gin/
  13.3% src/backend/commands/
  15.7% src/backend/optimizer/path/
  12.6% src/backend/partitioning/
  17.4% src/backend/utils/cache/
  30.1% src/include/replication/

diff --git a/contrib/btree_gin/btree_gin.c b/contrib/btree_gin/btree_gin.c
index 8c477d17e22..03c49a61fe5 100644
--- a/contrib/btree_gin/btree_gin.c
+++ b/contrib/btree_gin/btree_gin.c
@@ -857,7 +857,7 @@ GIN_SUPPORT(numeric, leftmostvalue_numeric, numeric_rhs_is_varlena, NULL, numeri
  * routines it needs it, so we can't use DirectFunctionCall2.
  */
 
-#define ENUM_IS_LEFTMOST(x) ((x) == InvalidOid)
+#define ENUM_IS_LEFTMOST(x) !OidIsValid(x)
 
 PG_FUNCTION_INFO_V1(gin_enum_cmp);
 
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 0d335123b5b..f4e9490cd71 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -19223,7 +19223,7 @@ AlterSeqNamespaces(Relation classRel, Relation rel,
 		 * ever re-instate that, we'll need to move the pg_type entry to the
 		 * new namespace, too (using AlterTypeNamespaceInternal).
 		 */
-		Assert(RelationGetForm(seqRel)->reltype == InvalidOid);
+		Assert(!OidIsValid(RelationGetForm(seqRel)->reltype));
 
 		/* Now we can close it.  Keep the lock till end of transaction. */
 		relation_close(seqRel, NoLock);
diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index 62a6642222b..932ffa75745 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -40,7 +40,7 @@
 
 /* XXX see PartCollMatchesExprColl */
 #define IndexCollMatchesExprColl(idxcollation, exprcollation) \
-	((idxcollation) == InvalidOid || (idxcollation) == (exprcollation))
+	(!OidIsValid(idxcollation) || (idxcollation) == (exprcollation))
 
 /* Whether we are looking for plain indexscan, bitmap scan, or either */
 typedef enum
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c
index 6fff4034c24..ec9582b1b1d 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -1782,7 +1782,7 @@ gen_prune_steps_from_opexps(GeneratePruningStepsContext *context,
  * See also IndexCollMatchesExprColl.
  */
 #define PartCollMatchesExprColl(partcoll, exprcoll) \
-	((partcoll) == InvalidOid || (partcoll) == (exprcoll))
+	(!OidIsValid(partcoll) || (partcoll) == (exprcoll))
 
 /*
  * match_clause_to_partition_key
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c
index 25893fc0d78..4220a51c464 100644
--- a/src/backend/utils/cache/inval.c
+++ b/src/backend/utils/cache/inval.c
@@ -481,7 +481,7 @@ AddRelcacheInvalidationMessage(InvalidationMsgsGroup *group,
 	ProcessMessageSubGroup(group, RelCacheMsgs,
 						   if (msg->rc.id == SHAREDINVALRELCACHE_ID &&
 							   (msg->rc.relId == relId ||
-								msg->rc.relId == InvalidOid))
+								!OidIsValid(msg->rc.relId)))
 						   return);
 
 	/* OK, add the item */
@@ -511,7 +511,7 @@ AddRelsyncInvalidationMessage(InvalidationMsgsGroup *group,
 	ProcessMessageSubGroup(group, RelCacheMsgs,
 						   if (msg->rc.id == SHAREDINVALRELSYNC_ID &&
 							   (msg->rc.relId == relId ||
-								msg->rc.relId == InvalidOid))
+								!OidIsValid(msg->rc.relId)))
 						   return);
 
 	/* OK, add the item */
diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h
index 09c69f83d57..01b1c5097a4 100644
--- a/src/include/replication/slot.h
+++ b/src/include/replication/slot.h
@@ -251,8 +251,8 @@ typedef struct ReplicationSlot
 
 } ReplicationSlot;
 
-#define SlotIsPhysical(slot) ((slot)->data.database == InvalidOid)
-#define SlotIsLogical(slot) ((slot)->data.database != InvalidOid)
+#define SlotIsPhysical(slot) (!OidIsValid((slot)->data.database))
+#define SlotIsLogical(slot) (OidIsValid((slot)->data.database))
 
 /*
  * Shared memory control area for all of replication slots.
-- 
2.34.1

