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 1u9iof-005Y1d-Ri for pgsql-general@arkaria.postgresql.org; Tue, 29 Apr 2025 11:07:58 +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 1u9ioc-006QDF-OO for pgsql-general@arkaria.postgresql.org; Tue, 29 Apr 2025 11:07:55 +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 1u9ioc-006QCV-9R for pgsql-general@lists.postgresql.org; Tue, 29 Apr 2025 11:07:55 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u9ioa-000AKo-2G for pgsql-general@lists.postgresql.org; Tue, 29 Apr 2025 11:07:53 +0000 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-736c3e7b390so5882707b3a.2 for ; Tue, 29 Apr 2025 04:07:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wikimedia.org; s=google; t=1745924872; x=1746529672; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=9nMqtNRg72UZZmAzkdUxjvzntmZrOBvuKs1IhbHD1S4=; b=Isd7mdkkPJyX5c4DL3C9DtyQBcij3j9fXulOKUFe+v2UkT4wpOLTAUKqtnyXJeKlbg gOYGKuGRepw2Zi7AcEHUJFbcT66EZf5qAcarjxt/OUeT2KMaq0PyigtJssNlQ+/Pf+cE hST9AZ+tCgZbyg0PqbfGCvgecuYp/LI45yS9o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745924872; x=1746529672; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9nMqtNRg72UZZmAzkdUxjvzntmZrOBvuKs1IhbHD1S4=; b=QSYsO9aj606ofRISpHl1gM5Y1lTlnH7B6WHO4IxbmvmiZE4A3ckeHzauaD9bjgF4qn fD9Sigx7A3q5tHe0SNeB92ZKgcESNPEIftp9vV0yXFop07Lq1swcIAJfNj5AMYxIMekf CoWHkn3tor9rHv7NPbrbo8GzKivDV+nx+inEGPilaUoWLhOZLRwhT8g8/tlLRS6nIoYO heN96gVI1ybkT3YKmtlpIJLCwSJsW5QULx5AsogixzYDDFXJW1TkCsXPXX957AiNH9+p ClCQKVUxxYsxB7LVvpzbN3pIblzJoy0QNI5u29ITzqJBUvSBrHdmUFbXrkPbIc5lhLMa bBVg== X-Gm-Message-State: AOJu0YzPia4cfbB7w//HBxPxeLCSFr3rOfDsONKTqFE/ofC+GLX8Ib3T E82r5DBrl2BQ0mxoqQWISTx+fzIGIlJAr6mb/OOyM1b/H0W5cfHAzq0/MiVlCphSoqIstkjRCI+ MO94= X-Gm-Gg: ASbGncvp81MrM10zsYURWDyQm62ovqgruT9OVSa3Uc9NJLunIbeV4au+lz3+rs1UVTq jGugH0HA71/ATS8jULwzerSuQSoxsrQXBFOcesroWot3xq9Gci3S902iaPieA/9YZ1BC7TrHY86 /bQfHG4/hKFKdr1yhLk+GcCg8Vdhv46pLE9P+AQvbrVWKlevfUqKSUpTW/05l9iwWizN7x/kWnC 0bMw/yKsW2lHG2RtkR/tLGjAbsytOZIeyhdc/FQ0pkbsfW6BW6FwVqqkcSLMfdma89qc1/JVKf7 zwpxct2gQUTi2Y4DpUkX1DQpAIEntJWtyBMsMtgam9cw1F6TuTC428aZirAtU2WaCsVixiSrgRd lt10= X-Google-Smtp-Source: AGHT+IE0FBKsGccVO0Gn4r178cIGJHRju7zzFGvrl7J8+KQBPtH7YdBgYjj3iQz5KmnBV5RjweL0hw== X-Received: by 2002:a05:6a00:928e:b0:73e:30e0:8a2a with SMTP id d2e1a72fcca58-740271df88bmr3954649b3a.17.1745924872087; Tue, 29 Apr 2025 04:07:52 -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 d2e1a72fcca58-74022ee1fdcsm2299733b3a.15.2025.04.29.04.07.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 29 Apr 2025 04:07:51 -0700 (PDT) Message-ID: Date: Tue, 29 Apr 2025 21:07:47 +1000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Upsert error "column reference is ambiguous" Content-Language: en-US To: Laurenz Albe , Tom Lane Cc: pgsql-general@lists.postgresql.org References: <697017.1745837694@sss.pgh.pa.us> <80f95342-cc09-4236-a2d7-68538fbfc41b@wikimedia.org> <774865.1745848449@sss.pgh.pa.us> <5fe1d1c9-662e-405e-aaed-21d15bdbea06@wikimedia.org> <10ff0fbe7f4d711300951431ad28254aa5bf2dc4.camel@cybertec.at> From: Tim Starling In-Reply-To: <10ff0fbe7f4d711300951431ad28254aa5bf2dc4.camel@cybertec.at> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 29/4/25 16:36, Laurenz Albe wrote: > On Tue, 2025-04-29 at 08:36 +1000, Tim Starling wrote: >> My code would be like >> >> function upsert( $table, $names, $values, $key, $set ) { >>      if ( $this->type === 'mysql' ) { >>          $conflict = 'ON DUPLICATE KEY UPDATE'; >>      } else { >>          $conflict = "ON CONFLICT ($key) DO UPDATE SET"; >>      } >>      return $this->query( "INSERT INTO $table ($names) " . >>          "VALUES ($values) $conflict $set" ); >> } >> >> The parameters are a little bit more structured than that, but that >> gives you the idea. > > Another litle "if" to cater for PostgreSQL's "EXCLUDED." would be > such a big problem? I don't understand what you mean. EXCLUDED is not needed. "$table." needs to be prefixed to every column reference in the string $set. How do you find the column references amongst the string literals, function calls, etc.? You would need to parse the expression. This is a public interface and there may be callers in code that I don't have access to. Part of the reason for wanting to replace the existing emulation with a native upsert is to simplify the code. Parsing the expression is definitely not a simplification. -- Tim Starling