From c51c7f7cfa700d3ec93a2828d3be119ef1839406 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Mon, 3 Oct 2022 00:12:37 +0200
Subject: [PATCH 2/7] review

---
 src/backend/optimizer/path/costsize.c   | 74 ++++++++++++++++++++++++-
 src/backend/optimizer/plan/createplan.c |  5 ++
 src/backend/utils/adt/selfuncs.c        |  2 +
 src/include/optimizer/cost.h            |  9 +++
 4 files changed, 89 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index d1663e5a379..da17da599d7 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -195,6 +195,8 @@ static double get_parallel_divisor(Path *path);
 /*
  *  Local functions and option variables to support
  *  semijoin pushdowns from join nodes
+ *
+ * XXX It's customary to keep the "root" as the first parameter.
  */
 static double evaluate_semijoin_filtering_rate(JoinPath *join_path,
 											   const List *hash_equijoins,
@@ -3901,6 +3903,12 @@ final_cost_mergejoin(PlannerInfo *root, MergePath *path,
 		 * determine if merge join should use a semijoin filter. We need to
 		 * rearrange the merge clauses so they match the status of the clauses
 		 * during Plan creation.
+		 *
+		 * XXX Not sure what "match the status" means? Perhaps ordering of clauses?
+		 * Ah, it means we get the clauses in outer/inner is first/second?
+		 *
+		 * XXX Why do we do the call get_actual_clauses() at all? Clearly the result
+		 * is just discarded right away ...
 		 */
 		mergeclauses_for_sjf = get_actual_clauses(path->path_mergeclauses);
 		mergeclauses_for_sjf = get_switched_clauses(path->path_mergeclauses,
@@ -3912,6 +3920,16 @@ final_cost_mergejoin(PlannerInfo *root, MergePath *path,
 		{
 			/* found a valid SJF at the very least */
 			/* want at least 1000 rows_filtered to avoid any nasty edge cases */
+			/*
+			 * XXX Not sure hard-coded limits will be acceptable. We do that for
+			 * JIT, but that's because we don't have any reasonable cost model
+			 * for the LLVM stuff etc. Can't we construct one here? Surely we
+			 * have at least some rough idea regarding how expensive it is to
+			 * build and query the bloom filter, no?
+			 *
+			 * XXX It's very weird this does not consider if the bloom filter
+			 * fits into work_mem and/or false positive rate.
+			 */
 			if (force_mergejoin_semijoin_filter || (filteringRate >= 0.35 && rows_filtered > 1000))
 			{
 				double		improvement;
@@ -3930,6 +3948,16 @@ final_cost_mergejoin(PlannerInfo *root, MergePath *path,
 				 * concludes thata a 17% filtering rate is the break-even
 				 * point, so we use 35% just be conservative. We use this
 				 * information to adjust the MergeJoin's planned cost.
+				 *
+				 * XXX I'd bet this will very much depend on what the path
+				 * (with the bloom filter) does.
+				 *
+				 * XXX Also, if we're only tweaking costs for the mergejoin
+				 * node, won't that be somewhat confusing? I mean, if we filter
+				 * out most of the rows, then it'll look like the row estimates
+				 * are wrong. I wonder if it may cause some more serious issues.
+				 * Of course, when planning the scans, we don't know there'll be
+				 * a join (not to mention bloom filter pushdown) later.
 				 */
 				improvement = 0.83 * filteringRate - 0.137;
 				run_cost = (1 - improvement) * run_cost;
@@ -6650,6 +6678,11 @@ compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual,
  *  and only if the TRACE_SJPD flag is defined.
  */
 
+/*
+ * XXX If we want some debug logging, perhaps this should do something more like
+ * we have for TRACE_SORT. Inventing new "debug_" macros seems unnecessary.
+ */
+
 #define TRACE_SJPD 0
 #define DEBUG_BUILD 0
 
@@ -6762,7 +6795,10 @@ debug_sj_expr_metadata(const char *side, int ord, const ExprMetadata * md)
 }
 #endif
 
-
+/*
+ * XXX No sure why this function (and various others) have the comment inside,
+ * and not before the function?
+ */
 static double
 estimate_distincts_remaining(double original_table_row_count,
 							 double original_distinct_count,
@@ -6796,6 +6832,11 @@ estimate_distincts_remaining(double original_table_row_count,
 	 *
 	 * Internally this function uses M, N, P, and K as variables to match the
 	 * notation used in the equation in the paper.
+	 *
+	 * XXX We already do similar estimated in estimate_num_groups_incremental,
+	 * i.e. we estimate num of groups when we select just some rows (that's
+	 * at selfuncs.c:3670 - roughly). There's reference to a paper by Alberto
+	 * Dell'Era. Why should this use a different logic? Might be confusing.
 	 */
 	double		n = original_table_row_count;
 	double		m = original_distinct_count;
@@ -7159,6 +7200,8 @@ analyze_expr_for_metadata(const Expr *ex,
  *  is possible, determine the single equijoin condition that is most
  *  useful as well as the estimated filtering rate of the filter.
  *
+ * XXX Why not to allow bloom filter on multiple conditions?
+ *
  *  The output args, inner_semijoin_keys and
  *  outer_semijoin_keys, will each contain a single key column
  *  from one of the hash equijoin conditions. probe_semijoin_keys
@@ -7224,9 +7267,19 @@ evaluate_semijoin_filtering_rate(JoinPath *join_path,
 
 	if (!enable_mergejoin_semijoin_filter && !force_mergejoin_semijoin_filter)
 	{
+		/*
+		 * XXX AFAICS we shouldn't ever get here, because this gets only called
+		 * with enable_mergejoin_semijoin_filter=true, no?
+		 */
 		return 0;				/* option setting disabled semijoin insertion  */
 	}
 
+	/*
+	 * XXX use the memory context through palloc, not alloca() or anything else
+	 * directly. Also, it's probably pointless to check if we got NULL - palloc
+	 * will check that, and in some cases we'll only find later on access anyway
+	 * because of overcommit.
+	 */
 	num_md = list_length(equijoin_list);
 	outer_md_array = alloca(sizeof(ExprMetadata) * num_md);
 	inner_md_array = alloca(sizeof(ExprMetadata) * num_md);
@@ -7564,6 +7617,14 @@ verify_valid_pushdown(const Path *path,
 								 * this fact until the Plan has been created.
 								 * We will push through for now, but verify
 								 * again during Plan creation. */
+
+				/*
+				 * XXX How come we can't validate that? Surely we consider if
+				 * a path hash grouping sets when constructing the AggPath, see
+				 * for example consider_groupingsets_paths. However, do we
+				 * even allow AggPath to have grouping sets? We have a separate
+				 * node type GroupingSetsPath for that, no?
+				 */
 				return verify_valid_pushdown(((AggPath *) path)->subpath, target_var_no, root);
 			}
 
@@ -7577,6 +7638,9 @@ verify_valid_pushdown(const Path *path,
 				 * previous HashJoin CR has implemented these cases, but that
 				 * code is run these after the plan has been created, so code
 				 * will need to be adjusted to do it during Path evaluation.
+				 *
+				 * XXX I'd say the Append case is something we'll need because
+				 * of partitioning, no?
 				 */
 				return false;
 			}
@@ -7589,6 +7653,9 @@ verify_valid_pushdown(const Path *path,
 				 * We could definitely implement pushdown filters for Index
 				 * and Bitmap Scans, but currently it is only implemented for
 				 * SeqScan. For now, we return false.
+				 *
+				 * XXX I think this would be very valuable, as it might
+				 * eliminate a lot of the random I/O with index scans.
 				 */
 				return false;
 			}
@@ -7627,6 +7694,11 @@ verify_valid_pushdown(const Path *path,
 	}
 }
 
+/*
+ * XXX Why we need this, when we have list_nth? In fact, that should be
+ * faster, as it may not need to iterate. Also, why do we ignore the case
+ * when the list is shorter? Shouldn't that be an error?
+ */
 static TargetEntry *
 get_nth_targetentry(int n, const List *targetlist)
 {
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 98a673b9b94..dd912ab76ce 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -7294,6 +7294,11 @@ is_projection_capable_plan(Plan *plan)
  *  	used only for partitioned table.
  *  cur_depth: current depth from the root hash join plan node.
  *  target_node: stores the target plan node where filter will be applied
+ *
+ * XXX I don't understand why this returns int and not bool.
+ * XXX Could we consider pushing the bloom filter only partially, e.g.
+ * as deep as possible (in case there's some sort of node preventing
+ * pushdown further down)?
  */
 static int
 depth_of_semijoin_target(Plan *pn,
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index b62de16899c..bf2d461bbe3 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -2900,6 +2900,8 @@ scalargejoinsel(PG_FUNCTION_ARGS)
  *		*leftend is set to the fraction of the left-hand variable expected
  *		 to be scanned before the join terminates (0 to 1).
  *		*rightstart, *rightend similarly for the right-hand variable.
+ *
+ * XXX Should explain what the new parameters do etc.
  */
 void
 mergejoinscansel(PlannerInfo *root, Node *clause,
diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h
index cca624a1d86..05df9434fee 100644
--- a/src/include/optimizer/cost.h
+++ b/src/include/optimizer/cost.h
@@ -61,6 +61,10 @@ extern PGDLLIMPORT bool enable_nestloop;
 extern PGDLLIMPORT bool enable_material;
 extern PGDLLIMPORT bool enable_memoize;
 extern PGDLLIMPORT bool enable_mergejoin;
+/*
+ * XXX These names sound a bit strange - it's not clear the semijoin
+ * filter is a bloom filter, or that it's pushing down stuff.
+ */
 extern PGDLLIMPORT bool enable_mergejoin_semijoin_filter;
 extern PGDLLIMPORT bool force_mergejoin_semijoin_filter;
 extern PGDLLIMPORT bool enable_hashjoin;
@@ -217,6 +221,11 @@ extern double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel,
 
 /*
  *  Container for metadata about an expression, used by semijoin decision logic
+ *
+ * XXX Probably should be private in costsize.c, why expose it here?
+ *
+ * XXX The name is a bit generic, maybe make sure it's associated to merge
+ * joins and/or bloom filters?
  */
 typedef struct expr_metadata
 {
-- 
2.37.3

