Re: pg_plan_advice

Robert Haas <robertmhaas@gmail.com>

From: Robert Haas <robertmhaas@gmail.com>
To: Jacob Champion <jacob.champion@enterprisedb.com>
Cc: Dian Fay <di@nmfay.com>, Matheus Alcantara <matheusssilv97@gmail.com>, Jakub Wartak <jakub.wartak@enterprisedb.com>, PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2026-01-14T22:23:21Z
Lists: pgsql-hackers
On Tue, Jan 13, 2026 at 1:48 PM Jacob Champion
<jacob.champion@enterprisedb.com> wrote:
> The first thing found with the new architecture is this:
>
>     -- note that f is not a partitioned table
>     SET pg_plan_advice.advice = 'join_order(f/e (f d))';
>     EXPLAIN (COSTS OFF, PLAN_ADVICE)
>         SELECT * FROM gt_fact f JOIN gt_dim d ON f.dim_id = d.id;
>     ERROR: cannot determine RTI for advice target
>
> Test, and a quick guess at expected output, attached.

Thanks. There are two separate bugs here. One is that
pgpa_walker_get_rti() is completely wrong-headed in thinking that only
system-generated advice should reach that function, and therefore that
it doesn't need to deal with 0 return values from
pgpa_compute_rti_from_identifier(). I've deleted pgpa_walker_get_rti()
and made the code that called it instead call
pgpa_compute_rti_from_identifier() and deal with 0 return values. That
revealed a second bug, which is that it thought that the
join_order(f/e (f d)) advice was fully matched, despite f/e not
existing in the query. That turns out to be because
pgpa_join_order_permits_join() was doing entry->flags |=
PGPA_TE_MATCH_FULL even when processing a sublist -- so the fact that
it found (f d) in the query made it think that it had matched the
entire join order specification, when in reality it had only matched
the entirety of a sublist. With that fixed, plan_advice.advice =
'join_order(f/d1 (d1 d2))' produces this:

+ Nested Loop
+   Disabled: true
+   Join Filter: ((d1.id = f.dim1_id) AND (d2.id = f.dim2_id))
+   ->  Nested Loop
+         ->  Seq Scan on jo_dim1 d1
+               Filter: (val1 = 1)
+         ->  Materialize
+               ->  Seq Scan on jo_dim2 d2
+                     Filter: (val2 = 1)
+   ->  Seq Scan on jo_fact f
+ Supplied Plan Advice:
+   JOIN_ORDER(f/d1 (d1 d2)) /* partially matched */
+ Generated Plan Advice:
+   JOIN_ORDER(d1 d2 f)
+   NESTED_LOOP_PLAIN(f)
+   NESTED_LOOP_MATERIALIZE(d2)
+   SEQ_SCAN(d1 d2 f)
+   NO_GATHER(f d1 d2)

This is because we don't have a global view of whether the join order
is valid. The planner works up from the bottom of the plan tree and
sees that joining f to d1 or d2 first contradicts the (d1 d2) portion
of the JOIN_ORDER advice, so the first join that gets done is the
d1-d2 join, which is not disabled. Joining the result to f also
contradicts the JOIN_ORDER advice, but there's no alternative to
consider so the planner picks the disabled path as the only option.

I was hoping to get a new version of the patch set with fixes for
these issues out today, but I've run out of day, so I'll have to come
back to that, hopefully tomorrow.

-- 
Robert Haas
EDB: http://www.enterprisedb.com



Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. pg_plan_advice: Fix another unique-semijoin bug.

  2. pg_plan_advice: Export feedback-related definitions.

  3. pg_plan_advice: Fix a bug when a subquery is pruned away entirely.

  4. pg_plan_advice: Add alternatives test to Makefile.

  5. pg_plan_advice: Handle non-repeatable TABLESAMPLE scans.

  6. pg_stash_advice: Allow stashed advice to be persisted to disk.

  7. Add pg_stash_advice contrib module.

  8. pg_plan_advice: Avoid assertion failure with partitionwise aggregate.

  9. pg_plan_advice: Invent DO_NOT_SCAN(relation_identifier).

  10. Add an alternative_plan_name field to PlannerInfo.

  11. pg_plan_advice: Refactor to invent pgpa_planner_info

  12. Respect disabled_nodes in fix_alternative_subplan.

  13. get_memoize_path: Don't exit quickly when PGS_NESTLOOP_PLAIN is unset.

  14. test_plan_advice: Set TAP test priority 50 in meson.build.

  15. pg_plan_advice: Avoid a crash under GEQO.

  16. Test pg_plan_advice using a new test_plan_advice module.

  17. pg_plan_advice: Always install pg_plan_advice.h, and in the right place

  18. pg_plan_advice: Fix failures to accept identifier keywords.

  19. Add pg_plan_advice contrib module.

  20. Allow extensions to mark an individual index as disabled.

  21. Replace get_relation_info_hook with build_simple_rel_hook.

  22. Store information about Append node consolidation in the final plan.

  23. Store information about elided nodes in the final plan.

  24. Store information about range-table flattening in the final plan.

  25. Pass cursorOptions to planner_setup_hook.

  26. Fix PGS_CONSIDER_NONPARTIAL interaction with Materialize nodes.

  27. Fix mistakes in commit 4020b370f214315b8c10430301898ac21658143f

  28. Allow for plugin control over path generation strategies.

  29. Update some comments for fasthash

  30. Allow passing a pointer to GetNamedDSMSegment()'s init callback.

  31. Don't reset the pathlist of partitioned joinrels.

  32. Treat number of disabled nodes in a path as a separate cost metric.