Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kvNHs-0007mC-Ng for pgsql-general@arkaria.postgresql.org; Fri, 01 Jan 2021 16:28:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kvNHL-0002tS-AQ for pgsql-general@arkaria.postgresql.org; Fri, 01 Jan 2021 16:27:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kvNHL-0002tK-2t for pgsql-general@lists.postgresql.org; Fri, 01 Jan 2021 16:27:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kvNHI-00034I-Vz for pgsql-general@lists.postgresql.org; Fri, 01 Jan 2021 16:27:50 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 101GRlfN2524882; Fri, 1 Jan 2021 11:27:47 -0500 From: Tom Lane To: Christophe Pettus cc: Dirk Mika , "pgsql-general@lists.postgresql.org" Subject: Re: Trigger with conditional predicates In-reply-to: <7097609B-DCC8-49B8-932C-35A3D7D6A405@thebuild.com> References: <7097609B-DCC8-49B8-932C-35A3D7D6A405@thebuild.com> Comments: In-reply-to Christophe Pettus message dated "Fri, 01 Jan 2021 08:05:26 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2524880.1609518467.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 01 Jan 2021 11:27:47 -0500 Message-ID: <2524881.1609518467@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Christophe Pettus writes: > PostgreSQL doesn't have an exact equivalent. Typically, the OLD and NEW= values are compared and then action is taken based on that. For example,= in PL/pgSQL: > IF NEW.is_canceled IS NOT DISTINCT FROM OLD.is_canceled THEN > NEW.is_canceled :=3D etc etc ; > ENDIF; > There's currently no way to detect if the column was simply not mentione= d at all in the UPDATE statement. That's not completely true: you can make the whole trigger firing dependent on that, by writing something like CREATE TRIGGER tgname BEFORE UPDATE OF column_name [, ... ] ON table ... and then the trigger won't fire if the column is not mentioned. This is not without downsides though: * If you've got several columns of concern, this would lead you to write a separate trigger for each one, and maybe another trigger for unconditional actions. My gut feel is that the trigger firing overhead is enough to make this less performant than one trigger with IF-conditions. I could be wrong though, never measured it. * When dealing with multiple triggers, you need to keep firmly in mind that the filter condition is whether the *original SQL text* listed the column as an update target. You can't tell this way whether some earlier trigger changed the column's value. regards, tom lane