Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation
Date: 2026-07-21 05:00:57
Message-ID: B5E7F225-2017-46C7-B1B6-02546971AF67@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Jul 20, 2026, at 12:45, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>
> Hi,
>
> While playing with pg_plan_advice, I noticed another small issue.
>
> Each FOREIGN_JOIN target sublist must contain at least two relation identifiers, as the error message states:
> ```
> evantest=# SET pg_plan_advice.advice = 'FOREIGN_JOIN((x))';
> ERROR: invalid value for parameter "pg_plan_advice.advice": "FOREIGN_JOIN((x))"
> DETAIL: Could not parse advice: FOREIGN_JOIN targets must contain more than one relation identifier at or near ")"
> ```
>
> However, it silently accepts an empty sublist:
> ```
> evantest=# SET pg_plan_advice.advice = 'FOREIGN_JOIN(())';
> SET
> ```
>
> The problem is that, the parser currently checks whether the sublist length is == 1, rather than < 2:
> ```
> if ($$->tag == PGPA_TAG_FOREIGN_JOIN)
> {
> foreach_ptr(pgpa_advice_target, target, $3)
> {
> if (target->ttype == PGPA_TARGET_IDENTIFIER ||
> list_length(target->children) == 1)
> pgpa_yyerror(result, parse_error_msg_p, yyscanner,
> "FOREIGN_JOIN targets must contain more than one relation identifier");
> }
> }
> ```
>
> So the fix is simply to change == 1 to < 2. With the fix, FOREIGN_JOIN(()) is rejected:
> ```
> evantest=# SET pg_plan_advice.advice = 'FOREIGN_JOIN(())';
> ERROR: invalid value for parameter "pg_plan_advice.advice": "FOREIGN_JOIN(())"
> DETAIL: Could not parse advice: FOREIGN_JOIN targets must contain more than one relation identifier at or near ")"
> ```
>
> FOREIGN_JOIN() is still accepted. As documented in the opening comments of syntax.sql, empty target lists are allowed for most advice tags, except JOIN_ORDER:
> ```
> -- An empty string is allowed. Empty target lists are allowed for most advice
> -- tags, but not for JOIN_ORDER. "Supplied Plan Advice" should be omitted in
> -- text format when there is no actual advice, but not in non-text format.
> ```

OOPS! Missed the attachment.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v1-0001-Fix-empty-FOREIGN_JOIN-sublist-validation.patch application/octet-stream 2.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ian Lawrence Barwick 2026-07-21 05:14:53 Re: [PATCH] Add pg_get_event_trigger_ddl() function
Previous Message Nico Williams 2026-07-21 04:57:00 Re: document the dangers of granting TRIGGER or REFERENCES