hashlib

稳定哈希函数包

概览

扩展包名版本分类许可证语言
pg_hashlib1.1UTILPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
4400hashlib-
相关扩展xxhash shacrypt cryptint pguecc pgcrypto gzip bzip zstd

build-deps: python3-docutils

版本

类型仓库版本PG 大版本包名依赖
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

构建

您可以使用 pig build 命令构建 pg_hashlib 扩展的 RPM / DEB 包:

pig build pkg pg_hashlib         # 构建 RPM / DEB 包

安装

您可以直接安装 pg_hashlib 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_hashlib;          # 当前活跃 PG 版本安装
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 hashlib;

用法

hashlib: PostgreSQL 的稳定哈希函数库

提供实现不会随 PostgreSQL 版本变化的稳定哈希函数。

字符串哈希(32 位)

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

使用可选的初始值:

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

字符串哈希(64 位)

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

字符串哈希(128 位)

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

整数哈希

SELECT hash_int4(42);        -- 32 位整数的 32 位哈希
SELECT hash_int8(42::bigint); -- 64 位整数的 64 位哈希

可用算法

算法CPU 无关位数描述
crc3232CRC32
murmur332MurmurHash v3
md5128MD5
city6464CityHash64
city128128CityHash128
siphash2464SipHash-2-4
spooky128SpookyHash
lookup264Jenkins lookup2
lookup364Jenkins lookup3 CPU 原生字节序
lookup3be64Jenkins lookup3 大端字节序
lookup3le64Jenkins lookup3 小端字节序
pgsql8464Postgres 8.4+ 中修改的 lookup3

整数算法:wang32wang32multjenkins(32 位);wang64wang64to32(64 位)。所有整数算法都是可逆的(1:1 映射),适合在唯一 ID 上创建随机排序。


最后修改 2026-03-14: update extension metadata (953cbd0)