How to properly hash a row or set of columns.

From: Klaudie Willis <Klaudie(dot)Willis(at)protonmail(dot)com>
To: PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: How to properly hash a row or set of columns.
Date: 2022-01-18 13:34:14
Message-ID: kD3RhivhqVqfoDIDV6AkTVQes6JvkmBYUJbhCUXtmX3PLic2nu7MHYa8d_9fhCzlnA6V2523oic9aaOnEQxEmX-9bNY159yNzyRzU6P9q6U=@protonmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Klaudie Willis 2022-01-18 15:32:28 Re: How to properly hash a row or set of columns.
Previous Message Carla Iriberri 2022-01-17 16:05:52 could not accept SSL connection: Success