v1-0002-Fix-row-estimation-in-gather-paths.patch
application/octet-stream
Filename: v1-0002-Fix-row-estimation-in-gather-paths.patch
Type: application/octet-stream
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v1-0002
Subject: Fix row estimation in gather paths
| File | + | − |
|---|---|---|
| src/backend/optimizer/path/allpaths.c | 3 | 4 |
| src/backend/optimizer/path/costsize.c | 19 | 0 |
| src/backend/optimizer/plan/planner.c | 9 | 3 |
| src/include/optimizer/optimizer.h | 2 | 0 |
| src/test/regress/expected/join_hash.out | 9 | 10 |
From 95558d304f8880ef2f1f0700b9d62a6f9617d248 Mon Sep 17 00:00:00 2001
From: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Date: Thu, 23 May 2024 11:24:44 +0200
Subject: Fix row estimation in gather paths
In parallel plans, the row count of a partial plan is estimated to
(rows/parallel_divisor). The parallel_divisor is the number of
parallel_workers plus a possible leader contribution.
When creating a gather path, we currently estimate the sum of gathered
rows to worker_rows*parallel_workers which leads to a lower estimated
row count.
This patch changes the gather path row estimation to
worker_rows*parallel_divisor to get a more accurate estimation.
Additionally, when creating a gather merge path in create_ordered_paths,
we try to use the source's rel rows when available.
---
src/backend/optimizer/path/allpaths.c | 7 +++----
src/backend/optimizer/path/costsize.c | 19 +++++++++++++++++++
src/backend/optimizer/plan/planner.c | 12 +++++++++---
src/include/optimizer/optimizer.h | 2 ++
src/test/regress/expected/join_hash.out | 19 +++++++++----------
5 files changed, 42 insertions(+), 17 deletions(-)
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index 4895cee994..c1244a9b83 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -3071,8 +3071,7 @@ generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
* of partial_pathlist because of the way add_partial_path works.
*/
cheapest_partial_path = linitial(rel->partial_pathlist);
- rows =
- cheapest_partial_path->rows * cheapest_partial_path->parallel_workers;
+ rows = gather_rows_estimate(cheapest_partial_path);
simple_gather_path = (Path *)
create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
NULL, rowsp);
@@ -3090,7 +3089,7 @@ generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
if (subpath->pathkeys == NIL)
continue;
- rows = subpath->rows * subpath->parallel_workers;
+ rows = gather_rows_estimate(subpath);
path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
subpath->pathkeys, NULL, rowsp);
add_path(rel, &path->path);
@@ -3274,7 +3273,7 @@ generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_r
subpath,
useful_pathkeys,
-1.0);
- rows = subpath->rows * subpath->parallel_workers;
+ rows = gather_rows_estimate(subpath);
}
else
subpath = (Path *) create_incremental_sort_path(root,
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index ee23ed7835..24feb513ce 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -217,6 +217,25 @@ clamp_row_est(double nrows)
return nrows;
}
+/*
+ * gather_rows_estimate
+ * Estimate the number of rows for gather nodes.
+ *
+ * When creating a gather (merge) path, we need to estimate the sum of rows
+ * distributed to all workers. A worker will have an estimated row set to
+ * (rows / parallel_divisor). Since parallel_divisor may include the leader
+ * contribution, we can't simply multiply workers' rows by the number of
+ * parallel_workers and instead need to reuse the parallel_divisor to get a
+ * more accurate estimation.
+ */
+double
+gather_rows_estimate(Path *partial_path)
+{
+ double parallel_divisor = get_parallel_divisor(partial_path);
+
+ return clamp_row_est(partial_path->rows * parallel_divisor);
+}
+
/*
* clamp_width_est
* Force a tuple-width estimate to a sane value.
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 032818423f..1f0a78be76 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -5281,8 +5281,14 @@ create_ordered_paths(PlannerInfo *root,
root->sort_pathkeys,
presorted_keys,
limit_tuples);
- total_groups = input_path->rows *
- input_path->parallel_workers;
+ total_groups = input_rel->rows;
+
+ /*
+ * If the number of rows is unknown, fallback to gather rows
+ * estimation
+ */
+ if (total_groups == 0)
+ total_groups = gather_rows_estimate(input_path);
sorted_path = (Path *)
create_gather_merge_path(root, ordered_rel,
sorted_path,
@@ -7453,7 +7459,7 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
(presorted_keys == 0 || !enable_incremental_sort))
continue;
- total_groups = path->rows * path->parallel_workers;
+ total_groups = gather_rows_estimate(path);
/*
* We've no need to consider both a sort and incremental sort. We'll
diff --git a/src/include/optimizer/optimizer.h b/src/include/optimizer/optimizer.h
index 7b63c5cf71..8e602cf95f 100644
--- a/src/include/optimizer/optimizer.h
+++ b/src/include/optimizer/optimizer.h
@@ -23,6 +23,7 @@
#define OPTIMIZER_H
#include "nodes/parsenodes.h"
+#include "nodes/pathnodes.h"
/*
* We don't want to include nodes/pathnodes.h here, because non-planner
@@ -92,6 +93,7 @@ extern PGDLLIMPORT int effective_cache_size;
extern double clamp_row_est(double nrows);
extern int32 clamp_width_est(int64 tuple_width);
extern long clamp_cardinality_to_long(Cardinality x);
+extern double gather_rows_estimate(Path *partial_path);
/* in path/indxpath.c: */
diff --git a/src/test/regress/expected/join_hash.out b/src/test/regress/expected/join_hash.out
index 262fa71ed8..4fc34a0e72 100644
--- a/src/test/regress/expected/join_hash.out
+++ b/src/test/regress/expected/join_hash.out
@@ -508,18 +508,17 @@ set local hash_mem_multiplier = 1.0;
set local enable_parallel_hash = on;
explain (costs off)
select count(*) from simple r join extremely_skewed s using (id);
- QUERY PLAN
------------------------------------------------------------------------
- Finalize Aggregate
+ QUERY PLAN
+-----------------------------------------------------------------
+ Aggregate
-> Gather
Workers Planned: 1
- -> Partial Aggregate
- -> Parallel Hash Join
- Hash Cond: (r.id = s.id)
- -> Parallel Seq Scan on simple r
- -> Parallel Hash
- -> Parallel Seq Scan on extremely_skewed s
-(9 rows)
+ -> Parallel Hash Join
+ Hash Cond: (r.id = s.id)
+ -> Parallel Seq Scan on simple r
+ -> Parallel Hash
+ -> Parallel Seq Scan on extremely_skewed s
+(8 rows)
select count(*) from simple r join extremely_skewed s using (id);
count
--
2.39.3 (Apple Git-146)