pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Marcos Pegoraro <marcos@f10.com.br>
Cc: 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:39:28 +0200
Message-ID: <34832EC6-D3E4-4596-A043-1A199AB04F89@gmail.com> (raw)
In-Reply-To: <CAB-JLwauKri8Y+7GX6TZKvxziHmSEvdb2oc+kE6u2YzY5JUErw@mail.gmail.com>
References: <CAKLmikNjiz=zwf9suiAHoBLrRpjLTNsQ=f4PGD3RMFBxzR-3OA@mail.gmail.com>
	<BEA64F14-AD5D-4221-B755-3F56570639C7@gmail.com>
	<CAB-JLwauKri8Y+7GX6TZKvxziHmSEvdb2oc+kE6u2YzY5JUErw@mail.gmail.com>


> On 26 Oct 2021, at 16:16, Marcos Pegoraro <marcos@f10.com.br> wrote:
> 
> 
>> 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.
>> 
> I don´t think two equal values being converted to json will be different in any way. If row_to_json of both are different, I suppose both record really are different, no ? 

For row_to_json, as it’s the system that combines the fields in a row into a JSON structure and it probably would do that in the same way each time.

The OP however has a field of type JSON in their table, and that can contain the same information between the OLD and NEW fields formatted in a slightly different way.

For example:

=> with x as (
select '{ "x": 1, "y": 2 }'::json
union all
select '{ "y": 2, "x": 1 }'::json
)
select row(x.json)::text, md5(row(x.json)::text) from x;
            row             |               md5                
----------------------------+----------------------------------
 ("{ ""x"": 1, ""y"": 2 }") | 84df40e8660dcf371d89dbf5d6a61c3d
 ("{ ""y"": 2, ""x"": 1 }") | abd6db88c2526be6ea97570aeec7e020
(2 rows)

Whereas:

=> with x as (
select '{ "x": 1, "y": 2 }'::jsonb
union all
select '{ "y": 2, "x": 1 }'::jsonb
)
select row(x.jsonb)::text, md5(row(x.jsonb)::text) from x;
           row            |               md5                
--------------------------+----------------------------------
 ("{""x"": 1, ""y"": 2}") | d5a6dbdec7a5bfe0dc99e090db30322e
 ("{""x"": 1, ""y"": 2}") | d5a6dbdec7a5bfe0dc99e090db30322e
(2 rows)


Alban Hertroys
--
There is always an exception to always.









view thread (24+ messages)  latest in thread

Message-ID: <34832EC6-D3E4-4596-A043-1A199AB04F89@gmail.com>
Permalink:  ../34832EC6-D3E4-4596-A043-1A199AB04F89@gmail.com/
Also on:    postgresql.org/message-id/34832EC6-D3E4-4596-A043-1A199AB04F89@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, marcos@f10.com.br, mmitar@gmail.com
  Subject: Re: Determining if a table really changed in a trigger
  In-Reply-To: <34832EC6-D3E4-4596-A043-1A199AB04F89@gmail.com>
  References: <CAKLmikNjiz=zwf9suiAHoBLrRpjLTNsQ=f4PGD3RMFBxzR-3OA@mail.gmail.com> <BEA64F14-AD5D-4221-B755-3F56570639C7@gmail.com> <CAB-JLwauKri8Y+7GX6TZKvxziHmSEvdb2oc+kE6u2YzY5JUErw@mail.gmail.com> <34832EC6-D3E4-4596-A043-1A199AB04F89@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