From aadf7db66ef5a8a723eb3362e2c8b460738f1107 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 19 Jul 2025 14:23:02 -0400
Subject: [PATCH] Mostly-cosmetic adjustments to
 estimate_multivariate_bucketsize().

The only practical effect of these changes is to avoid a useless
list_copy() operation when there is a single hashclause.  That's
never going to make any noticeable performance difference, but
the code is arguably clearer this way, especially if we take the
opportunity to add some comments so that readers don't have to
reverse-engineer the usage of these local variables.  Also add
some braces for better/more consistent style.

Author: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAHewXNnHBOO9NEa=NBDYOrwZL4oHu2NOcTYvqyNyWEswo8f5OQ@mail.gmail.com
---
 src/backend/utils/adt/selfuncs.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index ce6a626eba..17fbfa9b41 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -3798,18 +3798,25 @@ estimate_multivariate_bucketsize(PlannerInfo *root, RelOptInfo *inner,
 								 List *hashclauses,
 								 Selectivity *innerbucketsize)
 {
-	List	   *clauses = list_copy(hashclauses);
-	List	   *otherclauses = NIL;
-	double		ndistinct = 1.0;
+	List	   *clauses;
+	List	   *otherclauses;
+	double		ndistinct;
 
 	if (list_length(hashclauses) <= 1)
-
+	{
 		/*
 		 * Nothing to do for a single clause.  Could we employ univariate
 		 * extended stat here?
 		 */
 		return hashclauses;
+	}
 
+	/* "clauses" is the list of hashclauses we've not dealt with yet */
+	clauses = list_copy(hashclauses);
+	/* "otherclauses" holds clauses we are going to return to caller */
+	otherclauses = NIL;
+	/* current estimate of ndistinct */
+	ndistinct = 1.0;
 	while (clauses != NIL)
 	{
 		ListCell   *lc;
@@ -3874,12 +3881,13 @@ estimate_multivariate_bucketsize(PlannerInfo *root, RelOptInfo *inner,
 					group_rel = root->simple_rel_array[relid];
 				}
 				else if (group_relid != relid)
-
+				{
 					/*
 					 * Being in the group forming state we don't need other
 					 * clauses.
 					 */
 					continue;
+				}
 
 				/*
 				 * We're going to add the new clause to the varinfos list.  We
-- 
2.39.5

