From 5541f1773ce229f08da3c6a44c2cc3e524e99076 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Tue, 2 Dec 2025 11:51:01 +0800
Subject: [PATCH v8 02/12] cleanup: rename outer variables to avoid shadowing
 inner locals

This commit resolves several cases where an outer-scope variable shared
a name with a more frequently used inner variable. The fixes rename the
outer variables so each identifier remains distinct within its scope.

Author: Chao Li <lic@highgo.com>
Reviewed-by: Peter Smith <smithpb2250@gmail.com>
Discussion: https://postgr.es/m/CAEoWx2kQ2x5gMaj8tHLJ3=jfC+p5YXHkJyHrDTiQw2nn2FJTmQ@mail.gmail.com
---
 src/backend/access/brin/brin.c          | 10 +++++++---
 src/backend/catalog/objectaddress.c     | 10 +++++-----
 src/backend/catalog/pg_constraint.c     |  4 ++--
 src/backend/optimizer/path/equivclass.c |  6 +++---
 src/backend/partitioning/partdesc.c     |  6 +++---
 src/backend/statistics/extended_stats.c |  6 +++---
 src/backend/storage/aio/read_stream.c   |  6 +++---
 src/bin/pg_basebackup/pg_receivewal.c   |  6 +++---
 src/include/catalog/pg_constraint.h     |  2 +-
 src/include/optimizer/paths.h           |  2 +-
 src/include/storage/read_stream.h       |  2 +-
 11 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index bdb30752e09..8e67c7c923d 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -591,7 +591,9 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
 			   *nnullkeys;
 	char	   *ptr;
 	Size		len;
-	char	   *tmp PG_USED_FOR_ASSERTS_ONLY;
+#if defined(USE_ASSERT_CHECKING)
+	char	   *savePtr PG_USED_FOR_ASSERTS_ONLY;
+#endif
 
 	opaque = (BrinOpaque *) scan->opaque;
 	bdesc = opaque->bo_bdesc;
@@ -642,7 +644,9 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
 		MAXALIGN(sizeof(int) * bdesc->bd_tupdesc->natts);
 
 	ptr = palloc(len);
-	tmp = ptr;
+#if defined(USE_ASSERT_CHECKING)
+	savePtr = ptr;
+#endif
 
 	keys = (ScanKey **) ptr;
 	ptr += MAXALIGN(sizeof(ScanKey *) * bdesc->bd_tupdesc->natts);
@@ -665,7 +669,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
 		ptr += MAXALIGN(sizeof(ScanKey) * scan->numberOfKeys);
 	}
 
-	Assert(tmp + len == ptr);
+	Assert(savePtr + len == ptr);
 
 	/* zero the number of keys */
 	memset(nkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts);
diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c
index c1862809577..3eb5b82e8ae 100644
--- a/src/backend/catalog/objectaddress.c
+++ b/src/backend/catalog/objectaddress.c
@@ -2215,7 +2215,7 @@ pg_get_object_address(PG_FUNCTION_ARGS)
 	ObjectAddress addr;
 	TupleDesc	tupdesc;
 	Datum		values[3];
-	bool		nulls[3];
+	bool		isnulls[3];
 	HeapTuple	htup;
 	Relation	relation;
 
@@ -2471,11 +2471,11 @@ pg_get_object_address(PG_FUNCTION_ARGS)
 	values[0] = ObjectIdGetDatum(addr.classId);
 	values[1] = ObjectIdGetDatum(addr.objectId);
 	values[2] = Int32GetDatum(addr.objectSubId);
-	nulls[0] = false;
-	nulls[1] = false;
-	nulls[2] = false;
+	isnulls[0] = false;
+	isnulls[1] = false;
+	isnulls[2] = false;
 
-	htup = heap_form_tuple(tupdesc, values, nulls);
+	htup = heap_form_tuple(tupdesc, values, isnulls);
 
 	PG_RETURN_DATUM(HeapTupleGetDatum(htup));
 }
diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c
index b12765ae691..48171c0f7d5 100644
--- a/src/backend/catalog/pg_constraint.c
+++ b/src/backend/catalog/pg_constraint.c
@@ -831,7 +831,7 @@ AdjustNotNullInheritance(Oid relid, AttrNumber attnum, const char *new_conname,
  * 'include_noinh' determines whether to include NO INHERIT constraints or not.
  */
 List *
-RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh)
+RelationGetNotNullConstraints(Oid relid, bool want_cooked, bool include_noinh)
 {
 	List	   *notnulls = NIL;
 	Relation	constrRel;
@@ -859,7 +859,7 @@ RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh)
 
 		colnum = extractNotNullColumn(htup);
 
-		if (cooked)
+		if (want_cooked)
 		{
 			CookedConstraint *cooked;
 
diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/optimizer/path/equivclass.c
index e3697df51a2..d273288c28f 100644
--- a/src/backend/optimizer/path/equivclass.c
+++ b/src/backend/optimizer/path/equivclass.c
@@ -739,7 +739,7 @@ get_eclass_for_sort_expr(PlannerInfo *root,
 						 Oid opcintype,
 						 Oid collation,
 						 Index sortref,
-						 Relids rel,
+						 Relids relids,
 						 bool create_it)
 {
 	JoinDomain *jdomain;
@@ -782,14 +782,14 @@ get_eclass_for_sort_expr(PlannerInfo *root,
 		if (!equal(opfamilies, cur_ec->ec_opfamilies))
 			continue;
 
-		setup_eclass_member_iterator(&it, cur_ec, rel);
+		setup_eclass_member_iterator(&it, cur_ec, relids);
 		while ((cur_em = eclass_member_iterator_next(&it)) != NULL)
 		{
 			/*
 			 * Ignore child members unless they match the request.
 			 */
 			if (cur_em->em_is_child &&
-				!bms_equal(cur_em->em_relids, rel))
+				!bms_equal(cur_em->em_relids, relids))
 				continue;
 
 			/*
diff --git a/src/backend/partitioning/partdesc.c b/src/backend/partitioning/partdesc.c
index c3d275f8726..44297278756 100644
--- a/src/backend/partitioning/partdesc.c
+++ b/src/backend/partitioning/partdesc.c
@@ -146,7 +146,7 @@ RelationBuildPartitionDesc(Relation rel, bool omit_detached)
 	int			i,
 				nparts;
 	bool		retried = false;
-	PartitionKey key = RelationGetPartitionKey(rel);
+	PartitionKey partkey = RelationGetPartitionKey(rel);
 	MemoryContext new_pdcxt;
 	MemoryContext oldcxt;
 	int		   *mapping;
@@ -308,7 +308,7 @@ retry:
 	 * This could fail, but we haven't done any damage if so.
 	 */
 	if (nparts > 0)
-		boundinfo = partition_bounds_create(boundspecs, nparts, key, &mapping);
+		boundinfo = partition_bounds_create(boundspecs, nparts, partkey, &mapping);
 
 	/*
 	 * Now build the actual relcache partition descriptor, copying all the
@@ -329,7 +329,7 @@ retry:
 	if (nparts > 0)
 	{
 		oldcxt = MemoryContextSwitchTo(new_pdcxt);
-		partdesc->boundinfo = partition_bounds_copy(boundinfo, key);
+		partdesc->boundinfo = partition_bounds_copy(boundinfo, partkey);
 
 		/* Initialize caching fields for speeding up ExecFindPartition */
 		partdesc->last_found_datum_index = -1;
diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c
index 2b83355d26e..a6990db04be 100644
--- a/src/backend/statistics/extended_stats.c
+++ b/src/backend/statistics/extended_stats.c
@@ -1040,7 +1040,7 @@ build_sorted_items(StatsBuildData *data, int *nitems,
 	Size		len;
 	SortItem   *items;
 	Datum	   *values;
-	bool	   *isnull;
+	bool	   *isnulls;
 	char	   *ptr;
 	int		   *typlen;
 
@@ -1060,7 +1060,7 @@ build_sorted_items(StatsBuildData *data, int *nitems,
 	values = (Datum *) ptr;
 	ptr += nvalues * sizeof(Datum);
 
-	isnull = (bool *) ptr;
+	isnulls = (bool *) ptr;
 	ptr += nvalues * sizeof(bool);
 
 	/* make sure we consumed the whole buffer exactly */
@@ -1071,7 +1071,7 @@ build_sorted_items(StatsBuildData *data, int *nitems,
 	for (i = 0; i < data->numrows; i++)
 	{
 		items[nrows].values = &values[nrows * numattrs];
-		items[nrows].isnull = &isnull[nrows * numattrs];
+		items[nrows].isnull = &isnulls[nrows * numattrs];
 
 		nrows++;
 	}
diff --git a/src/backend/storage/aio/read_stream.c b/src/backend/storage/aio/read_stream.c
index 2374b4cd507..5e26fb64afe 100644
--- a/src/backend/storage/aio/read_stream.c
+++ b/src/backend/storage/aio/read_stream.c
@@ -1017,7 +1017,7 @@ read_stream_begin_smgr_relation(int flags,
  * the stream early at any time by calling read_stream_end().
  */
 Buffer
-read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
+read_stream_next_buffer(ReadStream *stream, void **pper_buffer_data)
 {
 	Buffer		buffer;
 	int16		oldest_buffer_index;
@@ -1162,8 +1162,8 @@ read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
 	Assert(oldest_buffer_index >= 0 &&
 		   oldest_buffer_index < stream->queue_size);
 	buffer = stream->buffers[oldest_buffer_index];
-	if (per_buffer_data)
-		*per_buffer_data = get_per_buffer_data(stream, oldest_buffer_index);
+	if (pper_buffer_data)
+		*pper_buffer_data = get_per_buffer_data(stream, oldest_buffer_index);
 
 	Assert(BufferIsValid(buffer));
 
diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c
index ddfec298fb7..681cdb5f3f1 100644
--- a/src/bin/pg_basebackup/pg_receivewal.c
+++ b/src/bin/pg_basebackup/pg_receivewal.c
@@ -59,7 +59,7 @@ static XLogRecPtr endpos = InvalidXLogRecPtr;
 static void usage(void);
 static DIR *get_destination_dir(char *dest_folder);
 static void close_destination_dir(DIR *dest_dir, char *dest_folder);
-static XLogRecPtr FindStreamingStart(uint32 *tli);
+static XLogRecPtr FindStreamingStart(uint32 *ptli);
 static void StreamLog(void);
 static bool stop_streaming(XLogRecPtr xlogpos, uint32 timeline,
 						   bool segment_finished);
@@ -265,7 +265,7 @@ close_destination_dir(DIR *dest_dir, char *dest_folder)
  * If there are no WAL files in the directory, returns InvalidXLogRecPtr.
  */
 static XLogRecPtr
-FindStreamingStart(uint32 *tli)
+FindStreamingStart(uint32 *ptli)
 {
 	DIR		   *dir;
 	struct dirent *dirent;
@@ -486,7 +486,7 @@ FindStreamingStart(uint32 *tli)
 
 		XLogSegNoOffsetToRecPtr(high_segno, 0, WalSegSz, high_ptr);
 
-		*tli = high_tli;
+		*ptli = high_tli;
 		return high_ptr;
 	}
 	else
diff --git a/src/include/catalog/pg_constraint.h b/src/include/catalog/pg_constraint.h
index 1b7fedf1750..51ccda7fe52 100644
--- a/src/include/catalog/pg_constraint.h
+++ b/src/include/catalog/pg_constraint.h
@@ -269,7 +269,7 @@ extern HeapTuple findDomainNotNullConstraint(Oid typid);
 extern AttrNumber extractNotNullColumn(HeapTuple constrTup);
 extern bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, const char *new_conname,
 									 bool is_local, bool is_no_inherit, bool is_notvalid);
-extern List *RelationGetNotNullConstraints(Oid relid, bool cooked,
+extern List *RelationGetNotNullConstraints(Oid relid, bool want_cooked,
 										   bool include_noinh);
 
 extern void RemoveConstraintById(Oid conId);
diff --git a/src/include/optimizer/paths.h b/src/include/optimizer/paths.h
index 17f2099ec3b..d8eceb66610 100644
--- a/src/include/optimizer/paths.h
+++ b/src/include/optimizer/paths.h
@@ -144,7 +144,7 @@ extern EquivalenceClass *get_eclass_for_sort_expr(PlannerInfo *root,
 												  Oid opcintype,
 												  Oid collation,
 												  Index sortref,
-												  Relids rel,
+												  Relids relids,
 												  bool create_it);
 extern EquivalenceMember *find_ec_member_matching_expr(EquivalenceClass *ec,
 													   Expr *expr,
diff --git a/src/include/storage/read_stream.h b/src/include/storage/read_stream.h
index 48995c6d534..f89b16bca5a 100644
--- a/src/include/storage/read_stream.h
+++ b/src/include/storage/read_stream.h
@@ -89,7 +89,7 @@ extern ReadStream *read_stream_begin_relation(int flags,
 											  ReadStreamBlockNumberCB callback,
 											  void *callback_private_data,
 											  size_t per_buffer_data_size);
-extern Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data);
+extern Buffer read_stream_next_buffer(ReadStream *stream, void **pper_buffer_data);
 extern BlockNumber read_stream_next_block(ReadStream *stream,
 										  BufferAccessStrategy *strategy);
 extern ReadStream *read_stream_begin_smgr_relation(int flags,
-- 
2.50.1 (Apple Git-155)

