hashlib
Stable hash functions for Postgres
Repository
markokr/pghashlib
https://github.com/markokr/pghashlib
Source
pg_hashlib-1.1.tar.gz
pg_hashlib-1.1.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_hashlib | 1.1 | UTIL | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4400 | hashlib | No | Yes | No | Yes | Yes | Yes | - |
| Related | xxhash shacrypt cryptint pguecc pgcrypto gzip bzip zstd |
|---|
build-deps: python3-docutils
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.1 | 1817161514 | pg_hashlib | - |
| RPM | PIGSTY | 1.1 | 1817161514 | pg_hashlib_$v | - |
| DEB | PIGSTY | 1.1 | 1817161514 | postgresql-$v-pg-hashlib | - |
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
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
| Algorithm | CPU-indep | Bits | Description |
|---|---|---|---|
crc32 | yes | 32 | CRC32 |
murmur3 | no | 32 | MurmurHash v3 |
md5 | yes | 128 | MD5 |
city64 | no | 64 | CityHash64 |
city128 | no | 128 | CityHash128 |
siphash24 | yes | 64 | SipHash-2-4 |
spooky | no | 128 | SpookyHash |
lookup2 | no | 64 | Jenkins lookup2 |
lookup3 | no | 64 | Jenkins lookup3 CPU-native |
lookup3be | yes | 64 | Jenkins lookup3 big-endian |
lookup3le | yes | 64 | Jenkins lookup3 little-endian |
pgsql84 | no | 64 | Hacked 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.
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.