Re: How to properly hash a row or set of columns.

From: Klaudie Willis <Klaudie(dot)Willis(at)protonmail(dot)com>
To: Klaudie Willis <Klaudie(dot)Willis(at)protonmail(dot)com>
Cc: PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: How to properly hash a row or set of columns.
Date: 2022-01-18 15:32:28
Message-ID: EHvjVACZMnepEkN-1mLZg38rlETeERE5o4UXGN2q-uLMfKDrMGJEttSk2ZcW-W3JAtaFbRCGHfma5KIMe3qQ_ImZTimSSkKJzKyQDqC_FuA=@protonmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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](https://protonmail.com/) Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Tuesday, January 18th, 2022 at 14:34, Klaudie Willis <Klaudie(dot)Willis(at)protonmail(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephen Frost 2022-01-18 17:53:03 Re: WAL Archiving and base backup
Previous Message Klaudie Willis 2022-01-18 13:34:14 How to properly hash a row or set of columns.