Subject: LATERAL subquery predicate pushdown: filter applied after JSON construction instead of inside join (PG 18)

From: Nyasha Chigwamba <[email-masked]>
Date: Wed, 11 Feb 2026 16:12:31 +0000

Good day Tom,

Note: this is my first email on this forum, please excuse any missed protocols.

We exploring an option to migrate a project from MongoDB to PostgreSQL and want to ensure that the final solution is performant.  We will be keeping some JSONB columns for flexibility and to minimize the scope of application changes. We plan to use views to replace some functionality that were implementing at the application layer. Views use LATERAL joins to assemble related rows into JSON structures.

Version (running inside Docker): PostgreSQL 18.1 (Debian 18.1-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit

When filtering on a right-side JSON field in a view built from LATERAL joins, PostgreSQL applies the predicate after the lateral returns. Dependent lookups and JSON construction run for every left-side row instead of only rows that match.

Putting the filter inside the LATERAL subquery avoids this and we have observed a boost in performance: 740 ms → 477 ms, 24,837 → 155 dependency index searches (~160× fewer).

  *
View: 740 ms, 467K buffers; filter on Subquery Scan (after JSON built); 24,837 dep index searches
  *
Filter in join: 477 ms, 369K buffers; filter on Index Scan (inside LATERAL); 155 dep index searches

Question: Can the planner push predicates on view columns into the underlying LATERAL subqueries?

I have attached the following:

  *
schema (right_side_filter_demo_abstract.sql)
  *
view plan (explain-plan-filter-on-view.txt)
  *
inlined plan (explain-plan-filter-in-join.txt)

Looking forward to your guidance.
 
Regards,
Nyasha
This communication is confidential and if not addressed to you and has been received in error, you must: (i) notify the sender immediately and delete the e-mail; and (ii) refrain from copying, printing, forwarding, publishing or disclosing the contents of the e-mail.

---

From: Tom Lane <[email-masked]>
Date: Wed, 11 Feb 2026 12:37:38 -0500

> Question: Can the planner push predicates on view columns into the underlying LATERAL subqueries?

It can, but it will not push them into a subquery with LIMIT,
because that would potentially change the subquery result.

I'd try to get rid of all those ORDER BY LIMIT bits in your
view definition.  That's generally considered an antipattern
in SQL.  It's definitely an optimization fence.

			regards, tom lane

---

From: Nyasha Chigwamba <[email-masked]>
Date: Mon, 23 Feb 2026 10:52:05 +0000

Hi Tom,

Thank you for this - much appreciated. Removing the ORDER BY LIMIT does change the query and after some further investigation, it looks like I also need to project the raw columns then apply the filters on the projected columns as a opposed to filtering on the constructed JSONB.

Regards,
Nyasha
________________________________
From: Tom Lane <[email-masked]>
Sent: February 11, 2026 12:37 PM
To: Nyasha Chigwamba <[email-masked]>
Cc: [email-masked] <[email-masked]>
Subject: Re: LATERAL subquery predicate pushdown: filter applied after JSON construction instead of inside join (PG 18)

                        regards, tom lane