From ae0b5af8c1fb417e3a80cc05d32880fd7273f998 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Sun, 16 Nov 2025 23:54:15 +0800
Subject: [PATCH v2 1/2] refactor 0 to InvalidAttrNumber for
 pg_partitioned_table.partattrs

discussion: https://postgr.es/m/CACJufxGmsphWX150CxMU6KSed-x2fmTH3UpCwHpedGmjV1Biug@mail.gmail.com
---
 src/backend/catalog/heap.c            | 2 +-
 src/backend/commands/indexcmds.c      | 2 +-
 src/backend/commands/tablecmds.c      | 2 +-
 src/backend/executor/execPartition.c  | 2 +-
 src/backend/optimizer/util/plancat.c  | 2 +-
 src/backend/parser/parse_utilcmd.c    | 4 ++--
 src/backend/partitioning/partbounds.c | 8 ++++----
 src/backend/utils/adt/ruleutils.c     | 2 +-
 src/backend/utils/cache/partcache.c   | 2 +-
 src/include/utils/partcache.h         | 2 +-
 10 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index fd6537567ea..23198161cbb 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -3981,7 +3981,7 @@ StorePartitionKey(Relation rel,
 	 */
 	for (i = 0; i < partnatts; i++)
 	{
-		if (partattrs[i] == 0)
+		if (!AttributeNumberIsValid(partattrs[i]))
 			continue;			/* ignore expressions here */
 
 		ObjectAddressSubSet(referenced, RelationRelationId,
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 5712fac3697..221c7d1d607 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -1012,7 +1012,7 @@ DefineIndex(Oid tableId,
 			 * It may be possible to support UNIQUE constraints when partition
 			 * keys are expressions, but is it worth it?  Give up for now.
 			 */
-			if (key->partattrs[i] == 0)
+			if (!AttributeNumberIsValid(key->partattrs[i]))
 				ereport(ERROR,
 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 						 errmsg("unsupported %s constraint with partition key definition",
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 23ebaa3f230..c0d743a1085 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -19920,7 +19920,7 @@ ComputePartitionAttrs(ParseState *pstate, Relation rel, List *partParams, AttrNu
 			}
 			else
 			{
-				partattrs[attn] = 0;	/* marks the column as expression */
+				partattrs[attn] = InvalidAttrNumber;	/* marks the column as expression */
 				*partexprs = lappend(*partexprs, expr);
 
 				/*
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index aa12e9ad2ea..6272fb5f214 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -1325,7 +1325,7 @@ FormPartitionKeyDatum(PartitionDispatch pd,
 		Datum		datum;
 		bool		isNull;
 
-		if (keycol != 0)
+		if (AttributeNumberIsValid(keycol))
 		{
 			/* Plain column; get the value directly from the heap tuple */
 			datum = slot_getattr(slot, keycol, &isNull);
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index d950bd93002..93a248c0459 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -2699,7 +2699,7 @@ set_baserel_partition_key_exprs(Relation relation,
 		Expr	   *partexpr;
 		AttrNumber	attno = partkey->partattrs[cnt];
 
-		if (attno != InvalidAttrNumber)
+		if (AttributeNumberIsValid(attno))
 		{
 			/* Single column partition key is stored as a Var node. */
 			Assert(attno > 0);
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index e96b38a59d5..a67761e81d3 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -4345,7 +4345,7 @@ transformPartitionBound(ParseState *pstate, Relation parent,
 					 parser_errposition(pstate, exprLocation((Node *) spec))));
 
 		/* Get the only column's name in case we need to output an error */
-		if (key->partattrs[0] != 0)
+		if (AttributeNumberIsValid(key->partattrs[0]))
 			colname = get_attname(RelationGetRelid(parent),
 								  key->partattrs[0], false);
 		else
@@ -4494,7 +4494,7 @@ transformPartitionRangeBounds(ParseState *pstate, List *blist,
 			Const	   *value;
 
 			/* Get the column's name in case we need to output an error */
-			if (key->partattrs[i] != 0)
+			if (AttributeNumberIsValid(key->partattrs[i]))
 				colname = get_attname(RelationGetRelid(parent),
 									  key->partattrs[i], false);
 			else
diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c
index 8ba038c5ef4..e09cd1c1a95 100644
--- a/src/backend/partitioning/partbounds.c
+++ b/src/backend/partitioning/partbounds.c
@@ -4026,7 +4026,7 @@ get_qual_for_hash(Relation parent, PartitionBoundSpec *spec)
 		Node	   *keyCol;
 
 		/* Left operand */
-		if (key->partattrs[i] != 0)
+		if (AttributeNumberIsValid(key->partattrs[i]))
 		{
 			keyCol = (Node *) makeVar(1,
 									  key->partattrs[i],
@@ -4082,7 +4082,7 @@ get_qual_for_list(Relation parent, PartitionBoundSpec *spec)
 	Assert(key->partnatts == 1);
 
 	/* Construct Var or expression representing the partition column */
-	if (key->partattrs[0] != 0)
+	if (AttributeNumberIsValid(key->partattrs[0]))
 		keyCol = (Expr *) makeVar(1,
 								  key->partattrs[0],
 								  key->parttypid[0],
@@ -4638,7 +4638,7 @@ get_range_key_properties(PartitionKey key, int keynum,
 						 Const **lower_val, Const **upper_val)
 {
 	/* Get partition key expression for this column */
-	if (key->partattrs[keynum] != 0)
+	if (AttributeNumberIsValid(key->partattrs[keynum]))
 	{
 		*keyCol = (Expr *) makeVar(1,
 								   key->partattrs[keynum],
@@ -4686,7 +4686,7 @@ get_range_nulltest(PartitionKey key)
 	{
 		Expr	   *keyCol;
 
-		if (key->partattrs[i] != 0)
+		if (AttributeNumberIsValid(key->partattrs[i]))
 		{
 			keyCol = (Expr *) makeVar(1,
 									  key->partattrs[i],
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 556ab057e5a..c4e0193d317 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -2031,7 +2031,7 @@ pg_get_partkeydef_worker(Oid relid, int prettyFlags,
 
 		appendStringInfoString(&buf, sep);
 		sep = ", ";
-		if (attnum != 0)
+		if (AttributeNumberIsValid(attnum))
 		{
 			/* Simple attribute reference */
 			char	   *attname;
diff --git a/src/backend/utils/cache/partcache.c b/src/backend/utils/cache/partcache.c
index f5d7d70def0..8bcf77d55a5 100644
--- a/src/backend/utils/cache/partcache.c
+++ b/src/backend/utils/cache/partcache.c
@@ -226,7 +226,7 @@ RelationBuildPartitionKey(Relation relation)
 		key->partcollation[i] = collation->values[i];
 
 		/* Collect type information */
-		if (attno != 0)
+		if (AttributeNumberIsValid(attno))
 		{
 			Form_pg_attribute att = TupleDescAttr(relation->rd_att, attno - 1);
 
diff --git a/src/include/utils/partcache.h b/src/include/utils/partcache.h
index 0fb6fc1c808..8a9d07c03c8 100644
--- a/src/include/utils/partcache.h
+++ b/src/include/utils/partcache.h
@@ -27,7 +27,7 @@ typedef struct PartitionKeyData
 	PartitionStrategy strategy; /* partitioning strategy */
 	int16		partnatts;		/* number of columns in the partition key */
 	AttrNumber *partattrs;		/* attribute numbers of columns in the
-								 * partition key or 0 if it's an expr */
+								 * partition key or InvalidAttrNumber (0) if it's an expr */
 	List	   *partexprs;		/* list of expressions in the partitioning
 								 * key, one for each zero-valued partattrs */
 
-- 
2.34.1

