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.94.2) (envelope-from ) id 1u9M9Y-000t4M-G1 for pgsql-general@arkaria.postgresql.org; Mon, 28 Apr 2025 10:56:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1u9M8W-0005EW-Dp for pgsql-general@arkaria.postgresql.org; Mon, 28 Apr 2025 10:54:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u9M8W-0005EO-2m for pgsql-general@lists.postgresql.org; Mon, 28 Apr 2025 10:54:57 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9M8U-002PLN-2z for pgsql-general@lists.postgresql.org; Mon, 28 Apr 2025 10:54:56 +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 53SAss0f697018; Mon, 28 Apr 2025 06:54:54 -0400 From: Tom Lane To: Tim Starling cc: pgsql-general@lists.postgresql.org Subject: Re: Upsert error "column reference is ambiguous" In-reply-to: References: Comments: In-reply-to Tim Starling message dated "Mon, 28 Apr 2025 14:55:58 +1000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <697016.1745837694.1@sss.pgh.pa.us> Date: Mon, 28 Apr 2025 06:54:54 -0400 Message-ID: <697017.1745837694@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tim Starling writes: > Regarding upsert syntax. > => INSERT INTO t VALUES (1,1) ON CONFLICT (k) DO UPDATE SET v=v+1; > ERROR: column reference "v" is ambiguous > Please convince me that this is not a bug. It's not a bug. > If I understand correctly, in the expression "v+1", both EXCLUDED.v > and t.v are present as the unqualified name "v". This is always the > case and it is never possible to reference an unqualified field name > in the expression of a conflict action. Correct: it's not clear whether you mean to use "v" from the new desired-to-be-inserted row or "v" from the existing row. > Thus, any query with an unqualified name is statically known to be > invalid. It is not a b/c break to remove EXCLUDED.v from the list of > unqualified fields in a new major release of PG, thus allowing it to DWIM. Even if I were on board with arbitrarily adopting one of the two possible interpretations, it's far from obvious to me that most people would agree that "v" should mean the value from the existing row, rather than the new value. Better to make them say which they want. regards, tom lane