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 1v1A5G-005BQu-0B for pgsql-general@arkaria.postgresql.org; Tue, 23 Sep 2025 20:57: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 1v1A5E-0086FF-LK for pgsql-general@arkaria.postgresql.org; Tue, 23 Sep 2025 20:57:56 +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 1v1A5E-0086F6-Ap for pgsql-general@lists.postgresql.org; Tue, 23 Sep 2025 20:57:56 +0000 Received: from fhigh-b1-smtp.messagingengine.com ([202.12.124.152]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1v1A5C-0023wZ-0b for pgsql-general@lists.postgresql.org; Tue, 23 Sep 2025 20:57:55 +0000 Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfhigh.stl.internal (Postfix) with ESMTP id 3DDB27A031E; Tue, 23 Sep 2025 16:57:54 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Tue, 23 Sep 2025 16:57:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1758661074; x=1758747474; bh=1lmwKikUwv1u7sueoFnrOW/gSgeCa87usFkM71A3q7w=; b= Dhf18e9LkJI2C66W/kSqoqvWSeF2Nr2+Qcc0oZ8LYAWngUqBM2ao1PJuYg9x1S+a 5K5tFvLk74DAP1QCrXKGsLCh6hdSeIB96KO1nignY32+XauAb/XwzHKRNEWmmeN8 /T5sPTUM5jHtmQJjOr6dfMYHZF2CcTajZjkUJqNVQwISQeJspLtDJr3MbT/37yYM 2bU08XEtIBtH+qNs1IW96d/lazUVbYUR9YjT87JBXhjPPPkhGdap+a5oczf46TcO tHbkbJHPEUfOQAq9ELVO3mtd8YuHsXIx/yL8lXW3GtbgT9UjZjSeSX8mw6W1cvyP AGFHcL6k0Yqtgf658ZZF5A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1758661074; x=1758747474; bh=1 lmwKikUwv1u7sueoFnrOW/gSgeCa87usFkM71A3q7w=; b=CPA3ytBl5zWdthoHz HDNjNUhmaJyQ4vGYetSrhvwMEgcdobrjq6AkiMsW4sjETXvVcHiucEh8bqsj+aWq Q5P7FzZB9AkxbTuVnHEvCOW48yAE48ViHH+S+N7Gu55sU4xzpbUlqvwA1J2kFs9A ck2cdpTR3yDEiEvRsyt6V4vOCsikclK1WjnuhBNJqvEkZECjjbPyc0s7A3TyYO+B hh8jeflJFWAZKB+miH+jIj8dLkiiipfwZjGOibK86TUgJc6tMaZqfC09sQFCxq0p j5FbdYCoeIbHi0R2eGvdURBZjcZcU1ogWwZ3bisLuy7Q1eMzjljh+MNjNzJWG+xS SAJKA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdeiudejhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgrnhcu mfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqne cuggftrfgrthhtvghrnhepleegveekkeekueeigfdtveeileeuhfefudefteekjeffkeej ueejheegheegkedtnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprhgtphhtthhopedvpdhmohguvg epshhmthhpohhuthdprhgtphhtthhopehsrghmuhgvlhhmrghrkhhssehgmhgrihhlrdgt ohhmpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlheslhhishhtshdrphhoshhtgh hrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Sep 2025 16:57:53 -0400 (EDT) Message-ID: <0bed88ec-d205-4202-81fe-d8ad990e1122@aklaver.com> Date: Tue, 23 Sep 2025 13:57:53 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: How do I upsert depending on a second table? To: Samuel Marks , pgsql-general@lists.postgresql.org References: Content-Language: en-US From: Adrian Klaver In-Reply-To: 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 9/23/25 13:36, Samuel Marks wrote: > Attempt: > ```sql > CREATE TABLE org > ( > "name" VARCHAR(50) PRIMARY KEY, > owner VARCHAR(50) NOT NULL > ); > > CREATE TABLE repo > ( > "id" INTEGER PRIMARY KEY, > full_name VARCHAR(255) UNIQUE NOT NULL, > org VARCHAR(50) NOT NULL REFERENCES org ("name") > ); > > INSERT INTO org(name, owner) VALUES ('org0', 'user0'); > > INSERT INTO repo (id, full_name, org) > VALUES (0, 'org0/name0 by wrong user', 'org0') > ON CONFLICT (full_name) DO UPDATE > SET full_name = EXCLUDED.full_name, > org = EXCLUDED.org > WHERE EXISTS (SELECT 1 > FROM org org_tbl > WHERE org_tbl.name = EXCLUDED.org > AND org_tbl.owner = 'wrong user') > RETURNING *; > > SELECT * FROM repo WHERE id = 0; > ``` Also, as shown, there is no conflict so I don't see the condition being run per: https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT " condition An expression that returns a value of type boolean. Only rows for which this expression returns true will be updated, although all rows will be locked when the ON CONFLICT DO UPDATE action is taken. Note that condition is evaluated last, after a conflict has been identified as a candidate to update. " > > This all succeeds. It should fail because the 'wrong user' is trying > to create a new—or update an existing—repo. > > Thanks for all suggestions > > -- Adrian Klaver adrian.klaver@aklaver.com