aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/src/sgml/func.sgml34
-rw-r--r--src/backend/utils/hash/pg_crc.c35
-rw-r--r--src/include/catalog/catversion.h2
-rw-r--r--src/include/catalog/pg_proc.dat8
-rw-r--r--src/test/regress/expected/opr_sanity.out2
-rw-r--r--src/test/regress/expected/strings.out27
-rw-r--r--src/test/regress/sql/strings.sql9
7 files changed, 115 insertions, 2 deletions
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 968a9985527..cdde6475135 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -4493,6 +4493,40 @@ SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three');
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
+ <primary>crc32</primary>
+ </indexterm>
+ <function>crc32</function> ( <type>bytea</type> )
+ <returnvalue>bigint</returnvalue>
+ </para>
+ <para>
+ Computes the CRC-32 value of the binary string.
+ </para>
+ <para>
+ <literal>crc32('abc'::bytea)</literal>
+ <returnvalue>891568578</returnvalue>
+ </para></entry>
+ </row>
+
+ <row>
+ <entry role="func_table_entry"><para role="func_signature">
+ <indexterm>
+ <primary>crc32c</primary>
+ </indexterm>
+ <function>crc32c</function> ( <type>bytea</type> )
+ <returnvalue>bigint</returnvalue>
+ </para>
+ <para>
+ Computes the CRC-32C value of the binary string.
+ </para>
+ <para>
+ <literal>crc32c('abc'::bytea)</literal>
+ <returnvalue>910901175</returnvalue>
+ </para></entry>
+ </row>
+
+ <row>
+ <entry role="func_table_entry"><para role="func_signature">
+ <indexterm>
<primary>get_bit</primary>
</indexterm>
<function>get_bit</function> ( <parameter>bytes</parameter> <type>bytea</type>,
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);
+}
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index 37c0354ab91..9a0ae278239 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -57,6 +57,6 @@
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 202408121
+#define CATALOG_VERSION_NO 202408122
#endif
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index be37576a34a..4abc6d95262 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -7743,6 +7743,14 @@
proname => 'system', provolatile => 'v', prorettype => 'tsm_handler',
proargtypes => 'internal', prosrc => 'tsm_system_handler' },
+# CRC variants
+{ oid => '8571', descr => 'CRC-32 value',
+ proname => 'crc32', proleakproof => 't', prorettype => 'int8',
+ proargtypes => 'bytea', prosrc => 'crc32_bytea' },
+{ oid => '8572', descr => 'CRC-32C value',
+ proname => 'crc32c', proleakproof => 't', prorettype => 'int8',
+ proargtypes => 'bytea', prosrc => 'crc32c_bytea' },
+
# cryptographic
{ oid => '2311', descr => 'MD5 hash',
proname => 'md5', proleakproof => 't', prorettype => 'text',
diff --git a/src/test/regress/expected/opr_sanity.out b/src/test/regress/expected/opr_sanity.out
index 9d047b21b88..0d734169f11 100644
--- a/src/test/regress/expected/opr_sanity.out
+++ b/src/test/regress/expected/opr_sanity.out
@@ -874,6 +874,8 @@ xid8ne(xid8,xid8)
xid8cmp(xid8,xid8)
uuid_extract_timestamp(uuid)
uuid_extract_version(uuid)
+crc32(bytea)
+crc32c(bytea)
-- restore normal output mode
\a\t
-- List of functions used by libpq's fe-lobj.c
diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out
index 52b69a107fb..b65bb2d5368 100644
--- a/src/test/regress/expected/strings.out
+++ b/src/test/regress/expected/strings.out
@@ -2256,6 +2256,33 @@ SELECT sha512('The quick brown fox jumps over the lazy dog.');
(1 row)
--
+-- CRC
+--
+SELECT crc32('');
+ crc32
+-------
+ 0
+(1 row)
+
+SELECT crc32('The quick brown fox jumps over the lazy dog.');
+ crc32
+------------
+ 1368401385
+(1 row)
+
+SELECT crc32c('');
+ crc32c
+--------
+ 0
+(1 row)
+
+SELECT crc32c('The quick brown fox jumps over the lazy dog.');
+ crc32c
+-----------
+ 419469235
+(1 row)
+
+--
-- encode/decode
--
SELECT encode('\x1234567890abcdef00', 'hex');
diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql
index 39596789929..8e0f3a0e75f 100644
--- a/src/test/regress/sql/strings.sql
+++ b/src/test/regress/sql/strings.sql
@@ -720,6 +720,15 @@ SELECT sha512('');
SELECT sha512('The quick brown fox jumps over the lazy dog.');
--
+-- CRC
+--
+SELECT crc32('');
+SELECT crc32('The quick brown fox jumps over the lazy dog.');
+
+SELECT crc32c('');
+SELECT crc32c('The quick brown fox jumps over the lazy dog.');
+
+--
-- encode/decode
--
SELECT encode('\x1234567890abcdef00', 'hex');