Skip to content

Instantly share code, notes, and snippets.

@mattbishop
mattbishop / crc32c.sql
Last active September 14, 2020 22:31
CRC32C calculator for Postgresql in PL/PGSQL
-- crc32c https://tools.ietf.org/html/rfc3385#section-4.1
-- Table-based crc32 (not exactly right algorithm, but a good pattern to follow)
-- https://gist.github.com/cuber/bcf0a3a96fc9a790d96d
-- Google-created CRC table
-- https://github.com/googlearchive/crc32c-java/blob/master/src/com/google/cloud/Crc32c.java
CREATE OR REPLACE FUNCTION update_crc32c(src TEXT,
crc BIGINT DEFAULT 0)
RETURNS BIGINT
LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE AS
$$