hashlib

hashlib

pg_hashlib : Stable hash functions for Postgres

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4400
hashlib
pg_hashlib
1.1
UTIL
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-r
No
Yes
No
Yes
yes
no
Relationships
See Also
xxhash
shacrypt
cryptint
pguecc
pgcrypto
gzip
bzip
zstd

build-deps: python3-docutils

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PIGSTY
1.1
18
17
16
15
14
pg_hashlib-
RPM
PIGSTY
1.1
18
17
16
15
14
pg_hashlib_$v-
DEB
PIGSTY
1.1
18
17
16
15
14
postgresql-$v-pg-hashlib-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
el8.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
el9.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
el9.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
el10.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
el10.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
d12.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
d12.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
d13.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
d13.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
u22.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
u22.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
u24.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
u24.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1

Source

pig build pkg pg_hashlib;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_hashlib;		# install via package name, for the active PG version
pig install hashlib;		# install by extension name, for the current active PG version

pig install hashlib -v 18;   # install for PG 18
pig install hashlib -v 17;   # install for PG 17
pig install hashlib -v 16;   # install for PG 16
pig install hashlib -v 15;   # install for PG 15
pig install hashlib -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION hashlib;

Usage

hashlib: Stable hash functions library for PostgreSQL

Provides stable hash functions whose implementations do not change across PostgreSQL versions.

String Hashing (32-bit)

SELECT hash_string('hello', 'crc32');
SELECT hash_string('hello', 'murmur3');

With optional initial value:

SELECT hash_string('hello', 'crc32', 42);

String Hashing (64-bit)

SELECT hash64_string('hello', 'city64');
SELECT hash64_string('hello', 'siphash24');
SELECT hash64_string('hello', 'lookup3');

String Hashing (128-bit)

SELECT hash128_string('hello', 'md5');
SELECT hash128_string('hello', 'city128');
SELECT hash128_string('hello', 'spooky');

Integer Hashing

SELECT hash_int4(42);        -- 32-bit hash of 32-bit integer
SELECT hash_int8(42::bigint); -- 64-bit hash of 64-bit integer

Available Algorithms

AlgorithmCPU-indepBitsDescription
crc32yes32CRC32
murmur3no32MurmurHash v3
md5yes128MD5
city64no64CityHash64
city128no128CityHash128
siphash24yes64SipHash-2-4
spookyno128SpookyHash
lookup2no64Jenkins lookup2
lookup3no64Jenkins lookup3 CPU-native
lookup3beyes64Jenkins lookup3 big-endian
lookup3leyes64Jenkins lookup3 little-endian
pgsql84no64Hacked lookup3 in Postgres 8.4+

Integer algorithms: wang32, wang32mult, jenkins (32-bit); wang64, wang64to32 (64-bit). All are reversible (1:1 mapping), useful for creating random sort orders over unique IDs.

Last updated on