Skip to content

Instantly share code, notes, and snippets.

@atwong
Created October 27, 2022 00:23
Show Gist options
  • Save atwong/a7d9832aa21d4e64bb591c36489ab718 to your computer and use it in GitHub Desktop.
Save atwong/a7d9832aa21d4e64bb591c36489ab718 to your computer and use it in GitHub Desktop.
--
-- Functions to generate an aggregate XOR of a UUID column [Postgres]
--
-- Useful to verify two tables are identical without computing
-- a full md5 checksum of entire table
--
-- Note, only uses the big-end 8 bytes of the UUID as there are
-- no 16-byte integers
--
-- Usage: select to_hex(aggrxoruuid(pk)) from mytable
-- where pk is of type UUID
--
create or replace function uuid2bit(val uuid) returns bigint
language sql
as
$$
select ('x' || translate(val::text, '-', ''))::bit(64)::bigint
$$;
create or replace function xoruuid(x1 bigint, u1 uuid) returns bigint
language sql
as
$$
select x1 # uuid2bit(u1)
$$;
create or replace aggregate aggrxoruuid(uuid)
(
sfunc = xoruuid,
stype = bigint,
initcond = 0
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment