pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chao Li <li.evan.chao@gmail.com>
To: PostgreSQL-development <pgsql-hackers@lists.postgresql.org>
Cc: Robert Haas <robertmhaas@gmail.com>
Subject: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation
Date: Mon, 20 Jul 2026 12:45:06 +0800
Message-ID: <BEDC04E0-6732-4310-95BA-6EC34BC1442C@gmail.com> (raw)

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/










view thread (11+ messages)  latest in thread

Message-ID: <BEDC04E0-6732-4310-95BA-6EC34BC1442C@gmail.com>
Permalink:  ../BEDC04E0-6732-4310-95BA-6EC34BC1442C@gmail.com/
Also on:    postgresql.org/message-id/BEDC04E0-6732-4310-95BA-6EC34BC1442C@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: li.evan.chao@gmail.com, robertmhaas@gmail.com
  Subject: Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation
  In-Reply-To: <BEDC04E0-6732-4310-95BA-6EC34BC1442C@gmail.com>
  References: <BEDC04E0-6732-4310-95BA-6EC34BC1442C@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox