From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Andreas Joseph Krogh <andreas@visena.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: How to specify that a trigger should fire when column is NOT in SET-clause?
Date: Fri, 25 Dec 2020 11:13:48 -0800
Message-ID: <f919b311-bd8a-dd60-3c16-b05fe4341cbc@aklaver.com> (raw)
In-Reply-To: <VisenaEmail.28.aa3c591dff4a2d38.1769b1655f9@tc7-visena>
References: <VisenaEmail.26.7cbf2947c8d23ceb.1769a2755ff@tc7-visena>
<dacbcd40-3cef-074b-71a6-f1806884f62b@aklaver.com>
<VisenaEmail.28.aa3c591dff4a2d38.1769b1655f9@tc7-visena>
On 12/25/20 10:19 AM, Andreas Joseph Krogh wrote:
> På fredag 25. desember 2020 kl. 17:48:39, skrev Adrian Klaver
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>>:
>
> 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 <mailto:andreas@visena.com>
> www.visena.com <https://www.visena.com;
> <https://www.visena.com;
--
Adrian Klaver
adrian.klaver@aklaver.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, andreas@visena.com
Subject: Re: How to specify that a trigger should fire when column is NOT in SET-clause?
In-Reply-To: <f919b311-bd8a-dd60-3c16-b05fe4341cbc@aklaver.com>
References: <VisenaEmail.26.7cbf2947c8d23ceb.1769a2755ff@tc7-visena> <dacbcd40-3cef-074b-71a6-f1806884f62b@aklaver.com> <VisenaEmail.28.aa3c591dff4a2d38.1769b1655f9@tc7-visena> <f919b311-bd8a-dd60-3c16-b05fe4341cbc@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox