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 1u9GYJ-00HTZj-OF for pgsql-general@arkaria.postgresql.org; Mon, 28 Apr 2025 04:57:12 +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 1u9GXJ-00BvqF-LF for pgsql-general@arkaria.postgresql.org; Mon, 28 Apr 2025 04:56:10 +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 1u9GXJ-00Bvq7-4T for pgsql-general@lists.postgresql.org; Mon, 28 Apr 2025 04:56:10 +0000 Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u9GXE-002QwU-1T for pgsql-general@lists.postgresql.org; Mon, 28 Apr 2025 04:56:08 +0000 Received: by mail-pj1-x1041.google.com with SMTP id 98e67ed59e1d1-309d2e8c20cso5266646a91.0 for ; Sun, 27 Apr 2025 21:56:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wikimedia.org; s=google; t=1745816163; x=1746420963; darn=lists.postgresql.org; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=Szt45aQQ1zJN6KuIjDFWiSc5tDf/VrKBME872/LVZ7k=; b=M1QVMKUtniOxGQPX+F5z1X3VKiP9tLJ/NpK35AooTVbrBqslP94d4Cl0ngDxgPZI/M DlJ9hVxcIG7eEaelr5fy0P54JengkJ1/TxeQ0KlLT2SSRM2z5H1gCLhwlmF0LR3nyptm YQa7ez4FQ/4W42rwjk9XP2piLvzmvomigbSzI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745816163; x=1746420963; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Szt45aQQ1zJN6KuIjDFWiSc5tDf/VrKBME872/LVZ7k=; b=hFVOApvcbD/tMm+iZtLw9ycjiZbF/qxF5nhVbYl4KB0MhXmzYQaC6mdfo+ieyVHHdi Ey0yjcBSHzJZ//JbSHask9dSByVOAaHT7THBR/GhqxEwk1eoH8VRCqpDN0jEIFQieKyj Rvyod992LfGjbIvPZC+LAnFqlPFf8bSuY424HloIEUR3TicmXUzb9BUw5JcY8pJ+L8xI fzTn5/g1QwlPpaB+HUHZT1na/kLLvFuyByDGJw09ft4sx7ccdcJUZC+5S4f4u0WIzTUH 2FX9FePH0FIrtPDiFhFzZliMI5JrkAAJL82u+w5DAVAM4jE6QkK6muyDJmpm5DZWtSvD 6gSQ== X-Gm-Message-State: AOJu0Yzz5wXsETF0meyMf0e7JJjsoYDZFuFj9ItOjuHd9jTyhtEdvzG6 evf6sw9osbHNBaQJZ+kI3PQRfBNZWA3hapAAqA/TccX54Cph4hAwHnqxMl9C7DID7zfGKRAYwEy lwX1sFw== X-Gm-Gg: ASbGncvifxSKITGYzVKnwGa7C1tSx/mrrTdWMZCL6aqdDmcV6NMyX+eCaaHGb0Eunn0 9RyZTjpm6IKq6hLH83INzO3e9szThnlWs+mgSz2sJcPD1RxZIJVava/pRRCxqZ+kOXHhQHaCbAe ScWLJlS5MU1CUg7N6UdUONaAsQSiVY3Kz/Tc+bA8ieeHa08RrF+MPt7uyt2RZlqVasA71Ta3NvP Vn3/photHYhF2KV6kEJbubrorBICKDrNDzDyBb4baLd5WfrlvyISm3084oBm2AlZf2rxKHvH1zG qEDQcesNtqGexmkYGOhNE9qd0WuTrJlpqG2QRnDUyooQZI1VCWNmSzYtzeJyQOgIEAMd+MV911I kdQE= X-Google-Smtp-Source: AGHT+IGGc4jfeAt2dEOYJTF7CHRZBcBcmA6TIc9oxf6zvS7NkqEWUpMgT0IxsufKavCFjVA971yZ+A== X-Received: by 2002:a17:90b:58ce:b0:2ff:53ad:a0ec with SMTP id 98e67ed59e1d1-309f7e04e7dmr13629963a91.21.1745816162619; Sun, 27 Apr 2025 21:56:02 -0700 (PDT) Received: from [10.1.1.45] (124-168-128-17.dyn.iinet.net.au. [124.168.128.17]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-309f77417d6sm6086590a91.4.2025.04.27.21.56.01 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 27 Apr 2025 21:56:02 -0700 (PDT) Message-ID: Date: Mon, 28 Apr 2025 14:55:58 +1000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: pgsql-general@lists.postgresql.org From: Tim Starling Subject: Upsert error "column reference is ambiguous" Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Regarding upsert syntax. psql (16.8 (Ubuntu 16.8-0ubuntu0.24.04.1), server 14.13 (Ubuntu 14.13-0ubuntu0.22.04.1)) => CREATE TABLE t (k INTEGER, v INTEGER); => CREATE UNIQUE INDEX t_k ON t (k); => INSERT INTO t VALUES (1,1); INSERT 0 1 => 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. 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. 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. I'm a maintainer of MediaWiki. Some kind person contributed PostgreSQL support many years ago so now I am required to maintain it in perpetuity. The work seems out of proportion to the benefit, but that's the industry I guess. A handful of users benefit, such as wiki.postgresql.org. Our application has an upsert method which takes the assignment "v=v+1" as a string. It is feasible to split it on the equals sign into the destination field and expression components, but it is not feasible to parse the expression or to require callers to supply an AST tree for the expressions they give us. It is not feasible to require callers to prefix all field names with the table name. We currently emulate upsert on PostgreSQL using several awkward and inefficient queries. It would be nice to be able to use PostgreSQL's native upsert feature. But unless someone here has an idea for a workaround, I think this field name resolution policy is a total blocker. We can implement upsert on MySQL and SQLite but on PostgreSQL it will remain emulated. -- Tim Starling