Re: should we have a fast-path planning for OLTP starjoins?

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: should we have a fast-path planning for OLTP starjoins?
Date: 2026-11-25 19:07:12
Message-ID: da2db963-e39d-4c35-b78c-ecee495d220c@vondra.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Here's a v7 of the starjoin planning patch. Or rather patches, because
it's now a series with 28 patches. Ultimately it should be squashed to
maybe one or two parts, I've kept it like this to make review simpler,
as it makes the incremental changes more obvious. Most of the patches
are pretty small, and the commit message explains the change.

The patches also have a STARJOINS.md documenting the whole feature. It's
partially written by AI, but I went through it and made sure it's
accurate and not hallucinated in some way. I chose to keep it more
detailed, so it may seem overly verbose. But it's intentional, and it's
not meant for commit, but I suppose some much-compressed variant of it
could eventually be committed.

The commit messages are more a development documentation / notes too,
I've included details like some basic performance measurements etc. In
the end it'd get squashed and reworded, of course.

The patch series may be split into several "groups" of patches:

v7-0001-Speed-up-planning-of-starjoin-like-queries.patch
--------------------------------------------------------

This is the original v6 patch, with a bunch of fixes and a lot of new
regression tests for those fixes. There was a lot of problems in how
the starjoin clusters were checked - try using the tests with v6.

Most of the issues were about violating "assumptions" in what joins we
can get as subproblems. For example, the code assumed we'll never see
multiple dimensions without seeing the fact too, or that the subproblems
match the relid order. Neither of these holds, because we can do a cross
join of dimensions first, and the fact can "fall" into the next join
subproblem. Or the dimensions can be joined in a different order, and
if an early relid falls into the next subproblem, the prefix check would
fail, making all plans invalid. The tests trigger these cases by
reducing the join_collapse_limit and picking an explicit join order.

The 0001 patch fixes that by restricting join clusters to a subproblem,
i.e. it considers only the part that falls into the current "context".
There's a couple more check if the fact is already joined, and so on.

v7-0002-Show-the-width-dependent-cost-issue-in-starjoin_w.patch
---------------------------------------------------------------

This demonstrated a "flaw" in the basic assumption that the order of
dimension joins does not matter. It does matter, the plans can have
different costs. It's not clear how to deal with this.

I'll discuss this in more detail later in this message.

v7-0003-Make-starjoin-pruning-canonical.patch
v7-0004-Allow-starjoin-dimensions-in-the-syntactic-LHS-of.patch
v7-0005-Reject-selective-dimension-joins-in-starjoin-clus.patch
v7-0006-Accept-fact-only-members-in-starjoin-dimension-EC.patch
v7-0007-Exclude-sampled-dimensions-from-starjoin-clusters.patch
v7-0008-Don-t-join-starjoin-dimensions-without-their-fact.patch
v7-0009-Allow-joining-a-starjoin-dimension-to-rels-withou.patch
---------------------------------------------------------------

These are mostly improvements on top of the 0001, making the various
checks more precise. The commit messages are briefly explaining each
change. The gist is that the checks in 0001 are not quite right. Some
are too strict (rejecting valid dimensions), other are not strict
enough (accepting e.g. sampled relations as dimensions).

v7-0010-Determine-starjoin-dimensions-once-per-join-searc.patch
---------------------------------------------------------------

This is an optimization, on top of 0001. One of the main fixes in 0001
is that we only consider the part of the starjoin cluster that overlaps
with the current join subproblem (the sets of relids). But it's wasteful
to calculate the intersection on every starjoin_order_invalid() call,
it does not change for the subproblem. So 0010 caches it, which for
some (admittedly fairly huge) queries eliminates ~75% of the cost.

v7-0011-GEQO-Add-a-starjoin_geqo-test-for-star-queries-pl.patch
v7-0012-GEQO-Count-starjoin-dimensions-as-fewer-rels-towa.patch
v7-0013-GEQO-Don-t-enforce-the-canonical-starjoin-order-i.patch
v7-0014-GEQO-Add-an-80-dimension-starjoin-planning-benchm.patch
---------------------------------------------------------------

As implemented in 0001, the starjoin optimization applies both to the
exhaustive DP and to the GEQO. But at some point I started wondering if
maybe GEQO should ignore that. Because GEQO is not actually exploring
all the different join orders, so there's not much to optimize. In fact,
I think it's detrimental, because enforcing the canonical join order
would force GEQO to try over and over, without actually saving anything.
So 0013 disables the optimization for GEQO.

But there's another aspect when evaluating geqo_threshold. I think it
makes sense to not count dimensions against geqo_threshold, because the
dimensions don't make the join search much harder. 0012 does this.

0011 and 0014 are just some tests / benchmarks for the other changes.

v7-0015-FK-Consider-all-matching-foreign-keys-for-a-starj.patch
v7-0016-FK-Exclude-PERIOD-foreign-keys-from-starjoin-dime.patch
v7-0017-FK-Check-collation-equality-before-accepting-a-st.patch
v7-0018-FK-Relax-the-collation-check-for-starjoin-dimensi.patch
v7-0019-FK-Check-operator-semantics-before-relaxing-starj.patch
v7-0020-FK-Exclude-ONLY-scans-of-partitioned-parents-from.patch
v7-0021-FK-Exclude-referenced-side-partition-entries-from.patch
v7-0022-FK-Require-immutable-equality-for-starjoin-dimens.patch
v7-0023-FK-Check-operator-and-input-coercions-for-starjoi.patch
v7-0024-FK-Accept-domains-over-arrays-and-ranges-for-star.patch
---------------------------------------------------------------

Now the fun parts, with open questions, and possibly even stuf that
might be existing bugs (or gaps) in get_foreign_key_join_selectivity.
Some of this comes from AI reviews, and AI seems to be very good in
finding gaps and counter examples to assumptions. In this case, the
premise is "foreign key guarantees the join does not change cardinality
of the join". And there is a bunch of examples contradicting that.

- 0015 - duplicate/overlapping foreign keys - Weird, not really a bug,
IMO, I think it'd be OK to just ignore this. But it's not hard to
handle it, so what.

- 0016 - temporal foreign keys - PERIOD foreign keys can actually change
the cardinality, it's not a 1:1 thing.

- 0017/0018 - mismatching collations can change cardinality too, so
consider than when matching the FK

- 0019 - a bit weird (collation-dependent) operator class can break the
estimate too

- 0020 - query matching just partitioning parent still matches the FK,
but it can't actually match anything (parent is empty)

- 0021-0024 - a bit weird examples with mismatching types, ...

I believe I could come up with more cases like this, but it'd be more
and more bizarre so I stopped. OTOH at least 0016-0018 seem like a valid
issues, and I suspect even get_foreign_key_join_selectivity might need
to deal with them in some way (instead of just trusting the FK).

But some seem like a made-up problem, and it'd be fine to ignore them.

v7-0025-PSEUDOCONST-Add-a-starjoin_var_free-test-for-star.patch
v7-0026-PSEUDOCONST-Ignore-pseudoconstant-quals-in-starjo.patch
v7-0027-PSEUDOCONST-Ignore-pseudoconstants-for-join-conne.patch
v7-0028-PSEUDOCONST-Ignore-pseudoconstant-quals-when-choo.patch
---------------------------------------------------------------

All these cases deal with a somewhat surprising issue. Consider a
var-free clause, such as "now() > '2000-01-01'" or an uncorrelated
EXISTS. This is a pseudoconstant with the relids of the whole join
domain, so it's in the joininfo list of every rel. Which means no rel
will quality as a starjoin dimension!

0025 adds a test, 0026-0028 try to ignore the pseudoconstants in
various cases. But TBH I'm not sure if this really is needed, because
how common are such var-free clauses in practice? And yes, if you use
it, it'll still plan just fine, it'll just not use the optimization.
So I'm not convinced we need to solve this, but maybe I'm wrong?

The order of dimensions does matter!
------------------------------------

The basic assumption of the optimization was that the order of joins to
dimensions does not matte, that joining D1,D2 is the same as D2,D1 (same
cost for both plans). But that's not true, we can get different costs
for different orders of the dimension joins.

The trouble seems to be that the dimensions may be of different size,
some may fit into work_mem in a hash join, and others may require
batching. On the other hand, dimensions may contribute different amounts
of data to the intermediate result - a relation may fit into work_mem,
but contribute a significant amount of data to each tuple. It matters
if this happens before/after the spilling, of course.

So it's possible to construct cases when joining the smaller dimension
first is a win, and also cases when it's better to join the larger one
first. I'm attaching two reproducers demonstrating this (small-first.sql
and large-first.sql).

But that's not all. There's also GROUP BY and eager aggregation, and
different join orders allow pushing down the join to different query
depth. This matters even when everything fits into memory. I'm attaching
two more reproducers demonstrating this (in-memory-unique.sql and
in-memory-aggregate.sql).

There's probably some heuristic to get "good" order, but getting the
optimal one likely requires an exhaustive search (and removing that is
the point of this optimization).

I'm open to ideas. Maybe there's a good solution to this? Could we maybe
do a simplified exhaustive search on this small subproblem, somehow?

Maybe we should treat large relations requiring batching as not
dimensions, i.e. exclude them from the optimization, so that only the
small relations would still benefit. That seems like it might be
acceptable, not sure. For the eager aggregation, a good heuristic is
probably to join the GROUP BY dimensions early.

regards

--
Tomas Vondra

Attachment Content-Type Size
v7-0001-Speed-up-planning-of-starjoin-like-queries.patch text/x-patch 238.3 KB
v7-0002-Show-the-width-dependent-cost-issue-in-starjoin_w.patch text/x-patch 7.8 KB
v7-0003-Make-starjoin-pruning-canonical.patch text/x-patch 13.6 KB
v7-0004-Allow-starjoin-dimensions-in-the-syntactic-LHS-of.patch text/x-patch 9.4 KB
v7-0005-Reject-selective-dimension-joins-in-starjoin-clus.patch text/x-patch 22.6 KB
v7-0006-Accept-fact-only-members-in-starjoin-dimension-EC.patch text/x-patch 12.7 KB
v7-0007-Exclude-sampled-dimensions-from-starjoin-clusters.patch text/x-patch 17.5 KB
v7-0008-Don-t-join-starjoin-dimensions-without-their-fact.patch text/x-patch 13.4 KB
v7-0009-Allow-joining-a-starjoin-dimension-to-rels-withou.patch text/x-patch 22.0 KB
v7-0010-Determine-starjoin-dimensions-once-per-join-searc.patch text/x-patch 16.1 KB
v7-0011-GEQO-Add-a-starjoin_geqo-test-for-star-queries-pl.patch text/x-patch 23.2 KB
v7-0012-GEQO-Count-starjoin-dimensions-as-fewer-rels-towa.patch text/x-patch 23.3 KB
v7-0013-GEQO-Don-t-enforce-the-canonical-starjoin-order-i.patch text/x-patch 13.3 KB
v7-0014-GEQO-Add-an-80-dimension-starjoin-planning-benchm.patch text/x-patch 13.5 KB
v7-0015-FK-Consider-all-matching-foreign-keys-for-a-starj.patch text/x-patch 15.1 KB
v7-0016-FK-Exclude-PERIOD-foreign-keys-from-starjoin-dime.patch text/x-patch 16.2 KB
v7-0017-FK-Check-collation-equality-before-accepting-a-st.patch text/x-patch 29.2 KB
v7-0018-FK-Relax-the-collation-check-for-starjoin-dimensi.patch text/x-patch 10.9 KB
v7-0019-FK-Check-operator-semantics-before-relaxing-starj.patch text/x-patch 23.2 KB
v7-0020-FK-Exclude-ONLY-scans-of-partitioned-parents-from.patch text/x-patch 11.7 KB
v7-0021-FK-Exclude-referenced-side-partition-entries-from.patch text/x-patch 15.7 KB
v7-0022-FK-Require-immutable-equality-for-starjoin-dimens.patch text/x-patch 13.1 KB
v7-0023-FK-Check-operator-and-input-coercions-for-starjoi.patch text/x-patch 27.7 KB
v7-0024-FK-Accept-domains-over-arrays-and-ranges-for-star.patch text/x-patch 8.8 KB
v7-0025-PSEUDOCONST-Add-a-starjoin_var_free-test-for-star.patch text/x-patch 15.2 KB
v7-0026-PSEUDOCONST-Ignore-pseudoconstant-quals-in-starjo.patch text/x-patch 31.1 KB
v7-0027-PSEUDOCONST-Ignore-pseudoconstants-for-join-conne.patch text/x-patch 26.7 KB
v7-0028-PSEUDOCONST-Ignore-pseudoconstant-quals-when-choo.patch text/x-patch 41.3 KB
in-memory-aggregate.sql application/sql 1.9 KB
in-memory-unique.sql application/sql 1.9 KB
large-first.sql application/sql 1.3 KB
small-first.sql application/sql 1.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2026-11-25 19:24:38 Re: passwordcheck: Make the user name check case-insensitive
Previous Message Masahiko Sawada 2026-11-25 18:57:59 Re: Why is the LSN reported for pg_logical_emit_message() different from other decoded operations?