Who this is for: anyone who read my first Postgres article and wanted the rest of the story, or anyone who builds views on views.

The short version: I wrote a patch for the Postgres 15.8 planner. It took one report from 194,910 ms to 114 ms. I then found 8 bugs in my own patch. Then I found that Tom Lane had committed a better fix, so I tested that one against the same data and did not submit mine.

1. The order is 16 years old, and it was a choice

In my first article I showed that the planner splits a big join into groups of join_collapse_limit tables (default 8). Only after that does it remove the LEFT JOINs a query does not need.

That order dates from Postgres 9.0 (2010). Tom Lane put join removal right after the grouping step on purpose. Removal needs facts the planner only builds during that step. Moving it earlier, he wrote, would need "a massive rethinking of the order of operations in the planner". He also judged that removal helps only a minority of queries.

For most queries this is fine. Views on views are the exception.

2. What it did to one report

A reporting view at work expanded to about 435 table references. The query needed about 11 of them. But the groups were drawn while all 435 were still counted. Two tables that needed each other, so the planner could look one up through an index from the other, landed in different groups. That fast plan was no longer possible.

I am not the first to hit this. People have asked about slow nested views since at least 2010 (pgsql-performance, "problem with from_collapse_limit and joined views"). Tom Lane answered a similar question again in 2017. The usual advice is to raise the limit, and that works. It is the workaround I shipped.

I wanted to know if the cause could be removed instead.

3. My patch: remove first, then group

I wrote a pass for 15.8 that removes the unneeded LEFT JOINs from the query tree before the split. The groups are then drawn on the survivors only.

join_collapse_limit Before With my patch
9 194,910 ms 114 ms
8 (default) only about 1.5x faster

At 8 the gain was small. My reading: about 11 tables survive, and 11 is more than 8, so the survivors still get split. At 9 the groups happened to fall the right way. That is the same "holes" behaviour from the first article.

The speed was the easy part.

4. The 8 bugs in my own patch

Correct results were the real work. Here is what I found, in plain words.

# What my patch did wrong What happened
1 Treated t.a = t.b as proof that t matches one row Wrong rows. A test returned 2 rows instead of 6
2 Left a dead entry behind when it removed a join Planner error: "so where are the outer joins?"
3 Opened an index without taking a lock Assertion failure on debug builds
4 Missed a table used through a join alias "no relation entry for relid N"
5 Missed a table needed by a placeholder expression with no column of it Same error
6 Removed the target table of a MERGE Broken MERGE
7 Ignored references from LATERAL subqueries Same error, in system views
8 Registered the new setting without a flag that EXPLAIN needs One regression test failed

Most of these sat in checks I had rewritten myself. I wrote my own "is this table used?" walker and my own "is this row unique?" check. Postgres already has both, tested over many years.

The lesson: a table can be "used" in ways a simple walker misses. Join aliases, placeholder expressions, references from subqueries and the target of a MERGE are four of them. Postgres's own bookkeeping already knows all four.

Bug 1 is the dangerous kind. The others fail loudly. That one returned wrong rows, and nothing complained.

5. Tom Lane's commit

Then I read commit 2ebf25e7d7, committed on 28 August 2026. It uses the same core idea: replace the LEFT JOIN node by its left input.

The purpose is different. He was fixing bug #19560, where a query silently lost a WHERE condition and returned wrong results. The faster grouping is a side effect.

The approach is also different:

  1. Let Postgres decide what is removable, using its own join_is_removable.
  2. Edit the jointree to cut those joins out.
  3. Throw away the derived data and restart planning, so the grouping runs on the survivors.
  4. Repeat until nothing more can be removed.

It was back-patched to 16. It shipped in 16.16, 17.12 and 18.7 on 12 November 2026. It never went into 15.

So the part where my patch failed, the safety proof, is the part he reuses. The price is the restart. My version avoided that cost, but it paid with the bugs above. I did not submit mine.

6. The proof: one commit, nothing else changed

"18.7 is fast" is not proof. 18.7 holds other changes too. So I ran an A/B on a copy of the real data, with default limits (join_collapse_limit = from_collapse_limit = 8):

Build Result
Official 18.6 about 2 minutes
My own build of 18.6, plain over 2 minutes (153 s, 358 s, 678 s)
My own build of 18.6 with only Tom's commit 2.1 s on the first run, then 0.3 s and 0.2 s
Official 18.7 about 40 ms

Both own builds came from the same source tree. I built it plain, applied the 18.6 back-port of the commit to the same tree, and rebuilt. Then I ran both servers on the same copy of the data.

The plan shows why. Without the commit, the child table is read by a scan over the whole account:

Bitmap Heap Scan on enrollment (all ~51k rows of the account) Recheck Cond: (<account filter> AND NOT is_deleted) 

With the commit, the same table is read through an index, one lookup per course:

Index Scan on enrollment Index Cond: (course_id = course.id) 

Index Cond: (course_id = course.id) only exists when course and enrollment sit in the same group. With the commit, my two tables finally landed together. No setting was needed.

7. Two more numbers

15.8 never gets the fix. On 15.8 the shipped workaround is still SET LOCAL join_collapse_limit = 64, with from_collapse_limit left at 8. Raising from_collapse_limit too made this query worse. On the 15.8 copy this gave about 24 ms. The setting stays scoped to one transaction.

What does the restart cost? A reader asked this under my first LinkedIn post. Part of the answer, from official images, same data, fresh statistics:

Version Planning time
18.7 0.5 to 0.8 s
18.6 0.9 to 2.3 s

On these views, 18.7 planned faster, not slower. That is not the full answer. Richard Guo measured the bad case on the mailing list. A chain of 64 removals that each depend on the last took 20.9 ms instead of 1.98 ms, because every link costs one full pass. Views on views can have that shape. I have not yet counted how many restart passes my views need. That is next.

8. Caveats

  • My own builds are slower and noisier than the official images, and plain 18.6 got slower on each run. Compare the ratio and the plan, not the milliseconds.
  • The own builds show a collation version warning, because the locale files came from the official image. It applies equally to both builds.
  • The data is a copy of real data. No names appear here, and the tables are renamed.
  • An early run of mine compared 15.8 with 18.7 and gave the wrong cause. The fresh statistics differed, and the collapse limit made no difference on 15.8. I dropped that comparison and ran the 18.6 A/B instead.
  • This is one query on one data set. It shows what the commit does here. It does not claim every nested view gets faster.

What I'd tell you

If you see a fast fix, find the cause. If you have a patch, look for a better one before you submit. Mine was right about the idea and wrong in eight places, and a Postgres committer had already done the same job with the checks the planner trusts.

Planner folks: beyond the regression suite, how do you test a change like this for wrong results?

Sources