parray_gin

为 text[] 提供部分匹配运算符与 GIN 索引支持

概览

扩展包名版本分类许可证语言
parray_gin1.4.0FUNCPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
4860parray_gin-
相关扩展intarray btree_gin btree_gist pg_trgm smlar aggs_for_arrays aggs_for_vecs arraymath

PGXN dist name and PostgreSQL extension name are both parray_gin; Pigsty packages are built for PG 14-18.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.4.01817161514parray_gin-
RPMPIGSTY1.4.01817161514parray_gin_$v-
DEBPIGSTY1.4.01817161514postgresql-$v-parray-gin-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
d13.x86_64
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
d13.aarch64
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
u22.x86_64
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
u22.aarch64
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
u24.x86_64
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
u24.aarch64
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0
PIGSTY 1.4.0

构建

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

pig build pkg parray_gin         # 构建 RPM / DEB 包

安装

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

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

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

pig install parray_gin;          # 当前活跃 PG 版本安装
pig ext install -y parray_gin -v 18  # PG 18
pig ext install -y parray_gin -v 17  # PG 17
pig ext install -y parray_gin -v 16  # PG 16
pig ext install -y parray_gin -v 15  # PG 15
pig ext install -y parray_gin -v 14  # PG 14
dnf install -y parray_gin_18       # PG 18
dnf install -y parray_gin_17       # PG 17
dnf install -y parray_gin_16       # PG 16
dnf install -y parray_gin_15       # PG 15
dnf install -y parray_gin_14       # PG 14
apt install -y postgresql-18-parray-gin   # PG 18
apt install -y postgresql-17-parray-gin   # PG 17
apt install -y postgresql-16-parray-gin   # PG 16
apt install -y postgresql-15-parray-gin   # PG 15
apt install -y postgresql-14-parray-gin   # PG 14

创建扩展

CREATE EXTENSION parray_gin;

用法

语法:

CREATE EXTENSION parray_gin;
CREATE INDEX test_tags_idx ON test_table USING gin (val parray_gin_ops);
SELECT * FROM test_table WHERE val @> ARRAY['must','contain'];
SELECT * FROM test_table WHERE val @@> ARRAY['what%like%'];

来源:README参考文档

parray_gintext[] 数组提供 GIN 索引和操作符支持,既支持严格匹配,也支持部分匹配。上游文档将其描述为基于 pg_trgm 三元组实现的数组索引方案。

数组索引

该扩展提供 parray_gin_ops 操作符类,可用于 text[] 上的 GIN 索引:

CREATE TABLE test_table(id bigserial, val text[]);
CREATE INDEX test_tags_idx ON test_table USING gin (val parray_gin_ops);

参考文档指出,被索引的值和查询都会拆分为 trigrams。由于 GIN 可能返回误命中,操作符匹配后还会进行复核。

操作符

严格匹配

@> (text[], text[]) -> bool

当左侧数组包含右侧数组中的所有元素时返回 true

SELECT * FROM test_table WHERE val @> ARRAY['far'];

<@ (text[], text[]) -> bool

当左侧数组被右侧数组包含时返回 true

SELECT * FROM test_table WHERE val <@ ARRAY['galaxy','ago','vader'];

部分匹配

@@> (text[], text[]) -> bool

当左侧数组以部分匹配方式包含右侧所有项时返回 true,例如 'foobar' ~~ 'foo%''foobar' ~~ '%oo%'

SELECT * FROM test_table WHERE val @@> ARRAY['%ar%'];

<@@ (text[], text[]) -> bool

当左侧数组被右侧所有模式部分匹配包含时返回 true

SELECT * FROM test_table WHERE val <@@ ARRAY['%ar%','vader'];

说明

上游文档指出,GIN 可用于 @>, <@, @@><@@。文档还提到,该扩展可用于从 JSON 文本字段中提取出的 JSON 数组,相关场景中曾与 json_accessors 扩展配合使用。


最后修改 2026-04-10: extension update (13b4540)