| From: | Nyasha Chigwamba <nyasha(dot)chigwamba(at)voss-solutions(dot)com> |
|---|---|
| To: | "tgl(at)sss(dot)pgh(dot)pa(dot)us" <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Cc: | "pgsql-performance(at)lists(dot)postgresql(dot)org" <pgsql-performance(at)lists(dot)postgresql(dot)org> |
| Subject: | LATERAL subquery predicate pushdown: filter applied after JSON construction instead of inside join (PG 18) |
| Date: | 2026-02-11 16:12:31 |
| Message-ID: | CH0PR10MB4908BD90B3C5155DF98D88A2B463A@CH0PR10MB4908.namprd10.prod.outlook.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-performance |
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.
| Attachment | Content-Type | Size |
|---|---|---|
| explain-plan-filter-in-join.txt | text/plain | 5.7 KB |
| explain-plan-filter-on-view.txt | text/plain | 5.1 KB |
| right_side_filter_demo_abstract.sql | application/octet-stream | 5.9 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2026-02-11 17:37:38 | Re: LATERAL subquery predicate pushdown: filter applied after JSON construction instead of inside join (PG 18) |
| Previous Message | Riaan Stander | 2026-02-10 23:13:01 | Postgres IO sweet spot |