pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Determining if a table really changed in a trigger
Date: Wed, 27 Oct 2021 10:02:27 +0200
Message-ID: <021f82ab-110a-a663-1de2-687ef3645780@gmx.net> (raw)
In-Reply-To: <CAKLmikNjiz=zwf9suiAHoBLrRpjLTNsQ=f4PGD3RMFBxzR-3OA@mail.gmail.com>
References: <CAKLmikNjiz=zwf9suiAHoBLrRpjLTNsQ=f4PGD3RMFBxzR-3OA@mail.gmail.com>

Mitar schrieb am 26.10.2021 um 09:05:
> I would like to test inside trigger_function if the table really
> changed. I have tried to do:
>
> PERFORM * FROM ((TABLE old_table EXCEPT TABLE new_table) UNION ALL
> (TABLE new_table EXCEPT TABLE old_table)) AS differences LIMIT 1;
> IF FOUND THEN
>   ... changed ...
> END IF;
>
> But this fails if the table contains a JSON field with the error:
>
> could not identify an equality operator for type json
>

If you use jsonb (which is recommended over json anyway), then this
would work.

I would probably use a row level trigger instead of a statement level trigger

Then compare the two records using IS DISTINCT FROM


   if new is distinct from old then
      ....
   end if;

> I want to skip trivial updates (those which have not changed anything).

It seems you might want to use the pre-defined function suppress_redundant_updates_trigger()

https://www.postgresql.org/docs/current/functions-trigger.html

Might be faster than a self written trigger.

Regards
Thomas





view thread (24+ messages)  latest in thread

Message-ID: <021f82ab-110a-a663-1de2-687ef3645780@gmx.net>
Permalink:  ../021f82ab-110a-a663-1de2-687ef3645780@gmx.net/
Also on:    postgresql.org/message-id/021f82ab-110a-a663-1de2-687ef3645780@gmx.net

 · 

reply

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: shammat@gmx.net
  Subject: Re: Determining if a table really changed in a trigger
  In-Reply-To: <021f82ab-110a-a663-1de2-687ef3645780@gmx.net>
  References: <CAKLmikNjiz=zwf9suiAHoBLrRpjLTNsQ=f4PGD3RMFBxzR-3OA@mail.gmail.com> <021f82ab-110a-a663-1de2-687ef3645780@gmx.net>

* 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