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 1kssXN-0004mm-4w for pgsql-general@arkaria.postgresql.org; Fri, 25 Dec 2020 19:14:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kssXL-0004IY-QZ for pgsql-general@arkaria.postgresql.org; Fri, 25 Dec 2020 19:14:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kssXK-0004IR-EH for pgsql-general@lists.postgresql.org; Fri, 25 Dec 2020 19:14:03 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kssXC-0007z9-Kp for pgsql-general@lists.postgresql.org; Fri, 25 Dec 2020 19:14:01 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id D85097F4; Fri, 25 Dec 2020 14:13:50 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Fri, 25 Dec 2020 14:13:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=6 tjFOoyhJRjeKXUZyl83yl4x1z4weNyv2aCsN/HFl70=; b=aBE2qJO+YW68Tj0s2 w410yge7oicfTOEZJQtEU0/R5RctTpN8TKsK+qUiw4bdjpGkDe8l3nDyDoejhZHq KoC5a8fE8GpYtFU5G3Fu+yYsuxAjsvPbbdSmE14/K23/K8qs6gaCRZREvUTKshDb hAKgAb/uNOR8IWi3jA16FounRSK5TZVCw29r6TH/+2iFp7e3bl+O+DDG4I6dkVcE bA5cMgw/YrFUcOf5avdB3O1S3XhJDeT/CXsInkNxsSkKuUlrP+vd68KsbvJ4i1RZ 0IUO2pxlg9lie/Kpqc5P8ISR0PJqq00Zc3L/sf9CL/t6IWtgRxhoejmUj0AimC+U k4z2A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=6tjFOoyhJRjeKXUZyl83yl4x1z4weNyv2aCsN/HFl 70=; b=DWe3d0qiLn9Q9bmB1iDedt6RVq4Hb96Q7IKpBx0wuJMtpn6ugeEyNyr2y AlNYFgp9JVc7IQkclUEhXRLeagbwsSlsbeGZzGeZ/Mg9zxBUm2p5LacaBrZITS4k QqWPbYb4WcG4SOpCO8tq49f90t5wtyZtA2Mg/MGvIRw5qtI80mMrbMY1JZWnz3Ek pR9FUQ3IFrUyXwc91txBGEvAHG4f5SptdP9qCl4dO16Y7WDAWpchjjEsDBHKQZrQ JwOJWfEgsLI4yAvIjDlKEQfxlq6lRjJYVRELEnNFKxx76b5nIDzL5Y+uDHNzM5T5 yhD6Qm3l+lVEzWs+UqsdVjxSrqMjw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvdduuddguddvvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepheevfeekgeekudeiteetffeivedutdethfethedv tddviefhheeftdekkedtffehnecuffhomhgrihhnpegthigsvghrthgvtgdqphhoshhtgh hrvghsqhhlrdgtohhmpdhvihhsvghnrgdrtghomhenucfkphepjeehrddujedvrddugedr udejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprg gurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.4] (75-172-14-17.tukw.qwest.net [75.172.14.17]) by mail.messagingengine.com (Postfix) with ESMTPA id 1791824005A; Fri, 25 Dec 2020 14:13:49 -0500 (EST) Subject: Re: How to specify that a trigger should fire when column is NOT in SET-clause? To: Andreas Joseph Krogh Cc: pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: Date: Fri, 25 Dec 2020 11:13:48 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/25/20 10:19 AM, Andreas Joseph Krogh wrote: > På fredag 25. desember 2020 kl. 17:48:39, skrev Adrian Klaver > >: > > On 12/25/20 5:52 AM, Andreas Joseph Krogh wrote: > > Hi. > > I need to set a value in a trigger if a column is explicitly NOT > > specified in UPDATE's SET-clause. > > Like for example having a "BEFORE UPDATE OF NOT" > > > > create TRIGGER my_trigger > >      BEFORE UPDATE OF NOT modified ON my_table FOR EACH ROW WHEN > (OLD.val <>NEW.val) > > EXECUTE PROCEDURE do_stuff(); > > > > I want the trigger to be fired when the column "modified" is NOT > > specified, is it possible? > > It will always be specified, it may or may not be changed. As example: > > True, but what I'm after is using the value from the "modified" column, > if specified, else use CURRENT_TIMESTAMP > My use-case is this; > I have this table: > > create table person ( > id serial primary key, > username varchar not null unique, > password varchar not null, > credentials_last_updated timestamp NOT NULL default CURRENT_TIMESTAMP, > created timestamp NOT NULL default CURRENT_TIMESTAMP, > modified timestamp ); > > Then this trigger to update "credentials_last_updated" whenever "password" is modified. > > create or replace FUNCTION person_password_updated_tf()returns TRIGGER AS $$ BEGIN NEW.credentials_last_updated =NEW.modified;-- OR CURRENT_TIMESTAMP if "modified" isn't specified RETURN NEW; > END; > $$ LANGUAGE plpgsql; > > create TRIGGER person_password_updated_tBEFORE UPDATE OF password ON onp_user FOR EACH ROW WHEN (OLD.password <>NEW.password ) > EXECUTE PROCEDURE person_password_updated_tf(); > > So, I want to set "credentials_last_updated to NEW.modified if > "modified" is specified, else to CURRENT_TIMESTAMP Because of this: Is UPDATE the same as DELETE + INSERT in PostgreSQL? https://www.cybertec-postgresql.com/en/is-update-the-same-as-delete-insert-in-postgresql/ you will always get a NEW.modified. There is no way(AFAIK) to determine whether the value came from a SET or just came in as part of the NEW tuple. All you can do is compare the OLD and NEW values of modified to see if it changed, using the IS DISTINCT FROM from Tom's post to deal with NULL values. Not sure why modified is there anyway. Why not just use credentials_last_updated and set it to CURRENT_TIMESTAMP whenever the password is changed? > -- > *Andreas Joseph Krogh* > CTO / Partner - Visena AS > Mobile: +47 909 56 963 > andreas@visena.com > www.visena.com > -- Adrian Klaver adrian.klaver@aklaver.com