Re: How to properly hash a row or set of columns. - Mailing list pgsql-general

From Klaudie Willis
Subject Re: How to properly hash a row or set of columns.
Date
Msg-id EHvjVACZMnepEkN-1mLZg38rlETeERE5o4UXGN2q-uLMfKDrMGJEttSk2ZcW-W3JAtaFbRCGHfma5KIMe3qQ_ImZTimSSkKJzKyQDqC_FuA=@protonmail.com
Whole thread Raw
In response to How to properly hash a row or set of columns.  (Klaudie Willis <Klaudie.Willis@protonmail.com>)
List pgsql-general
A minor correction by casting the row a little:
create table t (
  a text
  b text
  hashid varchar not NULL GENERATED ALWAYS AS (sha256(row(a,b)::text::bytea)) stored
)

Sent with ProtonMail Secure Email.


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Tuesday, January 18th, 2022 at 14:34, Klaudie Willis <Klaudie.Willis@protonmail.com> wrote:
Hashing a row or set of columns is useful in some circumstances where you need to compare the row with for instance incoming data. This is most relevant when you do not control the source data yourself, or else you would usually solve it by other means.

Still, It would be great if you could do it like this:
create table t (
  a text
  b text
  hashid varchar not NULL GENERATED ALWAYS AS (sha256(row(a,b))) stored
)

But, row is not immutable so you are not allowed to do this.  Instead, you need to start concatenating columns, but if you want to do that correctly, you also need separator symbols between columns, which then needs to be escaped in the individual column values. And then you have to handle NULL properly as well.  The first example handles all of this, if only row was immutable.

Any better way of doing this?  Can I create my own row2() constructor that IS immutable in a simple way?

best regards
Klaudie




pgsql-general by date:

Previous
From: Klaudie Willis
Date:
Subject: How to properly hash a row or set of columns.
Next
From: Stephen Frost
Date:
Subject: Re: WAL Archiving and base backup