[PATCH] pg_plan_advice: NESTED_LOOP_MEMOIZE advice can fail
Hi,
NESTED_LOOP_MEMOIZE advice can come back "matched, failed" even though
the planner could have done the join that way:
CREATE TABLE o (id int PRIMARY KEY, k int);
CREATE TABLE i (a int, b int, PRIMARY KEY (a, b));
INSERT INTO o SELECT g, g FROM generate_series(1, 1000) g;
INSERT INTO i SELECT g, g FROM generate_series(1, 1000) g;
VACUUM ANALYZE o, i;
LOAD 'pg_plan_advice';
SET pg_plan_advice.advice = 'NESTED_LOOP_MEMOIZE(i)';
EXPLAIN (COSTS OFF)
SELECT * FROM o JOIN i ON i.b = o.k AND i.a = 1 WHERE
https://www.google.com/url?q=http://o.id&source=gmail&ust=1795933592081000&sa=E
= 5;
Nested Loop
Disabled: true
Join Filter: (i.b = o.k)
-> Index Scan using o_pkey on o
Index Cond: (id = 5)
-> Index Only Scan using i_pkey on i
Index Cond: (a = 1)
Supplied Plan Advice:
NESTED_LOOP_MEMOIZE(i) /* matched, failed */
Both ways of scanning i, with and without b = o.k as an index
condition, are estimated at one row and the same cost. So add_path()
keeps only the unparameterized one, and there's nothing left to put a
Memoize on top of. (dc47beacaa fixed a similar-looking failure, but
there the path existed.)
The attached patch adds PGS_CONSIDER_UNPARAMETERIZED, which disables a
rel's unparameterized paths the way PGS_CONSIDER_NONPARTIAL disables
its non-partial ones. pg_plan_advice clears it for the relation that
NESTED_LOOP_MEMOIZE names. add_path() isn't touched.
This doesn't fix NESTED_LOOP_PLAIN(i), which gives the same join
filter here and reports "matched". That's the case we ran into. I
think it needs advice that says whether the inner scan is
parameterized, which Robert mentioned as a possibility in [1]https://www.google.com/url?q=https://postgr.es/m/CA%2BTgmoaZMOikxK%3DLqS%2BJn%2B835h9S139JLGk-3LyETVXw5W5j%3Dw@mail.gmail.com&source=gmail&ust=1795933592081000&sa=E.
[1]: https://www.google.com/url?q=https://postgr.es/m/CA%2BTgmoaZMOikxK%3DLqS%2BJn%2B835h9S139JLGk-3LyETVXw5W5j%3Dw@mail.gmail.com&source=gmail&ust=1795933592081000&sa=E
https://www.google.com/url?q=https://postgr.es/m/CA%2BTgmoaZMOikxK%3DLqS%2BJn%2B835h9S139JLGk-3LyETVXw5W5j%3Dw@mail.gmail.com&source=gmail&ust=1795933592081000&sa=E
Thanks,
Bjorn Pagen
On Sat, Nov 28, 2026 at 12:26 AM Bjorn Pagen <hello@bjornpagen.com> wrote:
SELECT * FROM o JOIN i ON i.b = o.k AND i.a = 1 WHERE [...] = 5;
Sorry, my mail client mangled the last line of this query. It should
be:
SELECT * FROM o JOIN i ON i.b = o.k AND i.a = 1 WHERE o.id = 5;
The link at [1] got wrapped in a redirect as well. It is
/messages/by-id/CA+TgmoaZMOikxK=LqS+Jn+835h9S139JLGk-3LyETVXw5W5j=w@mail.gmail.com
Thanks,
Bjorn Pagen