[PATCH] Add crc32(text) & crc32(bytea)
Aleksander Alekseev <aleksander@timescale.com>
From: Aleksander Alekseev <aleksander@timescale.com>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2024-07-18T11:24:23Z
Lists: pgsql-hackers
Attachments
- v1-0001-Add-crc32-text-crc32-bytea.patch (application/octet-stream) patch v1-0001
Hi, While answering one of the recent questions [1] I wanted to use crc32(text) and discovered that it's missing out-of-the box. Of course, one can use `substr(md5(x), 1, 8)` with almost the same effect but it's less convenient and could be slower (I didn't do actual benchmarks though). Also it's incompatible with third-party software that may calculate crc32's and store the results in PostgreSQL. I vaguely recall that I faced this problem before. Supporting crc32 was requested on the mailing list [2] and a number of workarounds exist in PL/pgSQL [3][4]. Since there seems to be a demand and it costs us nothing to maintain crc32() I suggest adding it. The proposed patch exposes our internal crc32 implementation to the user. I chose to return a hex string similarly to md5(). In my humble experience this is most convenient in practical use. However if the majority believes that the function should return a bigint (in order to fit an unsigned int32) or a bytea (as SHA* functions do), I'm fine with whatever consensus the community reaches. [1]: https://www.postgresql.org/message-id/CAJ7c6TOurV4uA5Yz%3DaJ-ae4czL_zdFNqxbu47eyVrYFefrWoog%40mail.gmail.com [2]: https://www.postgresql.org/message-id/flat/auto-000557707157%40umail.ru [3]: https://stackoverflow.com/questions/28179335/crc32-function-with-pl-pgsql [4]: https://gist.github.com/cuber/bcf0a3a96fc9a790d96d -- Best regards, Aleksander Alekseev
Commits
-
Add user-callable CRC functions.
- 760162fedb4f 18.0 landed
-
Add user-callable SHA-2 functions
- 10cfce34c0fe 11.0 cited