Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lVGTa-0008Oz-AG for pgsql-bugs@arkaria.postgresql.org; Sat, 10 Apr 2021 16:28:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lVGTY-0007Rb-S8 for pgsql-bugs@arkaria.postgresql.org; Sat, 10 Apr 2021 16:28:48 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lVGTY-0007RU-ME for pgsql-bugs@lists.postgresql.org; Sat, 10 Apr 2021 16:28:48 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lVGTS-0003ne-B9 for pgsql-bugs@lists.postgresql.org; Sat, 10 Apr 2021 16:28:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 13AGScMM2364237; Sat, 10 Apr 2021 12:28:39 -0400 From: Tom Lane To: Vik Fearing cc: Peter Geoghegan , Pantelis Theodosiou , Lukas Eder , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16958: "Invalid reference to FROM-clause entry for table" when qualifying columns in "on conflict .. where" In-reply-to: <8a396e1e-c2a0-5ba3-180e-697082ae397f@postgresfriends.org> References: <16958-963f638020de271c@postgresql.org> <8a396e1e-c2a0-5ba3-180e-697082ae397f@postgresfriends.org> Comments: In-reply-to Vik Fearing message dated "Sat, 10 Apr 2021 12:28:04 +0200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2364211.1618072075.0@sss.pgh.pa.us> Date: Sat, 10 Apr 2021 12:28:38 -0400 Message-ID: <2364236.1618072118@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2364211.1618072075.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Vik Fearing writes: > On 4/10/21 9:57 AM, Pantelis Theodosiou wrote: >> On Fri, Apr 9, 2021 at 11:00 PM PG Bug reporting form < >> noreply@postgresql.org> wrote: >>> SQL Error [42P01]: ERROR: invalid reference to FROM-clause entry for t= able "t" >>> Hint: There is an entry for table "t", but it cannot be referenced fro= m >>> this part of the query. >>> = >>> Notice the qualification of the t.b column in the "on conflict .. wher= e" >>> clause. I don't understand why b cannot be qualified at this location.= It >>> can be qualified in the index definition, looks like a bug to me. >> You don't need and shouldn't prefix the column with the table name in t= hat >> part. This should work: > Not needing to and not being able to are two completely different > things. I believe tablename qualification should be allowed here even > if it isn't necessary. It does seem like a pointless prohibition, but the comment about it in the source code implies it was intentional. Peter, do you remember why? Anyway, if the prohibition isn't necessary, the attached one-liner gets most of the way to removing it. I'm not quite satisfied with this though: regression=3D# create table t(a int primary key, b int, c int); CREATE TABLE regression=3D# insert into t (a,b) values(1,1) on conflict(a) where excluded.b is null do update set c=3Dexcluded.c+1; ERROR: missing FROM-clause entry for table "excluded" LINE 2: where excluded.b is null do update set c=3Dexcluded.c+1; ^ Seems like we want the same cannot-be-referenced HINT here as in the complaint, so just nuking the whole namespace as the code currently does ought to be revisited. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="allow-table-qual-in-ON-CONFLICT-wip.patch"; charset="us-ascii" Content-ID: <2364211.1618072075.2@sss.pgh.pa.us> Content-Description: allow-table-qual-in-ON-CONFLICT-wip.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_= clause.c index af80aa4593..4402be6845 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -3225,13 +3225,13 @@ transformOnConflictArbiter(ParseState *pstate, List *save_namespace; = /* - * While we process the arbiter expressions, accept only non-qualified - * references to the target table. Hide any other relations. + * While we process the arbiter expressions, accept only references to + * the target table. Hide any other relations. */ save_namespace =3D pstate->p_namespace; pstate->p_namespace =3D NIL; addNSItemToQuery(pstate, pstate->p_target_nsitem, - false, false, true); + false, true, true); = if (infer->indexElems) *arbiterExpr =3D resolve_unique_index_expr(pstate, infer, ------- =_aaaaaaaaaa0--