Hi,
The attached patch set adds an optimisation that intensifies the usage
of the sorted NestLoop trick to avoid sorts higher in the query tree.
This employs the idea that it is not so rare a case when an ORDER BY
query (or GROUP BY or DISTINCT operator) can benefit from a pre-sorted
path even if the outer subtree doesn't have an index on the sort
expression. It is especially beneficial when a LIMIT clause and an OUTER
JOIN are present - combined with tuple-bound propagation through the
outer side of such a join, LIMIT can reach the Sort below the join and
turn it into a bounded top-N heapsort.
Let me explain the problem with a typical schema of a sales database. It
has a 'products' table and description tables for each category (see the
SQL script in the attachment).
A front page provides the top-N products based on popularity or other
criteria. The query looks like multiple OUTER JOINs of the main
'products' table with tables for each category. At the top, non-null
fields are combined into a JSON descriptor -- something like the following:
SELECT p.id, p.name, p.category, p.popularity,
json_strip_nulls(json_build_object(
'warranty', e.warranty_months,
'voltage', e.voltage,
'size', c.size,
'color', c.color,
'expiry', f.expiry_days,
'organic', f.organic
)) AS properties
FROM products p
LEFT JOIN electronics_props e ON e.product_id = p.id
LEFT JOIN clothing_props c ON c.product_id = p.id
LEFT JOIN food_props f ON f.product_id = p.id
ORDER BY p.popularity DESC
LIMIT 10;
AFAICS, this pattern is common in sales systems, CRM and ERP workloads -
anywhere you query "top-N items with details" over a normalised schema
with LEFT JOINs to optional property tables.
There is usually no need to join all the tables and sort afterwards;
pre-sorting the outer relation might be more beneficial. This subject
has already been discussed [1,2], but here we try to solve the case in
general. There was an argument that the user might just add an index.
But sometimes it 1) might not be reasonable (like in the example) and 2)
is impossible if the data source is not a plain table but a
FunctionScan, SubqueryScan, etc. The most important source I consider
here is an Append over foreign tables.
There is a sketchy patch set in the attachment that outlines the
solution. It consists of the following parts:
1. Executor part, the ExecSetTupleBound extension: allows recursion into
the outer side of a join to bound the underlying subtree. It is crucial
because we lack a proper hook in this subsystem, which makes the
extensible implementation of such a feature very complex.
2. Planner part, modification of the joinpath.c::match_unsorted_outer.
In the case of NestLoop, where its outer side is mentioned in the
query_pathkeys prefix, and no sort on this prefix yet exists, Postgres
tries to build such a sorted path and proposes a JOIN with sorted output.
3. Adjustment of regression tests.
Open issues
1. Scope: the optimisation targets a specific pattern (ORDER BY + LIMIT
over joins, and NestLoop). One may argue that it is too narrow to
justify core code. That's true, but I see that such a 'Sort pushdown'
might enable the optimiser to create more effective MergeJoins,
aggregates, etc. You may check the regression tests diff for insights.
2. Cost estimation: the Sort node is costed with an estimated tuple
bound from LIMIT in case of OUTER JOIN, which can, in some cases, make
the pre-sorted path look cheaper than it actually is. I don't see an
actual problem here, since it's about in-memory sorting, which shouldn't
be too expensive anyway.
Feedback and review welcome.
References
[1] "Possible optimisation: push down SORT and LIMIT nodes", 2018
https://www.postgresql.org/message-id/E9FA92C2921F31408041863B74EE4C2001AEF33492@CCPMAILDAG03.cantab.local
[2] "Wasteful nested loop join when there is `limit` in the query", 2025
https://www.postgresql.org/message-id/CAAdwFAwm6HwXM_cuPWZBxrxX4E7pBdVg=KcVDSP6q9ume3hYpQ@mail.gmail.com
--
regards, Andrei Lepikhov,
pgEdge
Re: Try a presorted outer path when referenced by an ORDER BY prefix
Hi,
While working on this patch, I continued to receive user complaints and had to
fix several corner cases: keeping parameterised paths away from the Sort, and
reconciling the PG19 changes to semi-join unique-ification with earlier
versions. Planning overhead has been an ongoing problem too. Each of these
problems came from building the sorted outer inside join planning, duplicating
what the pathlist machinery already does. That looked like a red flag that join
planning is the wrong place for it, so I went back to the literature and our code.
After reviewing the literature and the current code, I concluded that we can
simplify the approach.
Simmen et al. [1] call this technique "sort-ahead": a sort required by ORDER BY
(or GROUP BY/DISTINCT) is pushed down into the join tree, and the optimiser
keeps it only where it is cheaper. The main price the planner pays is in the
join search: subtrees with different orders cannot be pruned against each other,
so each extra sort-ahead order multiplies the work. In our case there is only
one order, the one defined by query_pathkeys, so the overhead is a constant factor.
The new version therefore adds the sorted path once per base relation. It then
competes in add_path() as an index path with the same ordering would: it
survives only if no cheaper path provides that order, and it may displace a more
expensive index path.
In the previous version, only the outer side of a NestLoop could use the
presorted path. Now merge joins, Merge Append and ordered Append over
partitions, unique-ification of a semi-join input, and sort-based grouping can
use it too. The regression tests whose plans changed illustrate multiple use cases.
A few extra benefits:
1. When a join multiplies rows, the Sort operator on the base relation handles
fewer and narrower tuples than it would after the join, so it is cheaper and
less likely to spill.
2. The sort is costed on a base relation, whose cardinality we estimate much
more reliably than that of a join result. So the cost of the sort itself,
including the risk of spilling to disk, is less exposed to join misestimation
than a Sort placed above the joins.
3. A Sort on a prefix below the join, completed by an Incremental Sort above it,
can be cheaper than a single full Sort at the top.
Note:
The sort key is now evaluated below a join more often, including on rows the
join later removes. This is existing behaviour of Gather Merge and Merge Append
(see [2]); the create_am test had to stop casting relname to regclass for that
reason.
The patch is attached. An addition to pg_plan_advice as well.
Thoughts?
[1] Simmen, Shekita, Malkemus. Fundamental Techniques for Order Optimization.
SIGMOD 1996.
https://cs.uwaterloo.ca/~gweddell/cs798/p57-simmen.pdf
[2] issues with eager aggregation
https://www.postgresql.org/message-id/6e3bb6d2-e981-49a4-9e7d-f18ee06e94a1%40gmail.com
-- AL