Re: [PATCH] Add crc32(text) & crc32(bytea)

From: Aleksander Alekseev <aleksander(at)timescale(dot)com>
To: Nathan Bossart <nathandbossart(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Add crc32(text) & crc32(bytea)
Date: 2024-08-06 08:04:41
Message-ID: CAJ7c6TOQX3PDSbci3bAvKqQQDEYgvJpYdPMHSywPy43i_h_SWQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

> This looks pretty good to me. The only point that I think deserves more
> discussion is the return type. Does bytea make the most sense here? Or
> should we consider int/bigint?

Personally I would choose BYTEA in order to be consistent with sha*() functions.

It can be casted to TEXT if user wants a result similar to the one
md5() returns:

```
SELECT encode(crc32('PostgreSQL'), 'hex');
```

... and somewhat less convenient to BIGINT:

```
SELECT ((get_byte(crc, 0) :: bigint << 24) | (get_byte(crc, 1) << 16)
| (get_byte(crc, 2) << 8) | get_byte(crc, 3))
FROM (SELECT crc32('PostgreSQL') AS crc);
```

I don't like the `integer` option because crc32 value is typically
considered as an unsigned one and `integer` is not large enough to
represent uint32.

Perhaps we need get_int4() / get_int8() / get_numeric() as there seems
to be a demand [1][2] and it will allow us to easily cast a `bytea`
value to `integer` or `bigint`. This is probably another topic though.

[1]: https://stackoverflow.com/questions/32944267/postgresql-converting-bytea-to-bigint
[2]: https://postgr.es/m/AANLkTikip9xs8iXc8e%2BMgz1T1701i8Xk6QtbVB3KJQzX%40mail.gmail.com

--
Best regards,
Aleksander Alekseev

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Zhijie Hou (Fujitsu) 2024-08-06 08:15:27 RE: Conflict detection and logging in logical replication
Previous Message Peter Eisentraut 2024-08-06 08:04:27 Rename C23 keyword