From de3c83283487b6d219d960ffe5d2f49e068051d5 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Mon, 17 Jun 2024 15:56:25 +0200
Subject: [PATCH v20240617 23/56] Simplify code by using list_cell_number

instead of maintaining it manually.

and remove the below lines from statext_clauselist_join_selectivity.

	if (!clauses)
		return 1.0;

since it has been handled in clauselist_selectivity_ext.
---
 src/backend/statistics/extended_stats.c | 55 +++++++++----------------
 1 file changed, 19 insertions(+), 36 deletions(-)

diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c
index 070a362b44e..ab5b8d9a0d4 100644
--- a/src/backend/statistics/extended_stats.c
+++ b/src/backend/statistics/extended_stats.c
@@ -2852,15 +2852,6 @@ statext_is_supported_join_clause(PlannerInfo *root, Node *clause)
 	int			left_relid,
 				right_relid;
 
-	/*
-	 * XXX isn't this comment stale after removal of varRelid?
-	 *
-	 * evaluation as a restriction clause, either at scan node or forced
-	 *
-	 * XXX See treat_as_join_clause.
-	 */
-
-	/* XXX Can we rely on always getting RestrictInfo here? */
 	if (!IsA(clause, RestrictInfo))
 		return false;
 
@@ -2908,6 +2899,12 @@ statext_is_supported_join_clause(PlannerInfo *root, Node *clause)
 		!bms_get_singleton_member(rinfo->right_relids, &right_relid))
 		return false;
 
+	/*
+	 * XXX:
+	 * Join two columns in the same relation is uncommon and
+	 * extract_relation_info requires 2 different relids, so no bother to
+	 * handle it.
+	 */
 	if (left_relid == right_relid)
 		return false;
 
@@ -2927,7 +2924,6 @@ bool
 statext_try_join_estimates(PlannerInfo *root, List *clauses, int varRelid,
 						   JoinType jointype, SpecialJoinInfo *sjinfo)
 {
-	int			listidx;
 	int			k;
 	ListCell   *lc;
 	Bitmapset  *relids = NULL;
@@ -2955,15 +2951,11 @@ statext_try_join_estimates(PlannerInfo *root, List *clauses, int varRelid,
 	 * about the part not represented by MCV, which is now based on ndistinct
 	 * estimates.
 	 */
-	listidx = -1;
-	foreach(lc, clauses)
+	foreach (lc, clauses)
 	{
 		Node	   *clause = (Node *) lfirst(lc);
 		RestrictInfo *rinfo;
 
-		/* needs to happen before skipping any clauses */
-		listidx++;
-
 		/*
 		 * Skip clauses that are not join clauses or that we don't know how to
 		 * handle estimate using extended statistics.
@@ -3043,10 +3035,9 @@ static JoinPairInfo *
 statext_build_join_pairs(PlannerInfo *root, List *clauses,
 						 Bitmapset *estimatedclauses, int *npairs)
 {
-	int			cnt;
-	int			listidx;
-	JoinPairInfo *info;
-	ListCell   *lc;
+	int				cnt;
+	JoinPairInfo   *info;
+	ListCell	   *lc;
 
 	/*
 	 * Assume each clause is for a different pair of relations (some of them
@@ -3056,15 +3047,13 @@ statext_build_join_pairs(PlannerInfo *root, List *clauses,
 	info = (JoinPairInfo *) palloc0(sizeof(JoinPairInfo) * list_length(clauses));
 	cnt = 0;
 
-	listidx = -1;
 	foreach(lc, clauses)
 	{
-		int			i;
-		bool		found;
-		Node	   *clause = (Node *) lfirst(lc);
-		RestrictInfo *rinfo;
-
-		listidx++;
+		int				i;
+		bool			found;
+		Node		   *clause = (Node *) lfirst(lc);
+		RestrictInfo   *rinfo;
+		int				listidx = list_cell_number(clauses, lc);
 
 		/* skip already estimated clauses */
 		if (bms_is_member(listidx, estimatedclauses))
@@ -3276,15 +3265,11 @@ statext_clauselist_join_selectivity(PlannerInfo *root, List *clauses,
 									Bitmapset **estimatedclauses)
 {
 	int			i;
-	int			listidx;
-	Selectivity s = 1.0;
+	Selectivity	s = 1.0;
 
 	JoinPairInfo *info;
 	int			ninfo;
 
-	if (!clauses)
-		return 1.0;
-
 	/* extract pairs of joined relations from the list of clauses */
 	info = statext_build_join_pairs(root, clauses,
 									*estimatedclauses, &ninfo);
@@ -3461,12 +3446,10 @@ statext_clauselist_join_selectivity(PlannerInfo *root, List *clauses,
 			Node	   *clause = (Node *) rinfo->clause;
 			ListCell   *lc2;
 
-			listidx = -1;
-			foreach(lc2, clauses)
+			foreach (lc2, clauses)
 			{
-				Node	   *clause2 = (Node *) lfirst(lc2);
-
-				listidx++;
+				Node *clause2 = (Node *) lfirst(lc2);
+				int listidx = list_cell_number(clauses, lc2);
 
 				Assert(IsA(clause2, RestrictInfo));
 
-- 
2.45.2

