Re: BUG #19945: Partitionwise join omits lateral dependency from Memoize cache key

From: Nitin Motiani <nitinmotiani(at)google(dot)com>
To: 1548140642(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19945: Partitionwise join omits lateral dependency from Memoize cache key
Date: 2026-11-28 16:23:17
Message-ID: CAH5HC94v=hw_TmTfAGcHnc0vY-6fLcGj9kXHKNg-VwWzz61QGg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

Thanks for the report.

On Wed, Nov 25, 2026 at 11:23 PM PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
>
> A partitionwise join can omit a lateral dependency from a Memoize cache key.
> In this example, each partition's inner scan depends on both o.k and o.x,
> but its Memoize node uses only o.k. Alternating x values therefore cause
> repeated cache purges instead of hits.
>
> With partitionwise join enabled, the relevant parts of the plan are:
>
> -> Memoize (actual rows=1.00 loops=4)
> Cache Key: o_1.k
> Cache Mode: logical
> Hits: 0 Misses: 4 Evictions: 3 Overflows: 0 Memory Usage: 1kB
> -> Seq Scan on inner_t_p1 i_1 (actual rows=1.00 loops=4)
> Filter: ((o_1.x = y) AND (o_1.k = k))
>
> -> Memoize (actual rows=1.00 loops=4)
> Cache Key: o_2.k
> Cache Mode: logical
> Hits: 0 Misses: 4 Evictions: 3 Overflows: 0 Memory Usage: 1kB
> -> Seq Scan on inner_t_p2 i_2 (actual rows=1.00 loops=4)
> Filter: ((o_2.x = y) AND (o_2.k = k))
>

I was able to reproduce this. I used LLM to find some other queries
which might be affected. Any non-strict expression in the lateral
output that mixes in an outer column hits it too. For example, with
order_lines and products both partitioned by tenant_id, the below
query also ran into the same issue.

SELECT ol.order_id, pr.list_price, pr.price_currency
FROM order_lines ol
LEFT JOIN LATERAL (
SELECT p.list_price, COALESCE(ol.currency, p.currency) AS price_currency
FROM products p
WHERE p.tenant_id = ol.tenant_id AND p.product_id = ol.product_id
) pr ON true;

With partitionwise joins, there were no cache hits.

This stems from the fact that for a partitionwise join,
extract_lateral_vars_from_PHVs() compares the PHV's ph_eval_at, which
uses parent relids, with the child rel's relids. This check never
matches, so lateral references inside PHVs are left out of the memoize
cache key. So memoize then purges its cache on every rescan.

The attached patch fixes this inside extract_lateral_vars_from_PHVs()
by comparing against the child's top_parent_relids. When I ran this,
an existing test involving multiple levels of partitions started
failing. This involved the intermediate layer in the partition
hierarchy. So I explicitly limited the change to the leaf level
partitions to keep the code contained in one function.

I'm attaching the patch. Please take a look.

Thanks & Regards,
Nitin Motiani
Google

Attachment Content-Type Size
v1-0001-Fix-Memoize-cache-keys-for-PHV-lateral-refs-with-.patch application/x-patch 11.1 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message shihao zhong 2026-11-28 17:40:00 Re: BUG #17545: Incorrect selectivity for IS NOT DISTINCT FROM and NULLs
Previous Message Nitin Motiani 2026-11-28 15:54:37 Re: BUG #19951: levenshtein_less_equal returns 0 when exact distance is 1 with a negative cost