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 1u9kJE-005rSF-FW for pgsql-general@arkaria.postgresql.org; Tue, 29 Apr 2025 12:43:36 +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 1u9kJB-006txS-DI for pgsql-general@arkaria.postgresql.org; Tue, 29 Apr 2025 12:43:34 +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.94.2) (envelope-from ) id 1u9kJB-006txK-2Z for pgsql-general@lists.postgresql.org; Tue, 29 Apr 2025 12:43:34 +0000 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u9kJA-000BM8-10 for pgsql-general@lists.postgresql.org; Tue, 29 Apr 2025 12:43:33 +0000 Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-54b0d638e86so7039881e87.1 for ; Tue, 29 Apr 2025 05:43:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745930610; x=1746535410; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=AdKv29aNX2b1jPrsoWzY8V08AK8foALGBqcdrvTXxtU=; b=Ix/dT3T13qtI07vGoVv9we/bo7HHECTRVKwkFdkU35kPLvkM8ljrlBuYRqqQZPGSnh uim6YImqUcv/wmYxu+wsP3c94slRiwAdVfYjYNXyyJK3nmKp9Z81k2qFWbIuDx4K96G5 F/qzst1S5f6Yg9l1JdVYph0cBHnxAoFOxH3Zryqzn8SDUA3TImDCt9gSoTrPfftwbUR4 wWXap6C+4V9RLmrwmgdN897J41qxkDLCZaFGxqkn03j//nXeQQ+SYPK47xsFtg3uU0pc BBYQmhTLyH1muMij36dXKumHqlRpnqvMlkgTlB4ugKSHjyV62g5/khSPnv0Y3MsqF/kb z6Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745930610; x=1746535410; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AdKv29aNX2b1jPrsoWzY8V08AK8foALGBqcdrvTXxtU=; b=QIq63voniFS+b9FbsXXSG8sRfokMVBXjssv5JRgmVpsNu9E5h8nbT47g4JWl7//QSY msTsbCu1/YQsvinRxQeSgn4eU3HexM34DZFlllIaAmwPzwCBlhLydkeqjYA7MfuIrDgg ve57gaInnaJ7sbb8bPoQdL8/BmS/TGaRVL70Eym21fRSl+HEA+IyKUC31qGmZHey75uD J/rpXWsH4fmuJOljz23QbKX82ztgbjc6GvSJ2AfdO8A2dAYP4RWyqXU+h1Jui1Ow9KIX EM05apzNx9CMj+BHBmKDgLpMUMygNrz53J/VJY3huUf2PsI8q1OwTx6/HB2mIDsNbYC0 9jog== X-Forwarded-Encrypted: i=1; AJvYcCWeBARWR1XnqqnrhYHjZqiLOJAjsFSOkMuCMixXYfcJe37sGXfLuo+fw39MhHl7UMXpqA8yvR556rdHk4U4@lists.postgresql.org X-Gm-Message-State: AOJu0Yxg0S4VT+kAPgi4xoxVKUUfxdNRwtt3w9aao4H7mniCU8VTMJ+x zscaqXQry/h7maYKnYMofpzgF/hFA1PcKdyFUvGIfUA1ANE5xQUmx9RZZzTR/Bi1r+I2CHJZ/VA hGvys5G1QhUHA42vbhA3z3qb2jM9I7W48 X-Gm-Gg: ASbGncuTbDBq3QfzAwJ1YYzzGDHPsqLh27Nemh7uGwF/prh4EnEtXaGGkEjoTtv3cFy 5YshpisODzlKXTCyMGlFP7go7ACLpgnagJyIhljNJwqlLo6W+2mHNUFyMEWdhuaayXcIJfBS47K hssQkjiP3FPLGRG1ioh6jhbRwSrxZfoc34ipb9X5ue3fyUbwodkV6ofcM= X-Google-Smtp-Source: AGHT+IE12wdVSqm68PQ/sm/fNITuyyFCB4SVNn8QKLi1lLeiq6z6Pg+k8KI2HRhpKuXq7aF6mVNljZOJBCA+ZbyVlSE= X-Received: by 2002:a05:6512:695:b0:545:353:4d46 with SMTP id 2adb3069b0e04-54e8ffdae48mr3009892e87.25.1745930609630; Tue, 29 Apr 2025 05:43:29 -0700 (PDT) MIME-Version: 1.0 References: <697017.1745837694@sss.pgh.pa.us> <80f95342-cc09-4236-a2d7-68538fbfc41b@wikimedia.org> <774865.1745848449@sss.pgh.pa.us> In-Reply-To: <774865.1745848449@sss.pgh.pa.us> From: David Rowley Date: Wed, 30 Apr 2025 00:43:17 +1200 X-Gm-Features: ATxdqUGbeZIN6HM77ybRI_O2HkL33WHf4JKTSJbrvGNhzfruhmS1UnCQbEPWXmQ Message-ID: Subject: Re: Upsert error "column reference is ambiguous" To: Tom Lane Cc: Tim Starling , pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 29 Apr 2025 at 01:54, Tom Lane wrote: > I do actually have some sympathy for your proposal after thinking > about it a bit more, but the argument I would use is "the behavior > of the ON CONFLICT UPDATE SET list should be as much as possible like > the behavior of an ordinary UPDATE's SET list". Since "v = v+1" would > refer to the existing row's "v" in regular UPDATE, it's sensible to > let that happen here too. Of course the counter-argument is that this > should be compared not to a trivial UPDATE, but an "UPDATE ... FROM > othertable" where the othertable supplies some conflicting column > name(s). In that situation we're going to make you resolve the > conflict by qualifying the column names. The only thing that makes > that not a precise parallel is that EXCLUDED is not something the user > wrote into the query explicitly, so there's no opportunity to > substitute different column aliases, as a FROM clause would allow. > Perhaps that justifies demoting it to second-class citizenship whereby > EXCLUDED has to be qualified but the target table doesn't. (I don't > find this argument hugely compelling, but it's an argument.) Not arguing for or against, but... I think there are some cases where it would be more dangerous to relax this. Here's one case where not qualifying the column can be dangerous: create table a1 (a int); insert into a1 values(1),(2); create table a2 (a int); insert into a2 values(1); select * from a1 where a in(select a from a2); -- as expected. -- application changes, a2.a isn't needed anymore. column gets dropped but someone forgets to update a query in the app... alter table a2 drop column a; select * from a1 where a in(select a from a2); -- silently returns unexpected results. If the original author of that query had been thoughtful enough to qualify the column in the subquery then someone would probably have gotten an error and fixed it. The moral of that story is that sometimes forcing the query author to qualify the column is a good idea. (not that there's much we can do about that one...) Now the question is, do any similar hazards exist with ON CONFLICT DO UPDATE? I don't think so as any columns being dropped will disappear from the insert target table and the EXCLUDED work table at the same time. Another thought is that you can have an UPDATE with a RETURNING clause. An unqualified column defaults to NEW even though you could argue it's ambiguous due to OLD (as of 80feb727c). Likely we were forced into making it work that way through not wanting to force everyone to rewrite their RETURNING statements when upgrading to v18. The moral of that story is, UPDATE isn't exactly consistent already about when it requires column qualifications. Maybe it's weird to insist that users qualify columns with their ON CONFLICT UPDATE SET when RETURNING is happy to assume you must have meant NEW. David