[PATCH v2] Estimate hash join bucket size from the joint ndistinct of the hash keys
Hi,
This is v2 of the first patch of the series posted here [1]/messages/by-id/20261111121207.322512-1-hekuang.git@gmail.com, now split
into its own thread so it can be tracked on the commitfest
independently. It is rebased onto master (as of 45047c51aab), and the
patch file is attached.
Andrei asked in [2]/messages/by-id/f451ecb0-eccc-4c71-b806-6fce8fb543ab@gmail.com whether this patch carries new arguments for
re-visiting the line drawn by the earlier bucket-size discussion [3]/messages/by-id/1de52b5f-e4a6-5729-eeef-51d524534665@enterprisedb.com,
which concluded with 6bb6a62f3. The short answer is yes; the full
answer is in the commit message of the attached patch. In brief:
- Mechanically, the patch does not touch 6bb6a62f3's decision.
estimate_multivariate_bucketsize() keeps priority, the new joint
estimate runs only on the clauses it returns uncovered, and the
final min() can only shrink the estimated bucket, never enlarge it,
so extended statistics still have the final say wherever they exist.
- For the uncovered clauses -- tables without such statistics, the
common case -- the patch deliberately re-visits the conservative
line. The arguments: the same cost table currently prices this
join's output rows under the independence assumption while pricing
the bucket by the largest single-column ndistinct, which is
self-contradictory; estimate_num_groups() is not the bare product
rejected in [3]/messages/by-id/1de52b5f-e4a6-5729-eeef-51d524534665@enterprisedb.com -- it uses sampled per-column ndistinct, applies
functional dependencies, and is bounded by the input rows; and
semi/anti joins are excluded, where keeping them regressed a
merge-semi-join query in the workload by 27%.
- The exposure we do not claim to have solved is a fully correlated
inner side without statistics, where the estimate stays optimistic
by roughly the per-column product. Consistency with the rows
estimator is the bet, since the planner already takes it for the
join's row count. A shape where this clearly misleads plan choice
would make an excellent test case, and I will happily add one.
Changes since the series version: rebased; the joint estimate is now
computed once after the per-clause loop instead of once per clause;
the stale "-- Choose merge join" comments in the stats_ext case now
describe the new behavior (choose hash join); and the plan recorded
by contrib/pg_stash_advice for the two-key join changes, so its
expected file is updated accordingly. Costing logic and the measured
results are unchanged: on the motivating query the corrected estimate
turns a never-chosen hash join (cost 166,622,722) into the winning
plan (cost 282,717; 1,946 ms vs 4,154 ms for the merge join it used
to pick), and workload runs with and without the patch stayed within
the +/-2% run-to-run noise band.
I will register this on the commitfest once the thread is archived.
Thanks,
He Kuang
[1]: /messages/by-id/20261111121207.322512-1-hekuang.git@gmail.com
[2]: /messages/by-id/f451ecb0-eccc-4c71-b806-6fce8fb543ab@gmail.com
[3]: /messages/by-id/1de52b5f-e4a6-5729-eeef-51d524534665@enterprisedb.com
Hi hackers,
This is v3 of the standalone re-post of 0001 from the series "[PATCH 0/2]
Fix two hash join costing errors in final_cost_hashjoin()" (11 November):
/messages/by-id/20261111121207.322512-1-hekuang.git@gmail.com
It has no dependency on 0002, which I posted separately [1]/messages/by-id/CAC5HyryDtq_CaL5v4k+t3vGMBYeJVUVoB5sC9vp4oceK0xfWgQ@mail.gmail.com (now at v2)
and which is tracked as CommitFest entry 7658. I am splitting the series
so that each fix can be reviewed on its own merits.
Changes since v2:
- Rebased onto master 8303f2e13c5c2fab539e11054073a58942cb0f30, the
baseline used by the current CFBot run. The costing change in
final_cost_hashjoin() itself is unchanged.
- The stats_ext bucket-estimation test now uses 20,000 rows for sb_2
instead of 10,000. With equal table sizes the corrected hash join has
two exactly equally priced directions, and the tie is broken differently
by a plain regression run and by the streaming replication run in
t/027_stream_regress.pl. Making sb_2 larger removes the tie and makes
the chosen plan stable in both settings. The affected estimated-row
counts in the expected output are updated accordingly.
- The commit message is rewritten to state the reasoning more directly.
Andrei Lepikhov observed that 0001 looks like the approach discussed
earlier, which concluded with 6bb6a62f3. Let me state precisely what
is new. 6bb6a62f3 added estimate_multivariate_bucketsize(), which
answers only when a multivariate ndistinct statistics object exists.
For every table without one -- the common case -- final_cost_hashjoin()
still walks the hash clauses, keeps the smallest per-clause bucket
fraction, and therefore divides by the largest single-column
ndistinct. Meanwhile calc_joinrel_size_estimate() multiplies the
selectivities of that same join to estimate its output rows. The cost
table thus holds two contradictory beliefs about one join. This patch
covers the path 6bb6a62f3 left uncovered, and it uses
estimate_num_groups() on the remaining clauses rather than a bare
product: the same estimator the row count is built on, and bounded by
the number of input rows.
On a 1M x 1M three-column join with 100 distinct values per column
(one row per bucket), hash join is currently priced at 166,622,722 and
is never chosen. With the corrected bucket size it costs 282,717 and
runs in 1,946 ms, against 4,154 ms for the merge join the planner used
to pick (-53%).
SEMI and ANTI joins are deliberately excluded: their probe charge is
already scaled by inner_scan_frac, and repricing them regressed a
merge-semi-join query in the workload by 27%. A join whose inner side
is merely unique is still corrected, since its bucket really does hold
about one row per key.
Two expected-output files move with this patch. In
contrib/pg_stash_advice the join with two hash clauses now takes a hash
join on both keys, hashed on the fact table and probed with the join of
the two dimension tables, instead of a nested loop on top of a
single-clause hash join. In stats_ext the row-count estimates change
because sb_2 is larger. Both changes follow directly from the corrected
costing and the stabilized test setup.
Verification:
- git apply --check against 8303f2e1 is clean.
- make check-world with the CFBot configure flags
(--enable-cassert --enable-tap-tests --enable-injection-points
--enable-debug) passes in full, including t/027_stream_regress.pl.
Comments welcome.
[1]: /messages/by-id/CAC5HyryDtq_CaL5v4k+t3vGMBYeJVUVoB5sC9vp4oceK0xfWgQ@mail.gmail.com
Regards,
He Kuang