skip to content

In PostgreSQL, how do join_collapse_limit, from_collapse_limit and geqo_threshold bound join-order search, and when would you set join_collapse_limit to 1?

level: seniorimportance: nice to knowfreq 24%

answer

  1. flatten up to eight items
  2. genetic search from twelve
  3. 1 means written JOIN order
  4. planning time versus plan quality

basics

~20 s

from_collapse_limit and join_collapse_limit (both 8) stop subqueries and explicit JOINs merging into one larger search; join problems of geqo_threshold (12) items or more go to the genetic optimizer. join_collapse_limit = 1 joins explicit JOINs in written order.

solid answer

~50 s

PostgreSQL searches join orders exhaustively within each join problem, so it bounds the size of those problems. `from_collapse_limit` (default 8) stops a subquery or view being pulled up into its parent when the merged `FROM` list would exceed 8 items; `join_collapse_limit` (default 8) does the same for explicit `JOIN` syntax, except `FULL JOIN`. A problem that still has `geqo_threshold` (12) or more items is planned by GEQO, the genetic optimizer, which is faster but may miss the best order. Setting `join_collapse_limit = 1` makes the planner join explicit `JOIN`s in the order written. I use it when I know a better order than the planner finds, or to cut planning time for a generated query with many joins, and I scope it to that statement's transaction. Otherwise I keep `join_collapse_limit` equal to `from_collapse_limit`, as the manual suggests.

code

sql · 10 lines
sql
BEGIN;
SET LOCAL join_collapse_limit = 1;
EXPLAIN
SELECT m.id, sum(l.points)
FROM members m
JOIN loyalty_ledger l ON l.member_id = m.id
JOIN rewards r ON r.id = l.reward_id
WHERE m.tier = 'gold'
GROUP BY m.id;
COMMIT;

go deeper

for a junior

Recall that PostgreSQL reorders joins itself and that join_collapse_limit = 1 makes it follow the written JOIN order.

for a middle

Explain the two collapse limits at 8, GEQO from 12 items, and why a flat FROM list is never split by the limits.

for a senior

Use join_collapse_limit narrowly to fix a measured bad order or a planning-time blow-up, and know when raised limits switch GEQO on.

for a principal

Balance planning time against plan quality for generated SQL with many joins, and decide when query shape rather than settings should carry the fix.

## Why PostgreSQL bounds the search The number of possible join orders grows exponentially with the number of relations, and PostgreSQL's standard planner searches them exhaustively. To keep planning time under control, it limits how large each **join problem** may become and switches algorithm for very large ones. The theory of join enumeration is a general query-processing subject; what follows are PostgreSQL's own knobs. ## The settings | setting | default | effect | |---|---|---| | `from_collapse_limit` | 8 | a subquery or view is merged into its parent only if the parent's `FROM` list stays at or under this size | | `join_collapse_limit` | 8 | explicit `JOIN`s (except `FULL JOIN`) are flattened into a `FROM` list only up to this size | | `geqo` | on | enables the genetic query optimizer | | `geqo_threshold` | 12 | join problems with at least this many `FROM` items go to GEQO | | `geqo_effort` | 5 | trades GEQO planning time for plan quality | | `geqo_seed` | 0 | seed for GEQO's random choices | All of them are user-settable, so they can be changed for one session or one transaction. ## How the limits split a query 1. **Views and subqueries.** A view containing a three-way join, used in a query with two other tables, is normally pulled up into one five-item problem, which usually plans better. If merging would exceed `from_collapse_limit`, the subquery is planned on its own and its result joined as a unit. 2. **Explicit JOIN trees.** `a JOIN b JOIN c ...` is flattened into one `FROM` list when that list stays within `join_collapse_limit`; otherwise the parts are planned separately in roughly the written grouping. 3. **Flat FROM lists are not split.** A query that lists fourteen tables in one comma-separated `FROM` clause is a single fourteen-item problem; the collapse limits only stop merging, they never cut a list apart. At defaults it goes straight to GEQO. 4. **GEQO per problem.** Each problem with `geqo_threshold` or more items is planned genetically; smaller ones keep exhaustive search. Raising either collapse limit to 12 or more can therefore turn on GEQO for queries that did not use it before. ## join_collapse_limit = 1 in practice - **Written order becomes join order.** Inner `JOIN`, `CROSS JOIN` and a plain `FROM` list mean the same thing semantically, so normally the planner reorders them freely. At 1, explicit joins are executed in the order written. - **Partial constraints work.** In `FROM a CROSS JOIN b, c, d, e`, the setting forces `a` to join `b` first and leaves the rest free, cutting the search space without fixing it entirely. - **Outer joins already constrain some orders.** Only `FULL JOIN` fixes order completely; `LEFT JOIN` chains can often still be rearranged at the default setting. - **Use it narrowly.** Set it with `SET LOCAL` for the transaction that runs the statement, and only when you have measured that your order is better; the manual suggests it as a tool for experts who know the right order. ## GEQO behaviour worth knowing - With the default `geqo_seed`, GEQO is repeatable: the same query with the same statistics produces the same plan. Plans move when statistics, settings or the seed change. - Raising `geqo_threshold` to keep exhaustive search on big queries can make planning time exceed execution time, and that cost is paid on every custom-planned execution. - PostgreSQL 18's **self-join elimination** runs before join search and removes redundant self-joins, which can shrink a problem below the threshold. ## A diagnostic routine 1. Count the `FROM` items the query really has after views are expanded; generated SQL and nested views often carry more relations than the author expects. 2. Run `EXPLAIN (ANALYZE)` and compare Planning Time with Execution Time. Planning that dominates points at search size; a slow execution with a poor join order points at estimates or at a problem that was split or handed to GEQO. 3. Experiment in one transaction: lower `join_collapse_limit` to cap planning time, or raise both collapse limits (staying below `geqo_threshold`) to let the planner see the whole problem. 4. Keep the change only where it helps, scoped to that statement, and re-check it when the schema or data volume changes.

  • How does PostgreSQL 18's self-join elimination interact with join-order search?
    Before join search, the planner removes an inner join of a table to itself when the join clause proves both sides are the same row, for example equality on a unique key, and keeps a single scan. That leaves fewer items to order, sometimes enough to stay under geqo_threshold. enable_self_join_elimination, on by default, turns it off.
  • A generated PostgreSQL query lists fourteen tables in one comma-separated FROM clause. Does join_collapse_limit reduce its planning time?
    No. The collapse limits only stop separate subproblems from being merged; a flat fourteen-item FROM list is already one problem, and at the default geqo_threshold of 12 it is planned by GEQO. To constrain it, rewrite it with explicit JOINs and lower join_collapse_limit for that statement.
  • Why not raise the collapse limits and geqo_threshold to 20 in PostgreSQL to get better plans?
    Exhaustive search grows exponentially, so a twenty-table problem can take far longer to plan than to run, and custom-planned statements pay that on every execution. Raise the limits only for specific statements, measuring Planning Time before and after.

  • join_collapse_limit = 1 forces every join to be a nested loop.
  • INNER JOIN syntax always fixes PostgreSQL's join order at any setting.
  • GEQO plans every PostgreSQL query with more than eight joins.
  • Raising the collapse limits always gives better plans at no cost.
  • PostgreSQL core accepts a hint comment to set the leading join table.