permuteseq

伪随机数ID置换生成器

概览

扩展包名版本分类许可证语言
permuteseq1.2.2FUNCPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
4550permuteseq-
相关扩展pg_idkit random sequential_uuids pg_hashids pgx_ulid pg_uuidv7 uuid-ossp

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.2.21817161514permuteseq-
RPMPIGSTY1.2.21817161514permuteseq_$v-
DEBPIGSTY1.2.21817161514postgresql-$v-permuteseq-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
d13.x86_64
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
d13.aarch64
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
u22.x86_64
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
u22.aarch64
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
u24.x86_64
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
u24.aarch64
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2
PIGSTY 1.2.2

构建

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

pig build pkg permuteseq         # 构建 RPM / DEB 包

安装

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

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

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

pig install permuteseq;          # 当前活跃 PG 版本安装
pig ext install -y permuteseq -v 18  # PG 18
pig ext install -y permuteseq -v 17  # PG 17
pig ext install -y permuteseq -v 16  # PG 16
pig ext install -y permuteseq -v 15  # PG 15
pig ext install -y permuteseq -v 14  # PG 14
dnf install -y permuteseq_18       # PG 18
dnf install -y permuteseq_17       # PG 17
dnf install -y permuteseq_16       # PG 16
dnf install -y permuteseq_15       # PG 15
dnf install -y permuteseq_14       # PG 14
apt install -y postgresql-18-permuteseq   # PG 18
apt install -y postgresql-17-permuteseq   # PG 17
apt install -y postgresql-16-permuteseq   # PG 16
apt install -y postgresql-15-permuteseq   # PG 15
apt install -y postgresql-14-permuteseq   # PG 14

创建扩展

CREATE EXTENSION permuteseq;

用法

permuteseq: 可扩展的序列伪随机置换

无需存储历史值,即可生成唯一、非连续、看似随机的数字序列。使用 Feistel 密码与循环行走法实现格式保持加密。

CREATE EXTENSION permuteseq;

函数

函数描述
permute_nextval(seq_oid, crypt_key bigint)推进序列并返回序列范围内的加密值
reverse_permute(seq_oid, value bigint, crypt_key bigint)从置换后的元素计算出原始值
range_encrypt_element(clear_val bigint, min_val bigint, max_val bigint, crypt_key bigint)在给定范围内加密一个 bigint
range_decrypt_element(crypt_val bigint, min_val bigint, max_val bigint, crypt_key bigint)解密先前加密的值

示例

CREATE SEQUENCE s MINVALUE -10000 MAXVALUE 15000;

-- 从序列中生成看似随机的唯一值
SELECT permute_nextval('s'::regclass, 123456789012345)
  FROM generate_series(1, 10);

-- 将置换后的值反向还原为原始值
SELECT reverse_permute('s'::regclass, -545, 123456789012345);
-- -10000

-- 在任意范围内加密/解密
SELECT range_encrypt_element(91919191919, 1e10::bigint, 1e11::bigint, 123456789012345);
-- 83028080992

SELECT range_decrypt_element(83028080992, 1e10::bigint, 1e11::bigint, 123456789012345);
-- 91919191919

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