跳转到主要内容

pg_projection

PostgreSQL JSONB 的 MongoDB 风格投影读取函数

pg_projection · PostgreSQL JSONB 的 MongoDB 风格投影读取函数

概览

ID扩展软件包版本类别许可证编程语言
9090pg_projectionpg_projection1.0.0SIMMITSQL
属性可执行文件动态库需要预加载需要建扩展可迁移可信
----d-r否否否是是否
SQL-only extension.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY1.0.019 18 17 16 15pg_projection-
RPMPIGSTY1.0.019 18 17 16 15pg_projection_$v-
DEBPIGSTY1.0.019 18 17 16 15postgresql-$v-pg-projection-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el8.aarch64N/APIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el9.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el9.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el10.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el10.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
d12.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
d12.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
d13.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
d13.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u22.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u22.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u24.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u24.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u26.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u26.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
软件包版本平台仓库大小下载
pg_projection_191.0.0el9.x86_64pigsty9.8 KiBpg_projection_19-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_191.0.0el9.aarch64pigsty9.8 KiBpg_projection_19-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_191.0.0el10.x86_64pigsty10.0 KiBpg_projection_19-1.0.0-1PGSTY.el10.noarch.rpm
pg_projection_191.0.0el10.aarch64pigsty10.0 KiBpg_projection_19-1.0.0-1PGSTY.el10.noarch.rpm
postgresql-19-pg-projection1.0.0d12.x86_64pigsty3.9 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-19-pg-projection1.0.0d12.aarch64pigsty3.9 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-19-pg-projection1.0.0d13.x86_64pigsty3.9 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-19-pg-projection1.0.0d13.aarch64pigsty3.9 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-19-pg-projection1.0.0u22.x86_64pigsty3.9 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-19-pg-projection1.0.0u22.aarch64pigsty3.9 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-19-pg-projection1.0.0u24.x86_64pigsty3.8 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-19-pg-projection1.0.0u24.aarch64pigsty3.8 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-19-pg-projection1.0.0u26.x86_64pigsty3.9 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~resolute_all.deb
postgresql-19-pg-projection1.0.0u26.aarch64pigsty3.9 KiBpostgresql-19-pg-projection_1.0.0-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
pg_projection_181.0.0el8.x86_64pigsty9.8 KiBpg_projection_18-1.0.0-1PGSTY.el8.noarch.rpm
pg_projection_181.0.0el8.aarch64pigsty9.8 KiBpg_projection_18-1.0.0-1PGSTY.el8.noarch.rpm
pg_projection_181.0.0el9.x86_64pigsty9.8 KiBpg_projection_18-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_181.0.0el9.aarch64pigsty9.8 KiBpg_projection_18-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_181.0.0el10.x86_64pigsty10.0 KiBpg_projection_18-1.0.0-1PGSTY.el10.noarch.rpm
pg_projection_181.0.0el10.aarch64pigsty10.0 KiBpg_projection_18-1.0.0-1PGSTY.el10.noarch.rpm
postgresql-18-pg-projection1.0.0d12.x86_64pigsty3.9 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-18-pg-projection1.0.0d12.aarch64pigsty3.9 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-18-pg-projection1.0.0d13.x86_64pigsty3.9 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-18-pg-projection1.0.0d13.aarch64pigsty3.9 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-18-pg-projection1.0.0u22.x86_64pigsty3.9 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-18-pg-projection1.0.0u22.aarch64pigsty3.9 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-18-pg-projection1.0.0u24.x86_64pigsty3.8 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-18-pg-projection1.0.0u24.aarch64pigsty3.8 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-18-pg-projection1.0.0u26.x86_64pigsty3.9 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~resolute_all.deb
postgresql-18-pg-projection1.0.0u26.aarch64pigsty3.9 KiBpostgresql-18-pg-projection_1.0.0-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
pg_projection_171.0.0el8.x86_64pigsty9.8 KiBpg_projection_17-1.0.0-1PGSTY.el8.noarch.rpm
pg_projection_171.0.0el8.aarch64pigsty9.8 KiBpg_projection_17-1.0.0-1PGSTY.el8.noarch.rpm
pg_projection_171.0.0el9.x86_64pigsty9.8 KiBpg_projection_17-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_171.0.0el9.aarch64pigsty9.8 KiBpg_projection_17-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_171.0.0el10.x86_64pigsty10.0 KiBpg_projection_17-1.0.0-1PGSTY.el10.noarch.rpm
pg_projection_171.0.0el10.aarch64pigsty10.0 KiBpg_projection_17-1.0.0-1PGSTY.el10.noarch.rpm
postgresql-17-pg-projection1.0.0d12.x86_64pigsty3.9 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-17-pg-projection1.0.0d12.aarch64pigsty3.9 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-17-pg-projection1.0.0d13.x86_64pigsty3.9 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-17-pg-projection1.0.0d13.aarch64pigsty3.9 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-17-pg-projection1.0.0u22.x86_64pigsty3.9 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-17-pg-projection1.0.0u22.aarch64pigsty3.9 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-17-pg-projection1.0.0u24.x86_64pigsty3.8 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-17-pg-projection1.0.0u24.aarch64pigsty3.8 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-17-pg-projection1.0.0u26.x86_64pigsty3.8 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~resolute_all.deb
postgresql-17-pg-projection1.0.0u26.aarch64pigsty3.8 KiBpostgresql-17-pg-projection_1.0.0-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
pg_projection_161.0.0el8.x86_64pigsty9.8 KiBpg_projection_16-1.0.0-1PGSTY.el8.noarch.rpm
pg_projection_161.0.0el8.aarch64pigsty9.8 KiBpg_projection_16-1.0.0-1PGSTY.el8.noarch.rpm
pg_projection_161.0.0el9.x86_64pigsty9.8 KiBpg_projection_16-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_161.0.0el9.aarch64pigsty9.8 KiBpg_projection_16-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_161.0.0el10.x86_64pigsty10.0 KiBpg_projection_16-1.0.0-1PGSTY.el10.noarch.rpm
pg_projection_161.0.0el10.aarch64pigsty10.0 KiBpg_projection_16-1.0.0-1PGSTY.el10.noarch.rpm
postgresql-16-pg-projection1.0.0d12.x86_64pigsty3.9 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-16-pg-projection1.0.0d12.aarch64pigsty3.9 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-16-pg-projection1.0.0d13.x86_64pigsty3.9 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-16-pg-projection1.0.0d13.aarch64pigsty3.9 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-16-pg-projection1.0.0u22.x86_64pigsty3.9 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-16-pg-projection1.0.0u22.aarch64pigsty3.9 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-16-pg-projection1.0.0u24.x86_64pigsty3.8 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-16-pg-projection1.0.0u24.aarch64pigsty3.8 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-16-pg-projection1.0.0u26.x86_64pigsty3.9 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~resolute_all.deb
postgresql-16-pg-projection1.0.0u26.aarch64pigsty3.9 KiBpostgresql-16-pg-projection_1.0.0-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
pg_projection_151.0.0el8.x86_64pigsty9.8 KiBpg_projection_15-1.0.0-1PGSTY.el8.noarch.rpm
pg_projection_151.0.0el8.aarch64pigsty9.8 KiBpg_projection_15-1.0.0-1PGSTY.el8.noarch.rpm
pg_projection_151.0.0el9.x86_64pigsty9.8 KiBpg_projection_15-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_151.0.0el9.aarch64pigsty9.8 KiBpg_projection_15-1.0.0-1PGSTY.el9.noarch.rpm
pg_projection_151.0.0el10.x86_64pigsty10.0 KiBpg_projection_15-1.0.0-1PGSTY.el10.noarch.rpm
pg_projection_151.0.0el10.aarch64pigsty10.0 KiBpg_projection_15-1.0.0-1PGSTY.el10.noarch.rpm
postgresql-15-pg-projection1.0.0d12.x86_64pigsty3.9 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-15-pg-projection1.0.0d12.aarch64pigsty3.9 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~bookworm_all.deb
postgresql-15-pg-projection1.0.0d13.x86_64pigsty3.9 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-15-pg-projection1.0.0d13.aarch64pigsty3.9 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~trixie_all.deb
postgresql-15-pg-projection1.0.0u22.x86_64pigsty3.9 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-15-pg-projection1.0.0u22.aarch64pigsty3.9 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~jammy_all.deb
postgresql-15-pg-projection1.0.0u24.x86_64pigsty3.8 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-15-pg-projection1.0.0u24.aarch64pigsty3.8 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~noble_all.deb
postgresql-15-pg-projection1.0.0u26.x86_64pigsty3.9 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~resolute_all.deb
postgresql-15-pg-projection1.0.0u26.aarch64pigsty3.9 KiBpostgresql-15-pg-projection_1.0.0-1PGSTY~resolute_all.deb

源码

pig build pkg pg_projection;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

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

使用 pig 安装扩展。

pig install pg_projection;		# install by package name

pig install pg_projection -v 19;   # install for PG 19
pig install pg_projection -v 18;   # install for PG 18
pig install pg_projection -v 17;   # install for PG 17
pig install pg_projection -v 16;   # install for PG 16
pig install pg_projection -v 15;   # install for PG 15

在数据库中创建扩展。

CREATE EXTENSION pg_projection;

用法

pg_projection 为 PostgreSQL jsonb 提供类似 MongoDB 的读取投影能力。1.0 SQL 文件定义了两个函数:pg_project(jsonb, jsonb) 用于单个 JSON 文档,pg_project_set(text, jsonb) 用于把查询结果转换为 JSON 数组后再做投影。

投影单个 JSONB 值

投影值使用数字标志:1 表示包含字段,0 表示排除字段。

CREATE EXTENSION pg_projection;

SELECT pg_project(
  '{"_id": 7, "name": "Ada", "email": "ada@example.test", "secret": "x"}'::jsonb,
  '{"name": 1, "email": 1}'::jsonb
);
-- {"_id": 7, "name": "Ada", "email": "ada@example.test"}

在包含模式下,如果 _id 存在,默认会被保留。调用方只想返回选中字段时,需要显式排除它:

SELECT pg_project(
  '{"_id": 7, "name": "Ada", "email": "ada@example.test"}'::jsonb,
  '{"_id": 0, "name": 1}'::jsonb
);
-- {"name": "Ada"}

排除字段

当投影使用 0 时,函数会从原始文档出发,移除匹配的顶层 key:

SELECT pg_project(
  '{"name": "Ada", "internal_id": "a-1", "secret_key": "k"}'::jsonb,
  '{"internal_id": 0, "secret_key": 0}'::jsonb
);
-- {"name": "Ada"}

投影查询结果

pg_project_set(query_text, projection_json) 会执行传入的 SQL 文本,用 to_jsonb(t) 转换每一行,应用 pg_project,并返回 JSON 数组:

SELECT pg_project_set(
  'SELECT id, username, password_hash FROM users WHERE active',
  '{"password_hash": 0}'::jsonb
);

由于 query_text 是动态 SQL,只应传入由你控制的应用代码或迁移代码组装出的可信查询字符串。不要把不可信的用户输入拼接进这个参数。

注意事项

  • SQL 实现只投影顶层 key;它没有实现 MongoDB 的嵌套路径投影。
  • 投影值会在内部转换为整数,因此应使用数字 0 和 1 标志。
  • pg_project(jsonb, jsonb) 声明为 IMMUTABLE STRICT;pg_project_set(text, jsonb) 声明为 STABLE。

表格阅读器