From f3bfc5fed5c7aec084ca3394a073ebbfdce69459 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Thu, 12 Nov 2020 03:09:21 +0100
Subject: [PATCH 3/4] WIP: pass flag

---
 src/backend/optimizer/path/clausesel.c  | 20 +++++++++++++-------
 src/backend/statistics/extended_stats.c |  6 ++++--
 src/include/optimizer/optimizer.h       |  6 ++++--
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/backend/optimizer/path/clausesel.c b/src/backend/optimizer/path/clausesel.c
index 215c5db9fd..360f5c6512 100644
--- a/src/backend/optimizer/path/clausesel.c
+++ b/src/backend/optimizer/path/clausesel.c
@@ -129,7 +129,8 @@ clauselist_selectivity_internal(PlannerInfo *root,
 	 */
 	return s1 * clauselist_selectivity_simple(root, clauses, varRelid,
 											  jointype, sjinfo,
-											  estimatedclauses);
+											  estimatedclauses,
+											  use_extended_stats);
 }
 
 /*
@@ -186,7 +187,8 @@ clauselist_selectivity_or(PlannerInfo *root,
 	/* Estimate the rest of the clauses as if they were independent. */
 	return clauselist_selectivity_simple_or(root, clauses, varRelid,
 											jointype, sjinfo,
-											estimatedclauses, s);
+											estimatedclauses, s,
+											use_extended_stats);
 }
 
 /*
@@ -237,7 +239,8 @@ clauselist_selectivity_simple(PlannerInfo *root,
 							  int varRelid,
 							  JoinType jointype,
 							  SpecialJoinInfo *sjinfo,
-							  Bitmapset *estimatedclauses)
+							  Bitmapset *estimatedclauses,
+							  bool use_extended_stats)
 {
 	Selectivity s1 = 1.0;
 	RangeQueryClause *rqlist = NULL;
@@ -251,7 +254,8 @@ clauselist_selectivity_simple(PlannerInfo *root,
 	 */
 	if (list_length(clauses) == 1 && bms_is_empty(estimatedclauses))
 		return clause_selectivity_internal(root, (Node *) linitial(clauses),
-										   varRelid, jointype, sjinfo, false);
+										   varRelid, jointype, sjinfo,
+										   use_extended_stats);
 
 	/*
 	 * Anything that doesn't look like a potential rangequery clause gets
@@ -276,7 +280,8 @@ clauselist_selectivity_simple(PlannerInfo *root,
 
 		/* Always compute the selectivity using clause_selectivity */
 		s2 = clause_selectivity_internal(root, clause, varRelid,
-										 jointype, sjinfo, false);
+										 jointype, sjinfo,
+										 use_extended_stats);
 
 		/*
 		 * Check for being passed a RestrictInfo.
@@ -459,7 +464,8 @@ clauselist_selectivity_simple_or(PlannerInfo *root,
 								 JoinType jointype,
 								 SpecialJoinInfo *sjinfo,
 								 Bitmapset *estimatedclauses,
-								 Selectivity s)
+								 Selectivity s,
+								 bool use_extended_stats)
 {
 	ListCell   *lc;
 	int			listidx;
@@ -488,7 +494,7 @@ clauselist_selectivity_simple_or(PlannerInfo *root,
 										 varRelid,
 										 jointype,
 										 sjinfo,
-										 false);
+										 use_extended_stats);
 
 		s = s + s2 - s * s2;
 	}
diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c
index f5493bcad6..ebc4887ee6 100644
--- a/src/backend/statistics/extended_stats.c
+++ b/src/backend/statistics/extended_stats.c
@@ -1380,10 +1380,12 @@ statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varReli
 		 */
 		if (is_or)
 			simple_sel = clauselist_selectivity_simple_or(root, stat_clauses, varRelid,
-														  jointype, sjinfo, NULL, 0.0);
+														  jointype, sjinfo, NULL, 0.0,
+														  false);
 		else
 			simple_sel = clauselist_selectivity_simple(root, stat_clauses, varRelid,
-													   jointype, sjinfo, NULL);
+													   jointype, sjinfo, NULL,
+													   false);
 
 		/*
 		 * Now compute the multi-column estimate from the MCV list, along with
diff --git a/src/include/optimizer/optimizer.h b/src/include/optimizer/optimizer.h
index de24d7e1d4..bae13ad1c5 100644
--- a/src/include/optimizer/optimizer.h
+++ b/src/include/optimizer/optimizer.h
@@ -63,14 +63,16 @@ extern Selectivity clauselist_selectivity_simple(PlannerInfo *root,
 												 int varRelid,
 												 JoinType jointype,
 												 SpecialJoinInfo *sjinfo,
-												 Bitmapset *estimatedclauses);
+												 Bitmapset *estimatedclauses,
+												 bool use_extended_stats);
 extern Selectivity clauselist_selectivity_simple_or(PlannerInfo *root,
 													List *clauses,
 													int varRelid,
 													JoinType jointype,
 													SpecialJoinInfo *sjinfo,
 													Bitmapset *estimatedclauses,
-													Selectivity s);
+													Selectivity s,
+													bool use_extended_stats);
 extern Selectivity clauselist_selectivity(PlannerInfo *root,
 										  List *clauses,
 										  int varRelid,
-- 
2.26.2

