pg_plan_advice fails on a CustomScan that replaces a join
Hi,
This is about finding #19 in the defect report attached to Noah's
review of pg_plan_advice [1]/messages/by-id/20260827171830.68.noahmisch@microsoft.com: pgpa_relids() ignores
CustomScan.custom_relids. Robert wrote [2]/messages/by-id/CA+TgmoYmXy-jiP5qDhqNEiYFEBzQsArO6O2d9E8szNZqi1bePQ@mail.gmail.com:
#19 might be a real bug, but needs validation, and can't be easily
validated with in-core code.
I validated #19 with a small test-only custom scan provider (cjoin.c,
attached) and a script that uses it (cjoin.sql). The provider uses
set_join_pathlist_hook to replace the inner join of two tables named
cj_a and cj_b with one CustomScan that has scanrelid = 0. It only
builds the plan and cannot execute the join.
On master (852fd5b86e1), with cjoin loaded:
EXPLAIN (COSTS OFF, PLAN_ADVICE)
SELECT * FROM cj_a a JOIN cj_b b ON a.id = b.id
JOIN cj_c c ON c.id = a.id;
ERROR: XX000: plan node has no RTIs: 360
LOCATION: pgpa_build_scan, pgpa_scan.c:215
360 is T_CustomScan. The plan, from EXPLAIN without PLAN_ADVICE, is:
Hash Join
Hash Cond: (a.id = c.id)
-> Custom Scan (CustomJoin)
-> Hash
-> Seq Scan on cj_c c
When the custom scan is the topmost node, there is no error, but no
advice is generated for the two tables.
The same query without EXPLAIN also fails when
pg_plan_advice.always_store_advice_details is on, or when
pg_plan_advice.feedback_warnings is on and any advice is supplied.
The attached patch makes pgpa_relids() return custom_relids, in the
same way as fs_relids for a ForeignScan. With the patch, the query
above gives:
Generated Plan Advice:
JOIN_ORDER({a b} c)
HASH_JOIN(c)
SEQ_SCAN(c)
NO_GATHER(a b c)
This is the form that the README describes for a custom scan that
scans two tables.
With the patch, the pg_plan_advice, test_plan_advice and
test_extensible suites pass. The patch also applies to REL_19_STABLE,
but I tested it only on master.
The patch has no regression test, because a test needs a custom scan
provider that replaces a join. src/test/modules/test_extensible
(0e944fe3579) has a provider now, but only for a scan of one table.
Would it be acceptable to add a join mode to it for such a test? I
can write that.
[1]: /messages/by-id/20260827171830.68.noahmisch@microsoft.com
[2]: /messages/by-id/CA+TgmoYmXy-jiP5qDhqNEiYFEBzQsArO6O2d9E8szNZqi1bePQ@mail.gmail.com
Best regards,
Dongpo Liu
On Mon, Oct 5, 2026 at 4:23 AM Dongpo Liu <poe.liu@pm.me> wrote:
The attached patch makes pgpa_relids() return custom_relids, in the
same way as fs_relids for a ForeignScan.
Thanks. Committed and back-patched to v19. I strongly suspect that
there are more problems in this area, many of which will probably
require more redesign than is practical for v19, but that's not a
reason not to commit simple fixes that move the needle, and this seems
to fall into that category.
Longer term, I think we should consider giving names to custom scan
providers, and finding some way to let advice specify one. For
example, CUSTOM_SCAN cjoin ((foo bar)) could mean "use the cjoin
provider to join foo and bar. The exact syntax is open to
bikeshedding. Foreign joins might merit similar treatment, but it
seems less clear, because presumably only one foreign scan provider is
eligible to handle an given scan or join.
The patch has no regression test, because a test needs a custom scan
provider that replaces a join. src/test/modules/test_extensible
(0e944fe3579) has a provider now, but only for a scan of one table.
Would it be acceptable to add a join mode to it for such a test? I
can write that.
I don't know of a reason why that wouldn't be acceptable. I'd just
caution that you should think about how much energy you're willing to
put into this. Making everything work nicely for plan advice with
custom scans is probably a significant amount of work, which is why I
didn't do it. I'm happy to have someone else pick it up, but we'll
probably need to draw a line somewhere in terms of what we can
back-patch into v19, and target anything beyond that at v20 or later.
--
Robert Haas
Databricks