Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j4u3B-0001lY-I8 for pgsql-general@arkaria.postgresql.org; Thu, 20 Feb 2020 22:12:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j4u3A-0007Rt-Aq for pgsql-general@arkaria.postgresql.org; Thu, 20 Feb 2020 22:12:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j4u39-0007Rl-U8 for pgsql-general@lists.postgresql.org; Thu, 20 Feb 2020 22:12:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j4u37-0001v6-NL for pgsql-general@postgresql.org; Thu, 20 Feb 2020 22:12:02 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 01KMBwdU027386; Thu, 20 Feb 2020 17:11:58 -0500 From: Tom Lane To: Ted Toth cc: Adrian Klaver , pgsql-general Subject: Re: before insert for each row trigger on upsert In-reply-to: References: <18def70d-c2c4-ff92-b488-243ed0287001@aklaver.com> Comments: In-reply-to Ted Toth message dated "Thu, 20 Feb 2020 15:03:04 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27383.1582236718.1@sss.pgh.pa.us> Date: Thu, 20 Feb 2020 17:11:58 -0500 Message-ID: <27384.1582236718@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ted Toth writes: > I think you are right in that the trigger doesn't change the row being > updated (which is good) but it also updates another table and that's what > I'd actually like to avoid on update. Probably need to put that in an AFTER trigger, then. By definition, BEFORE triggers don't really know whether the insert is going to happen. This doesn't even require use of upsert to cause a problem: a later BEFORE trigger could prevent the insertion, or change what gets inserted. regards, tom lane