hi.

if PartitionKeyData->partattrs is 0, then it means that partition key is
expression, else it's column reference.

we can change from
if (key->partattrs[i] == 0)
to
if (key->partattrs[i] == InvalidAttrNumber)

the reason I can think of is to improve grepability.
numeric value 0 is too common to reliably locate relevant code.

what do you think?

--
jian
https://www.enterprisedb.com/
From 940c0290fe68f4fb3701f7222f83316e416fd4f0 Mon Sep 17 00:00:00 2001
From: jian he <[email protected]>
Date: Fri, 14 Nov 2025 23:44:21 +0800
Subject: [PATCH v1 1/1] partattrs 0 refactor to InvalidAttrNumber

The main reason I can think of is to improve grepability.
the numeric value 0 is too common to reliably locate relevant code.

discussion: https://postgr.es/m/
---
 src/backend/catalog/heap.c            | 2 +-
 src/backend/commands/indexcmds.c      | 2 +-
 src/backend/commands/tablecmds.c      | 2 +-
 src/backend/parser/parse_utilcmd.c    | 4 ++--
 src/backend/partitioning/partbounds.c | 8 ++++----
 src/include/utils/partcache.h         | 2 +-
 6 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index fd6537567ea..6c7bdef5600 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 (partattrs[i] == InvalidAttrNumber)
 			continue;			/* ignore expressions here */
 
 		ObjectAddressSubSet(referenced, RelationRelationId,
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 5712fac3697..665ed5a690a 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 (key->partattrs[i] == InvalidAttrNumber)
 				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/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index e96b38a59d5..439061a2a0b 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 (key->partattrs[0] != InvalidAttrNumber)
 			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 (key->partattrs[i] != InvalidAttrNumber)
 				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..77f326fb63f 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 (key->partattrs[i] != InvalidAttrNumber)
 		{
 			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 (key->partattrs[0] != InvalidAttrNumber)
 		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 (key->partattrs[keynum] != InvalidAttrNumber)
 	{
 		*keyCol = (Expr *) makeVar(1,
 								   key->partattrs[keynum],
@@ -4686,7 +4686,7 @@ get_range_nulltest(PartitionKey key)
 	{
 		Expr	   *keyCol;
 
-		if (key->partattrs[i] != 0)
+		if (key->partattrs[i] != InvalidAttrNumber)
 		{
 			keyCol = (Expr *) makeVar(1,
 									  key->partattrs[i],
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

Reply via email to