Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wm2bs-000YBU-15 for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Jul 2026 05:01:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wm2br-005n1m-3D for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Jul 2026 05:01:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wm2br-005n1d-28 for pgsql-hackers@lists.postgresql.org; Tue, 21 Jul 2026 05:01:39 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wm2bo-00000000NsS-30DI for pgsql-hackers@lists.postgresql.org; Tue, 21 Jul 2026 05:01:38 +0000 Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-38e041ea211so8776784a91.0 for ; Mon, 20 Jul 2026 22:01:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784610094; x=1785214894; darn=lists.postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:content-type :message-id:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=dT4ErtLAkuELXH9mqSY7Kt9wp0p+MZk8//DgD+Qd4Jw=; b=o7bvcVitnN5vVYdCyur5ByF9qftE+urVjkEPaGBu7dkrBBfHNTKCrQnhkzf8JBE7S6 Df+3beRke1XlrkyHRWmYCd/aXHM8OOkXhhfv1MGAtF6fHp4v/0ncljfF9pCdyiO4ECWS WL9H7LnXrw0EpRmoYCRZWtpnXOC6eTpZi//9uqqRfQnLmscx3rwudSFgCo8Q8s48NEcT ewiqyxziL5HlJuHRywHf/H2lbIWAyt1MWz2Z+MK2kiT82Y/XwVdcDrgEKKA9FE2+6Hsf fo04gmjGKiVAZbnVT7NlNsHobRrN71mKRfAsiIqFPfeW1vbUWZ32Bxex3ASMRuxbnN/Q 1h0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784610094; x=1785214894; h=references:to:cc:in-reply-to:date:subject:mime-version:content-type :message-id:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=dT4ErtLAkuELXH9mqSY7Kt9wp0p+MZk8//DgD+Qd4Jw=; b=FOJ8Kaasz92DuEPzRW6iPt/TmwtgRxtX9jGvWUY4sg0dw2tYAXw9/M0WpyAphWGERO uzOVAFZeDFOo4wRBpQza9fubW+4qXuxXKRPST4hQs04q/f80Um3zOx4PQrrKh7/jMazR UipjMgm3pAlU9X5yAHzMU+jnVCekGKoPnt6VDebhj32KBLUF0ktx2EtbQFOrl38VBI2c r89cJdFFHY27+vb4N/tZJ5CetPwWq9rxvXiQYNZLuGX+7hj8mnZtkilgWh8yCDJm5uDx fMQEwMc43RAV0/4c6qJxfACOR2GvHLwG9R9iZSPREhJhvXRFwP4iMN2LbkeYeJwlbkLW /sZw== X-Gm-Message-State: AOJu0YwEyCA77YhtqlV9q3XG1fcSNndV1P/fHCvbh7WqKVVJR76QfxYN Ha4ymFjU3XSCv0Rf2H79qDXgkmARjl3BGhU0zyTesM4Cbo1y/q24tkGqyXjxhQ== X-Gm-Gg: AfdE7cmBH3OsH7Dhw0RATPtSWKKxXe37EugKP5UHxia4+GswvyA2BlwqUWMSTkKok5f Ouqtc9r6+qiBgvGL316g8gV8HGHbHWbssCg+NBjOph+50h7JMr/sWoubfQ4xcz8Z5oCVroDHL35 nHOPht2uGnnGTWkpcIpmWgPdQcB9oKj2oc9U40iFiGI9cUmm2jGJDj/pYDYcVCNg5W9Su/heLy0 6VlviAmcPeYyIrL6SEvxaYhb0vpZP+H6Mnw0wn11CsoJ5WNG50lYCR3M1KIH/92usnNWZ3sHihv zWaF2y7snWRn8ngPKoKzKcR7UIcDLWDfyfpGfowCfspSujsEt2zKhbgPpSqFNM5kpV99TmaicVN +M8LyJenTQ6yekryTClmY1+28qdiNzfXbVRV3z98TVicYK4lEcCXnh9MAFGSEFrCxU0LpKhJPsL /dIV2pYWRVUx0= X-Received: by 2002:a05:6300:614e:b0:3c3:875d:704f with SMTP id adf61e73a8af0-3c3ad629f84mr18779738637.18.1784610094162; Mon, 20 Jul 2026 22:01:34 -0700 (PDT) Received: from smtpclient.apple ([185.135.79.125]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cb517f8f067sm5202711a12.10.2026.07.20.22.01.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Jul 2026 22:01:33 -0700 (PDT) From: Chao Li Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_651FFEDF-A729-4072-A89C-20A04EF3EFA0" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: pg_plan_advice: fix empty FOREIGN_JOIN sublist validation Date: Tue, 21 Jul 2026 13:00:57 +0800 In-Reply-To: Cc: Robert Haas To: PostgreSQL-development References: X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_651FFEDF-A729-4072-A89C-20A04EF3EFA0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Jul 20, 2026, at 12:45, Chao Li wrote: >=20 > Hi, >=20 > While playing with pg_plan_advice, I noticed another small issue. >=20 > Each FOREIGN_JOIN target sublist must contain at least two relation = identifiers, as the error message states: > ``` > evantest=3D# SET pg_plan_advice.advice =3D '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 ")" > ``` >=20 > However, it silently accepts an empty sublist: > ``` > evantest=3D# SET pg_plan_advice.advice =3D 'FOREIGN_JOIN(())'; > SET > ``` >=20 > The problem is that, the parser currently checks whether the sublist = length is =3D=3D 1, rather than < 2: > ``` > if ($$->tag =3D=3D PGPA_TAG_FOREIGN_JOIN) > { > foreach_ptr(pgpa_advice_target, target, $3) > { > if (target->ttype =3D=3D PGPA_TARGET_IDENTIFIER || > list_length(target->children) =3D=3D 1) > pgpa_yyerror(result, parse_error_msg_p, yyscanner, > "FOREIGN_JOIN targets must contain more than one relation = identifier"); > } > } > ``` >=20 > So the fix is simply to change =3D=3D 1 to < 2. With the fix, = FOREIGN_JOIN(()) is rejected: > ``` > evantest=3D# SET pg_plan_advice.advice =3D '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 ")" > ``` >=20 > 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/ --Apple-Mail=_651FFEDF-A729-4072-A89C-20A04EF3EFA0 Content-Disposition: attachment; filename=v1-0001-Fix-empty-FOREIGN_JOIN-sublist-validation.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-Fix-empty-FOREIGN_JOIN-sublist-validation.patch" Content-Transfer-Encoding: quoted-printable =46rom=204a0e610157b0d7c6885339d9e64ff19cde14a862=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Mon,=2020=20Jul=202026=2012:40:20=20+0800=0ASubject:=20[PATCH=20= v1]=20Fix=20empty=20FOREIGN_JOIN=20sublist=20validation=0A=0A= FOREIGN_JOIN=20target=20sublists=20must=20contain=20at=20least=20two=20= relation=20identifiers.=0AHowever,=20the=20parser=20checked=20only=20for=20= sublists=20with=20exactly=20one=20identifier,=20so=0AFOREIGN_JOIN(())=20= was=20accepted.=0A=0AReject=20sublists=20with=20fewer=20than=20two=20= relation=20identifiers,=20and=20add=20regression=0Acoverage.=0A=0A= Author:=20Chao=20Li=20=0A---=0A=20= contrib/pg_plan_advice/expected/syntax.out=20|=203=20+++=0A=20= contrib/pg_plan_advice/pgpa_parser.y=20=20=20=20=20=20=20|=202=20+-=0A=20= contrib/pg_plan_advice/sql/syntax.sql=20=20=20=20=20=20|=201=20+=0A=203=20= files=20changed,=205=20insertions(+),=201=20deletion(-)=0A=0Adiff=20= --git=20a/contrib/pg_plan_advice/expected/syntax.out=20= b/contrib/pg_plan_advice/expected/syntax.out=0Aindex=20= 3b57bb2bf57..d53d9598634=20100644=0A---=20= a/contrib/pg_plan_advice/expected/syntax.out=0A+++=20= b/contrib/pg_plan_advice/expected/syntax.out=0A@@=20-209,6=20+209,9=20@@=20= DETAIL:=20=20Could=20not=20parse=20advice:=20FOREIGN_JOIN=20targets=20= must=20contain=20more=20than=20one=0A=20SET=20pg_plan_advice.advice=20=3D=20= 'FOREIGN_JOIN((a))';=0A=20ERROR:=20=20invalid=20value=20for=20parameter=20= "pg_plan_advice.advice":=20"FOREIGN_JOIN((a))"=0A=20DETAIL:=20=20Could=20= not=20parse=20advice:=20FOREIGN_JOIN=20targets=20must=20contain=20more=20= than=20one=20relation=20identifier=20at=20or=20near=20")"=0A+SET=20= pg_plan_advice.advice=20=3D=20'FOREIGN_JOIN(())';=0A+ERROR:=20=20invalid=20= value=20for=20parameter=20"pg_plan_advice.advice":=20"FOREIGN_JOIN(())"=0A= +DETAIL:=20=20Could=20not=20parse=20advice:=20FOREIGN_JOIN=20targets=20= must=20contain=20more=20than=20one=20relation=20identifier=20at=20or=20= near=20")"=0A=20--=20Tag=20keywords=20used=20as=20alias=20names=20work=20= fine,=20because=20the=20'identifier'=0A=20--=20nonterminal=20accepts=20= all=20token=20types.=0A=20SET=20pg_plan_advice.advice=20=3D=20= 'SEQ_SCAN(hash_join)';=0Adiff=20--git=20= a/contrib/pg_plan_advice/pgpa_parser.y=20= b/contrib/pg_plan_advice/pgpa_parser.y=0Aindex=20= 5811a6e5e56..295f16ad064=20100644=0A---=20= a/contrib/pg_plan_advice/pgpa_parser.y=0A+++=20= b/contrib/pg_plan_advice/pgpa_parser.y=0A@@=20-135,7=20+135,7=20@@=20= advice_item:=20TOK_TAG_JOIN_ORDER=20'('=20join_order_target_list=20')'=0A= =20=09=09=09=09foreach_ptr(pgpa_advice_target,=20target,=20$3)=0A=20=09=09= =09=09{=0A=20=09=09=09=09=09if=20(target->ttype=20=3D=3D=20= PGPA_TARGET_IDENTIFIER=20||=0A-=09=09=09=09=09=09= list_length(target->children)=20=3D=3D=201)=0A+=09=09=09=09=09=09= list_length(target->children)=20<=202)=0A=20=09=09=09=09=09=09=09= pgpa_yyerror(result,=20parse_error_msg_p,=20yyscanner,=0A=20=09=09=09=09=09= =09=09=09=09=09=20"FOREIGN_JOIN=20targets=20must=20contain=20more=20than=20= one=20relation=20identifier");=0A=20=09=09=09=09}=0Adiff=20--git=20= a/contrib/pg_plan_advice/sql/syntax.sql=20= b/contrib/pg_plan_advice/sql/syntax.sql=0Aindex=20= 5af7607db42..de11e8c6fc2=20100644=0A---=20= a/contrib/pg_plan_advice/sql/syntax.sql=0A+++=20= b/contrib/pg_plan_advice/sql/syntax.sql=0A@@=20-73,6=20+73,7=20@@=20SET=20= pg_plan_advice.advice=20=3D=20'/*/*=20stuff=20*/*/';=0A=20--=20Foreign=20= join=20requires=20multiple=20relation=20identifiers.=0A=20SET=20= pg_plan_advice.advice=20=3D=20'FOREIGN_JOIN(a)';=0A=20SET=20= pg_plan_advice.advice=20=3D=20'FOREIGN_JOIN((a))';=0A+SET=20= pg_plan_advice.advice=20=3D=20'FOREIGN_JOIN(())';=0A=20=0A=20--=20Tag=20= keywords=20used=20as=20alias=20names=20work=20fine,=20because=20the=20= 'identifier'=0A=20--=20nonterminal=20accepts=20all=20token=20types.=0A--=20= =0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_651FFEDF-A729-4072-A89C-20A04EF3EFA0--