pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

Started by Chao Li4 months ago11 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

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.
```

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

#2Chao Li
li.evan.chao@gmail.com
In reply to: Chao Li (#1)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

On Jul 20, 2026, at 12:45, Chao Li <li.evan.chao@gmail.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/

Attachments:

t253124_2
v1-0001-Fix-empty-FOREIGN_JOIN-sublist-validation.patchapplication/octet-stream; name=v1-0001-Fix-empty-FOREIGN_JOIN-sublist-validation.patch; x-unix-mode=0644Download+5-2
#3Pierre Forstmann
pierre.forstmann@gmail.com
In reply to: Chao Li (#2)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

The following review has been posted through the commitfest application:
make installcheck-world: tested, passed
Implements feature: tested, passed
Spec compliant: not tested
Documentation: not tested

Hello,

I have reviewed this patch and it looks good to me.
I can reproduce the issue in master branch and applying the patch in a specific branch has fixed the issue: new test case looks also good to me.

I don't think it is needed to check SQL compliance or to update documentation.

Regards
Pierre Forstmann.

The new status of this patch is: Ready for Committer

#4Nathan Bossart
nathandbossart@gmail.com
In reply to: Chao Li (#2)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

Does this one deserve a mention on the open items wiki [0]https://wiki.postgresql.org/wiki/PostgreSQL_19_Open_Items?

[0]: https://wiki.postgresql.org/wiki/PostgreSQL_19_Open_Items

--
nathan

#5Chao Li
li.evan.chao@gmail.com
In reply to: Nathan Bossart (#4)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

On Aug 26, 2026, at 03:55, Nathan Bossart <nathandbossart@gmail.com> wrote:

Does this one deserve a mention on the open items wiki [0]?

[0] https://wiki.postgresql.org/wiki/PostgreSQL_19_Open_Items

--
nathan

Hi Nathan,

Thanks for noticing this patch and for asking.

This issue is 19-only, and it is a bug, so I think it meets the criteria. I didn’t add it because I thought it was a tiny bug with a one-line fix and assumed it might be processed soon. Given that it has been left open for a long time, let me add it for better tracking.

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

#6Nathan Bossart
nathandbossart@gmail.com
In reply to: Chao Li (#5)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

On Wed, Aug 26, 2026 at 09:12:42AM +0800, Chao Li wrote:

On Aug 26, 2026, at 03:55, Nathan Bossart <nathandbossart@gmail.com> wrote:
Does this one deserve a mention on the open items wiki [0]?

Thanks for noticing this patch and for asking.

This issue is 19-only, and it is a bug, so I think it meets the criteria.
I didn’t add it because I thought it was a tiny bug with a one-line fix
and assumed it might be processed soon. Given that it has been left open
for a long time, let me add it for better tracking.

Robert, what do you think about the proposed patch?

--
nathan

#7Robert Haas
robertmhaas@gmail.com
In reply to: Nathan Bossart (#6)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

On Wed, Aug 26, 2026 at 6:11 PM Nathan Bossart <nathandbossart@gmail.com> wrote:

Robert, what do you think about the proposed patch?

Looks fine to me. If you feel like taking care of it, awesome,
otherwise I can do it.

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

#8Nathan Bossart
nathandbossart@gmail.com
In reply to: Robert Haas (#7)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

On Thu, Aug 27, 2026 at 11:53:46AM -0400, Robert Haas wrote:

On Wed, Aug 26, 2026 at 6:11 PM Nathan Bossart <nathandbossart@gmail.com> wrote:

Robert, what do you think about the proposed patch?

Looks fine to me. If you feel like taking care of it, awesome,
otherwise I can do it.

I would be grateful if you could take it.

--
nathan

#9Robert Haas
robertmhaas@gmail.com
In reply to: Nathan Bossart (#8)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

On Thu, Aug 27, 2026 at 11:56 AM Nathan Bossart
<nathandbossart@gmail.com> wrote:

I would be grateful if you could take it.

Sure thing, done, and I marked the open item resolved.

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

#10Chao Li
li.evan.chao@gmail.com
In reply to: Robert Haas (#9)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

On Aug 28, 2026, at 04:40, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Aug 27, 2026 at 11:56 AM Nathan Bossart
<nathandbossart@gmail.com> wrote:

I would be grateful if you could take it.

Sure thing, done, and I marked the open item resolved.

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

Thanks for taking care of this patch.

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

#11Robert Haas
robertmhaas@gmail.com
In reply to: Chao Li (#10)
Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation

On Thu, Aug 27, 2026 at 10:53 PM Chao Li <li.evan.chao@gmail.com> wrote:

Thanks for taking care of this patch.

Likewise, thanks for the report.

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