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 1v1YW9-009ukh-1i for pgsql-general@arkaria.postgresql.org; Wed, 24 Sep 2025 23:03:21 +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 1v1YW7-00GFZS-P1 for pgsql-general@arkaria.postgresql.org; Wed, 24 Sep 2025 23:03:19 +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 1v1YW6-00GFYa-NW for pgsql-general@lists.postgresql.org; Wed, 24 Sep 2025 23:03:19 +0000 Received: from fhigh-b1-smtp.messagingengine.com ([202.12.124.152]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v1YW1-002hrW-1q for pgsql-general@lists.postgresql.org; Wed, 24 Sep 2025 23:03:18 +0000 Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfhigh.stl.internal (Postfix) with ESMTP id 78DD37A0106; Wed, 24 Sep 2025 19:03:12 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Wed, 24 Sep 2025 19:03:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc: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=1758754992; x=1758841392; bh=+w6yEn6EwIQmg0GrcZp3waTZbv/H25okZ3kfrX8w1lo=; b= 2RUAdtMHb1UiYDn834uQrg3XWqE6VSYmIctFmbbiLxBsYYQf40jkbKCzeW79ASJV bx6V/u6AdSjiKmV3isGBxFSjW5+GmM179oX1cW4BsJJx5teNQ2yDtWMuhjNk+P5C xB10fzg0t9uUEhHKMOKK1ooIjkcp+WrCpnbtfbosKTSqbsq4J0Kn88B1k6odOh2T E+q646WPzZreIm1vRBZV5MMltEmiS9B+er6rVEULiv9nC1yt4qp9hcTTJbTjMjqO w2SQJPYuOZY/ytUTyf+VIQpJgbYG8m0HCLbG34Y/6gQXrp2fLhDAXcl+ho0cBv8e BIQCg3vms4Iho6h4yZUahA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1758754992; x= 1758841392; bh=+w6yEn6EwIQmg0GrcZp3waTZbv/H25okZ3kfrX8w1lo=; b=E UKUREu/eAXDzjEfHd4N/5QgKta/dHBiYQDR1y/4ZTZRJDA8xYpQ9KTu45MhkIhyX 2pF6xx6Kce3cE88JmpokcwNmj9h0cHq/m06U8bJ0aQDwnxaXEOYQOuff6eHstJcI U/0wYIicnfZTJE8T7d5aN76fAUdXJdZNRksyrUqhWSuJZo5WiJUE6Xm19NK/x3m1 xo1265KZUpyWtHxWYGJ0Hd7AnqYk4G7zGb+OBhWiBHQuE8I3bjNyhPsIN19HE9wc HhjzLHZAiV+ro5VNW3/bOjEO+ZHOwWbqhaOitY+y8tdLyLNIDTOnUiQF4NxnyZSQ kf54r4PwOKnDi1iwpiYrQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdeigeekkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefkffggfgfuvfevfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghn ucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqe enucggtffrrghtthgvrhhnpefgudefkefhveefleevieeuveehvdduudekuddvvdelhfeu ueeijedtuedvvedvueenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgnecuve hluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghn rdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnhgspghrtghpthhtohepfedpmhhoug gvpehsmhhtphhouhhtpdhrtghpthhtohepshgrmhhuvghlmhgrrhhkshesghhmrghilhdr tghomhdprhgtphhtthhopehrohgurhhighhosghurhhgohhsmhgvlhhlrgesghhmrghilh drtghomhdprhgtphhtthhopehpghhsqhhlqdhgvghnvghrrghlsehlihhsthhsrdhpohhs thhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 24 Sep 2025 19:03:11 -0400 (EDT) Message-ID: <22052a80-6461-4bc3-b75b-7b1063982101@aklaver.com> Date: Wed, 24 Sep 2025 16:03:10 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: How do I upsert depending on a second table? To: Samuel Marks Cc: Juan Rodrigo Alejandro Burgos Mella , pgsql-general References: <8f2e4644-eccd-49e8-b70b-b5c51c3aaa8c@aklaver.com> 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/24/25 10:02, Samuel Marks wrote: > On Wed, Sep 24, 2025 at 10:13 AM Adrian Klaver > Yes but it's meant to divide by zero. That cancels the whole transaction > stopping it from going through. It being a transaction lets me guarantee > that at point of update or insert [upsert] the org owner matches the > requestor. > > I would preference a single statement (one semicolon) solution; but for > now at least this works 🤷 I don't have enough experience with below to come up with an off the top of my head examples, but they look like they may offer alternatives. MERGE: https://www.postgresql.org/docs/current/sql-merge.html and/or Row level Security: https://www.postgresql.org/docs/current/ddl-rowsecurity.html In above link see example that starts below the phrase: "... If it is necessary to consult other rows or other tables to make a policy decision, that can be accomplished using sub-SELECTs, or functions that contain SELECTs, in the policy expressions. ... " -- Adrian Klaver adrian.klaver@aklaver.com