hashlib

Stable hash functions for Postgres

Overview

PackageVersionCategoryLicenseLanguage
pg_hashlib1.1UTILPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
4400hashlibNoYesNoYesYesYes-
Relatedxxhash shacrypt cryptint pguecc pgcrypto gzip bzip zstd

build-deps: python3-docutils

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.11817161514pg_hashlib-
RPMPIGSTY1.11817161514pg_hashlib_$v-
DEBPIGSTY1.11817161514postgresql-$v-pg-hashlib-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
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

Build

You can build the RPM / DEB packages for pg_hashlib using pig build:

pig build pkg pg_hashlib         # build RPM / DEB packages

Install

You can install pg_hashlib directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

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

Install the extension using pig or apt/yum/dnf:

pig install pg_hashlib;          # Install for current active PG version
pig ext install -y pg_hashlib -v 18  # PG 18
pig ext install -y pg_hashlib -v 17  # PG 17
pig ext install -y pg_hashlib -v 16  # PG 16
pig ext install -y pg_hashlib -v 15  # PG 15
pig ext install -y pg_hashlib -v 14  # PG 14
dnf install -y pg_hashlib_18       # PG 18
dnf install -y pg_hashlib_17       # PG 17
dnf install -y pg_hashlib_16       # PG 16
dnf install -y pg_hashlib_15       # PG 15
dnf install -y pg_hashlib_14       # PG 14
apt install -y postgresql-18-pg-hashlib   # PG 18
apt install -y postgresql-17-pg-hashlib   # PG 17
apt install -y postgresql-16-pg-hashlib   # PG 16
apt install -y postgresql-15-pg-hashlib   # PG 15
apt install -y postgresql-14-pg-hashlib   # PG 14

Create Extension:

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 Modified 2026-03-12: add pg extension catalog (95749bf)