pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Mitar <mmitar@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Determining if a table really changed in a trigger
Date: Tue, 26 Oct 2021 16:05:42 +0200
Message-ID: <BEA64F14-AD5D-4221-B755-3F56570639C7@gmail.com> (raw)
In-Reply-To: <CAKLmikNjiz=zwf9suiAHoBLrRpjLTNsQ=f4PGD3RMFBxzR-3OA@mail.gmail.com>
References: <CAKLmikNjiz=zwf9suiAHoBLrRpjLTNsQ=f4PGD3RMFBxzR-3OA@mail.gmail.com>


> On 26 Oct 2021, at 9:05, Mitar <mmitar@gmail.com> wrote:
> 
> Hi!
> 
> I have a trigger like:
> 
> CREATE TRIGGER update_trigger AFTER UPDATE ON my_table REFERENCING NEW
> TABLE AS new_table OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE
> FUNCTION trigger_function;
> 
> 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

Perhaps if you store an extra column containing a hash (for example MD5) of the row contents (minus the hash column, obviously)? You can put an index on the hash and match between OLD and NEW tables which ones changed.

When calculating the hash, you would have to specify the column names to exclude the hash itself, so something like this:

md5(row(col1, col2, col3)::text)

The row-to-text conversion already takes care of converting JSONB(!) to text.
Don’t use this approach with JSON (as opposed to JSONB) type fields though, a single extra space in the JSON structure would already lead to a difference, as would other formatting differences.

Regards,

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.






view thread (24+ messages)  latest in thread

Message-ID: <BEA64F14-AD5D-4221-B755-3F56570639C7@gmail.com>
Permalink:  ../BEA64F14-AD5D-4221-B755-3F56570639C7@gmail.com/
Also on:    postgresql.org/message-id/BEA64F14-AD5D-4221-B755-3F56570639C7@gmail.com

 · 

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: haramrae@gmail.com, mmitar@gmail.com
  Subject: Re: Determining if a table really changed in a trigger
  In-Reply-To: <BEA64F14-AD5D-4221-B755-3F56570639C7@gmail.com>
  References: <CAKLmikNjiz=zwf9suiAHoBLrRpjLTNsQ=f4PGD3RMFBxzR-3OA@mail.gmail.com> <BEA64F14-AD5D-4221-B755-3F56570639C7@gmail.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