summaryrefslogtreecommitdiff
path: root/src/backend/utils/hash
diff options
context:
space:
mode:
authorNathan Bossart2024-08-12 15:35:06 +0000
committerNathan Bossart2024-08-12 15:35:06 +0000
commit760162fedb4f7ee6f0167cc6acfadee6ccb6be66 (patch)
tree95564338177e250a12e506473c4e0fe62f9daa7d /src/backend/utils/hash
parent313df8f5adde186b0052d7d634a6d0ae41852de0 (diff)
Add user-callable CRC functions.
We've had code for CRC-32 and CRC-32C for some time (for WAL records, etc.), but there was no way for users to call it, despite apparent popular demand. The new crc32() and crc32c() functions accept bytea input and return bigint (to avoid returning negative values). Bumps catversion. Author: Aleksander Alekseev Reviewed-by: Peter Eisentraut, Tom Lane Discussion: https://postgr.es/m/CAJ7c6TNMTGnqnG%3DyXXUQh9E88JDckmR45H2Q%2B%3DucaCLMOW1QQw%40mail.gmail.com
Diffstat (limited to 'src/backend/utils/hash')
-rw-r--r--src/backend/utils/hash/pg_crc.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/src/backend/utils/hash/pg_crc.c b/src/backend/utils/hash/pg_crc.c
index 3595938dc4f..a85e6171864 100644
--- a/src/backend/utils/hash/pg_crc.c
+++ b/src/backend/utils/hash/pg_crc.c
@@ -17,9 +17,12 @@
*-------------------------------------------------------------------------
*/
-#include "c.h"
+#include "postgres.h"
+#include "port/pg_crc32c.h"
+#include "utils/builtins.h"
#include "utils/pg_crc.h"
+#include "varatt.h"
/*
* Lookup table for calculating CRC-32 using Sarwate's algorithm.
@@ -95,3 +98,33 @@ const uint32 pg_crc32_table[256] = {
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
};
+
+/*
+ * SQL-callable functions
+ */
+
+Datum
+crc32_bytea(PG_FUNCTION_ARGS)
+{
+ bytea *in = PG_GETARG_BYTEA_PP(0);
+ pg_crc32 crc;
+
+ INIT_TRADITIONAL_CRC32(crc);
+ COMP_TRADITIONAL_CRC32(crc, VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
+ FIN_TRADITIONAL_CRC32(crc);
+
+ PG_RETURN_INT64(crc);
+}
+
+Datum
+crc32c_bytea(PG_FUNCTION_ARGS)
+{
+ bytea *in = PG_GETARG_BYTEA_PP(0);
+ pg_crc32c crc;
+
+ INIT_CRC32C(crc);
+ COMP_CRC32C(crc, VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
+ FIN_CRC32C(crc);
+
+ PG_RETURN_INT64(crc);
+}