LATERAL subquery predicate pushdown: filter applied after JSON construction instead of inside join (PG 18) - Mailing list pgsql-performance

From Nyasha Chigwamba
Subject LATERAL subquery predicate pushdown: filter applied after JSON construction instead of inside join (PG 18)
Date
Msg-id CH0PR10MB4908BD90B3C5155DF98D88A2B463A@CH0PR10MB4908.namprd10.prod.outlook.com
Whole thread Raw
Responses Re: LATERAL subquery predicate pushdown: filter applied after JSON construction instead of inside join (PG 18)
List 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).

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

I have attached the following:
  • 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

pgsql-performance by date:

Previous
From: Riaan Stander
Date:
Subject: Postgres IO sweet spot
Next
From: Tom Lane
Date:
Subject: Re: LATERAL subquery predicate pushdown: filter applied after JSON construction instead of inside join (PG 18)