Skip to content

Commit 760162f

Browse files
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
1 parent 313df8f commit 760162f

File tree

7 files changed

+115
-2
lines changed

7 files changed

+115
-2
lines changed

doc/src/sgml/func.sgml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4490,6 +4490,40 @@ SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three');
44904490
</para></entry>
44914491
</row>
44924492

4493+
<row>
4494+
<entry role="func_table_entry"><para role="func_signature">
4495+
<indexterm>
4496+
<primary>crc32</primary>
4497+
</indexterm>
4498+
<function>crc32</function> ( <type>bytea</type> )
4499+
<returnvalue>bigint</returnvalue>
4500+
</para>
4501+
<para>
4502+
Computes the CRC-32 value of the binary string.
4503+
</para>
4504+
<para>
4505+
<literal>crc32('abc'::bytea)</literal>
4506+
<returnvalue>891568578</returnvalue>
4507+
</para></entry>
4508+
</row>
4509+
4510+
<row>
4511+
<entry role="func_table_entry"><para role="func_signature">
4512+
<indexterm>
4513+
<primary>crc32c</primary>
4514+
</indexterm>
4515+
<function>crc32c</function> ( <type>bytea</type> )
4516+
<returnvalue>bigint</returnvalue>
4517+
</para>
4518+
<para>
4519+
Computes the CRC-32C value of the binary string.
4520+
</para>
4521+
<para>
4522+
<literal>crc32c('abc'::bytea)</literal>
4523+
<returnvalue>910901175</returnvalue>
4524+
</para></entry>
4525+
</row>
4526+
44934527
<row>
44944528
<entry role="func_table_entry"><para role="func_signature">
44954529
<indexterm>

src/backend/utils/hash/pg_crc.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*-------------------------------------------------------------------------
1818
*/
1919

20-
#include "c.h"
20+
#include "postgres.h"
2121

22+
#include "port/pg_crc32c.h"
23+
#include "utils/builtins.h"
2224
#include "utils/pg_crc.h"
25+
#include "varatt.h"
2326

2427
/*
2528
* Lookup table for calculating CRC-32 using Sarwate's algorithm.
@@ -95,3 +98,33 @@ const uint32 pg_crc32_table[256] = {
9598
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
9699
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
97100
};
101+
102+
/*
103+
* SQL-callable functions
104+
*/
105+
106+
Datum
107+
crc32_bytea(PG_FUNCTION_ARGS)
108+
{
109+
bytea *in = PG_GETARG_BYTEA_PP(0);
110+
pg_crc32 crc;
111+
112+
INIT_TRADITIONAL_CRC32(crc);
113+
COMP_TRADITIONAL_CRC32(crc, VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
114+
FIN_TRADITIONAL_CRC32(crc);
115+
116+
PG_RETURN_INT64(crc);
117+
}
118+
119+
Datum
120+
crc32c_bytea(PG_FUNCTION_ARGS)
121+
{
122+
bytea *in = PG_GETARG_BYTEA_PP(0);
123+
pg_crc32c crc;
124+
125+
INIT_CRC32C(crc);
126+
COMP_CRC32C(crc, VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
127+
FIN_CRC32C(crc);
128+
129+
PG_RETURN_INT64(crc);
130+
}

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202408121
60+
#define CATALOG_VERSION_NO 202408122
6161

6262
#endif

src/include/catalog/pg_proc.dat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7743,6 +7743,14 @@
77437743
proname => 'system', provolatile => 'v', prorettype => 'tsm_handler',
77447744
proargtypes => 'internal', prosrc => 'tsm_system_handler' },
77457745

7746+
# CRC variants
7747+
{ oid => '8571', descr => 'CRC-32 value',
7748+
proname => 'crc32', proleakproof => 't', prorettype => 'int8',
7749+
proargtypes => 'bytea', prosrc => 'crc32_bytea' },
7750+
{ oid => '8572', descr => 'CRC-32C value',
7751+
proname => 'crc32c', proleakproof => 't', prorettype => 'int8',
7752+
proargtypes => 'bytea', prosrc => 'crc32c_bytea' },
7753+
77467754
# cryptographic
77477755
{ oid => '2311', descr => 'MD5 hash',
77487756
proname => 'md5', proleakproof => 't', prorettype => 'text',

src/test/regress/expected/opr_sanity.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,8 @@ xid8ne(xid8,xid8)
874874
xid8cmp(xid8,xid8)
875875
uuid_extract_timestamp(uuid)
876876
uuid_extract_version(uuid)
877+
crc32(bytea)
878+
crc32c(bytea)
877879
-- restore normal output mode
878880
\a\t
879881
-- List of functions used by libpq's fe-lobj.c

src/test/regress/expected/strings.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,33 @@ SELECT sha512('The quick brown fox jumps over the lazy dog.');
22552255
\x91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed
22562256
(1 row)
22572257

2258+
--
2259+
-- CRC
2260+
--
2261+
SELECT crc32('');
2262+
crc32
2263+
-------
2264+
0
2265+
(1 row)
2266+
2267+
SELECT crc32('The quick brown fox jumps over the lazy dog.');
2268+
crc32
2269+
------------
2270+
1368401385
2271+
(1 row)
2272+
2273+
SELECT crc32c('');
2274+
crc32c
2275+
--------
2276+
0
2277+
(1 row)
2278+
2279+
SELECT crc32c('The quick brown fox jumps over the lazy dog.');
2280+
crc32c
2281+
-----------
2282+
419469235
2283+
(1 row)
2284+
22582285
--
22592286
-- encode/decode
22602287
--

src/test/regress/sql/strings.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,15 @@ SELECT sha384('The quick brown fox jumps over the lazy dog.');
719719
SELECT sha512('');
720720
SELECT sha512('The quick brown fox jumps over the lazy dog.');
721721

722+
--
723+
-- CRC
724+
--
725+
SELECT crc32('');
726+
SELECT crc32('The quick brown fox jumps over the lazy dog.');
727+
728+
SELECT crc32c('');
729+
SELECT crc32c('The quick brown fox jumps over the lazy dog.');
730+
722731
--
723732
-- encode/decode
724733
--

0 commit comments

Comments
 (0)