From: Tom Lane <tgl@sss.pgh.pa.us>
To: Christophe Pettus <xof@thebuild.com>
Cc: Dirk Mika <Dirk.Mika@mikatiming.de>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Trigger with conditional predicates
Date: Fri, 01 Jan 2021 11:27:47 -0500
Message-ID: <2524881.1609518467@sss.pgh.pa.us> (raw)
In-Reply-To: <7097609B-DCC8-49B8-932C-35A3D7D6A405@thebuild.com>
References: <A4A3D4D4-B8C9-4ED2-B10D-0A908D639F99@mikatiming.de>
<7097609B-DCC8-49B8-932C-35A3D7D6A405@thebuild.com>
Christophe Pettus <xof@thebuild.com> 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 := etc etc ;
> ENDIF;
> There's currently no way to detect if the column was simply not mentioned 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
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: tgl@sss.pgh.pa.us, xof@thebuild.com, Dirk.Mika@mikatiming.de
Subject: Re: Trigger with conditional predicates
In-Reply-To: <2524881.1609518467@sss.pgh.pa.us>
References: <A4A3D4D4-B8C9-4ED2-B10D-0A908D639F99@mikatiming.de> <7097609B-DCC8-49B8-932C-35A3D7D6A405@thebuild.com> <2524881.1609518467@sss.pgh.pa.us>
* 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