跳转到主要内容

pgproto

原生 Protobuf 解析、修改、索引与 JSON 转换支持

pgproto · 原生 Protobuf 解析、修改、索引与 JSON 转换支持

概览

ID扩展软件包版本类别许可证编程语言
4130pgprotopgproto0.5.0UTILPostgreSQLC
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d-r否是否是是否
SQL 1.0.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.5.019 18 17 16 15pgproto-
RPMPIGSTY0.5.019 18 17 16 15pgproto_$v-
DEBPIGSTY0.5.019 18 17 16 15postgresql-$v-pgproto-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
el8.aarch64N/APIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
el9.x86_64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
el9.aarch64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
el10.x86_64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
el10.aarch64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
d12.x86_64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
d12.aarch64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
d13.x86_64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
d13.aarch64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
u22.x86_64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
u22.aarch64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
u24.x86_64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
u24.aarch64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
u26.x86_64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
u26.aarch64PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0PIGSTY 0.5.0
软件包版本平台仓库大小下载
pgproto_190.5.0el9.x86_64pigsty64.5 KiBpgproto_19-0.5.0-1PGSTY.el9.x86_64.rpm
pgproto_190.5.0el9.aarch64pigsty63.2 KiBpgproto_19-0.5.0-1PGSTY.el9.aarch64.rpm
pgproto_190.5.0el10.x86_64pigsty65.3 KiBpgproto_19-0.5.0-1PGSTY.el10.x86_64.rpm
pgproto_190.5.0el10.aarch64pigsty63.8 KiBpgproto_19-0.5.0-1PGSTY.el10.aarch64.rpm
postgresql-19-pgproto0.5.0d12.x86_64pigsty52.5 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~bookworm_amd64.deb
postgresql-19-pgproto0.5.0d12.aarch64pigsty50.9 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~bookworm_arm64.deb
postgresql-19-pgproto0.5.0d13.x86_64pigsty52.4 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~trixie_amd64.deb
postgresql-19-pgproto0.5.0d13.aarch64pigsty51.0 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~trixie_arm64.deb
postgresql-19-pgproto0.5.0u22.x86_64pigsty54.8 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~jammy_amd64.deb
postgresql-19-pgproto0.5.0u22.aarch64pigsty53.8 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~jammy_arm64.deb
postgresql-19-pgproto0.5.0u24.x86_64pigsty54.8 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~noble_amd64.deb
postgresql-19-pgproto0.5.0u24.aarch64pigsty53.8 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~noble_arm64.deb
postgresql-19-pgproto0.5.0u26.x86_64pigsty54.3 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~resolute_amd64.deb
postgresql-19-pgproto0.5.0u26.aarch64pigsty53.5 KiBpostgresql-19-pgproto_0.5.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pgproto_180.5.0el8.x86_64pigsty64.1 KiBpgproto_18-0.5.0-1PGSTY.el8.x86_64.rpm
pgproto_180.5.0el8.aarch64pigsty62.3 KiBpgproto_18-0.5.0-1PGSTY.el8.aarch64.rpm
pgproto_180.5.0el9.x86_64pigsty64.5 KiBpgproto_18-0.5.0-1PGSTY.el9.x86_64.rpm
pgproto_180.5.0el9.aarch64pigsty63.2 KiBpgproto_18-0.5.0-1PGSTY.el9.aarch64.rpm
pgproto_180.5.0el10.x86_64pigsty65.3 KiBpgproto_18-0.5.0-1PGSTY.el10.x86_64.rpm
pgproto_180.5.0el10.aarch64pigsty63.8 KiBpgproto_18-0.5.0-1PGSTY.el10.aarch64.rpm
postgresql-18-pgproto0.5.0d12.x86_64pigsty52.5 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~bookworm_amd64.deb
postgresql-18-pgproto0.5.0d12.aarch64pigsty50.9 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~bookworm_arm64.deb
postgresql-18-pgproto0.5.0d13.x86_64pigsty52.4 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~trixie_amd64.deb
postgresql-18-pgproto0.5.0d13.aarch64pigsty51.0 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~trixie_arm64.deb
postgresql-18-pgproto0.5.0u22.x86_64pigsty54.8 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~jammy_amd64.deb
postgresql-18-pgproto0.5.0u22.aarch64pigsty53.8 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~jammy_arm64.deb
postgresql-18-pgproto0.5.0u24.x86_64pigsty54.9 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~noble_amd64.deb
postgresql-18-pgproto0.5.0u24.aarch64pigsty53.7 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~noble_arm64.deb
postgresql-18-pgproto0.5.0u26.x86_64pigsty54.3 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~resolute_amd64.deb
postgresql-18-pgproto0.5.0u26.aarch64pigsty53.5 KiBpostgresql-18-pgproto_0.5.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pgproto_170.5.0el8.x86_64pigsty64.1 KiBpgproto_17-0.5.0-1PGSTY.el8.x86_64.rpm
pgproto_170.5.0el8.aarch64pigsty62.2 KiBpgproto_17-0.5.0-1PGSTY.el8.aarch64.rpm
pgproto_170.5.0el9.x86_64pigsty64.5 KiBpgproto_17-0.5.0-1PGSTY.el9.x86_64.rpm
pgproto_170.5.0el9.aarch64pigsty63.1 KiBpgproto_17-0.5.0-1PGSTY.el9.aarch64.rpm
pgproto_170.5.0el10.x86_64pigsty65.3 KiBpgproto_17-0.5.0-1PGSTY.el10.x86_64.rpm
pgproto_170.5.0el10.aarch64pigsty63.7 KiBpgproto_17-0.5.0-1PGSTY.el10.aarch64.rpm
postgresql-17-pgproto0.5.0d12.x86_64pigsty52.5 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~bookworm_amd64.deb
postgresql-17-pgproto0.5.0d12.aarch64pigsty50.8 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~bookworm_arm64.deb
postgresql-17-pgproto0.5.0d13.x86_64pigsty52.4 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~trixie_amd64.deb
postgresql-17-pgproto0.5.0d13.aarch64pigsty51.0 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~trixie_arm64.deb
postgresql-17-pgproto0.5.0u22.x86_64pigsty57.6 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~jammy_amd64.deb
postgresql-17-pgproto0.5.0u22.aarch64pigsty56.4 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~jammy_arm64.deb
postgresql-17-pgproto0.5.0u24.x86_64pigsty54.8 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~noble_amd64.deb
postgresql-17-pgproto0.5.0u24.aarch64pigsty53.7 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~noble_arm64.deb
postgresql-17-pgproto0.5.0u26.x86_64pigsty54.3 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~resolute_amd64.deb
postgresql-17-pgproto0.5.0u26.aarch64pigsty53.5 KiBpostgresql-17-pgproto_0.5.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pgproto_160.5.0el8.x86_64pigsty64.1 KiBpgproto_16-0.5.0-1PGSTY.el8.x86_64.rpm
pgproto_160.5.0el8.aarch64pigsty62.2 KiBpgproto_16-0.5.0-1PGSTY.el8.aarch64.rpm
pgproto_160.5.0el9.x86_64pigsty64.5 KiBpgproto_16-0.5.0-1PGSTY.el9.x86_64.rpm
pgproto_160.5.0el9.aarch64pigsty63.1 KiBpgproto_16-0.5.0-1PGSTY.el9.aarch64.rpm
pgproto_160.5.0el10.x86_64pigsty65.3 KiBpgproto_16-0.5.0-1PGSTY.el10.x86_64.rpm
pgproto_160.5.0el10.aarch64pigsty63.7 KiBpgproto_16-0.5.0-1PGSTY.el10.aarch64.rpm
postgresql-16-pgproto0.5.0d12.x86_64pigsty52.5 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~bookworm_amd64.deb
postgresql-16-pgproto0.5.0d12.aarch64pigsty50.9 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~bookworm_arm64.deb
postgresql-16-pgproto0.5.0d13.x86_64pigsty52.4 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~trixie_amd64.deb
postgresql-16-pgproto0.5.0d13.aarch64pigsty50.9 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~trixie_arm64.deb
postgresql-16-pgproto0.5.0u22.x86_64pigsty57.6 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~jammy_amd64.deb
postgresql-16-pgproto0.5.0u22.aarch64pigsty56.4 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~jammy_arm64.deb
postgresql-16-pgproto0.5.0u24.x86_64pigsty54.8 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~noble_amd64.deb
postgresql-16-pgproto0.5.0u24.aarch64pigsty53.7 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~noble_arm64.deb
postgresql-16-pgproto0.5.0u26.x86_64pigsty54.3 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~resolute_amd64.deb
postgresql-16-pgproto0.5.0u26.aarch64pigsty53.5 KiBpostgresql-16-pgproto_0.5.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pgproto_150.5.0el8.x86_64pigsty64.1 KiBpgproto_15-0.5.0-1PGSTY.el8.x86_64.rpm
pgproto_150.5.0el8.aarch64pigsty62.3 KiBpgproto_15-0.5.0-1PGSTY.el8.aarch64.rpm
pgproto_150.5.0el9.x86_64pigsty64.6 KiBpgproto_15-0.5.0-1PGSTY.el9.x86_64.rpm
pgproto_150.5.0el9.aarch64pigsty63.2 KiBpgproto_15-0.5.0-1PGSTY.el9.aarch64.rpm
pgproto_150.5.0el10.x86_64pigsty65.5 KiBpgproto_15-0.5.0-1PGSTY.el10.x86_64.rpm
pgproto_150.5.0el10.aarch64pigsty63.8 KiBpgproto_15-0.5.0-1PGSTY.el10.aarch64.rpm
postgresql-15-pgproto0.5.0d12.x86_64pigsty52.6 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~bookworm_amd64.deb
postgresql-15-pgproto0.5.0d12.aarch64pigsty50.9 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~bookworm_arm64.deb
postgresql-15-pgproto0.5.0d13.x86_64pigsty52.4 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~trixie_amd64.deb
postgresql-15-pgproto0.5.0d13.aarch64pigsty51.1 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~trixie_arm64.deb
postgresql-15-pgproto0.5.0u22.x86_64pigsty57.7 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~jammy_amd64.deb
postgresql-15-pgproto0.5.0u22.aarch64pigsty56.5 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~jammy_arm64.deb
postgresql-15-pgproto0.5.0u24.x86_64pigsty54.8 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~noble_amd64.deb
postgresql-15-pgproto0.5.0u24.aarch64pigsty53.7 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~noble_arm64.deb
postgresql-15-pgproto0.5.0u26.x86_64pigsty54.3 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~resolute_amd64.deb
postgresql-15-pgproto0.5.0u26.aarch64pigsty53.5 KiBpostgresql-15-pgproto_0.5.0-1PGSTY~resolute_arm64.deb

源码

pig build pkg pgproto;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install pgproto;		# install by package name

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

在数据库中创建扩展。

CREATE EXTENSION pgproto;

用法

来源:README, release 0.5.0, PGXN 0.5.0, SQL definitions, Makefile, pgproto.control

pgproto 在 PostgreSQL 中以原生 protobuf 类型存储 Protocol Buffers proto3 payload,提供 schema 感知提取、更新辅助函数、包含/索引支持,以及文本/整数路径操作符。上游包版本为 0.5.0;扩展 SQL/control 默认版本仍为 1.0。

当前上游源码是 C/PGXS 扩展:官方 Makefile 设置 MODULE_big = pgproto,从 src/*.o 构建 C object,并包含 $(PGXS)。README 描述该实现为纯 C,不依赖外部 Protobuf 库。

CREATE EXTENSION pgproto;

Schema 注册与存储

pgproto 需要运行时 protobuf descriptor,才能用名称/路径提取解释二进制 payload。可以把序列化后的 FileDescriptorSet 注册到 pb_schemas,也可以在适合的工作流中调用 SQL 注册辅助函数:

INSERT INTO pb_schemas (name, data)
VALUES ('MySchema', '\x...');

SELECT pb_register_schema('MySchema', '\x...');

把序列化后的 protobuf bytes 存入 protobuf 列:

CREATE TABLE items (
  id serial PRIMARY KEY,
  data protobuf
);

INSERT INTO items (data) VALUES ('\x0a02082a');

0.5.0 SQL 还安装了从 protobuf 到 bytea 的便捷 cast,因此需要时可以使用 length(data::bytea) 这类面向 byte 的函数。

查询

对嵌套、repeated 和 map 字段使用路径操作符:

-- Integer accessor: returns int4
SELECT data #> '{Outer, inner, id}'::text[] FROM items;

-- Text accessor: returns text
SELECT data #>> '{Outer, tags, mykey}'::text[] FROM items;

-- Array index lookup
SELECT data #> '{Outer, scores, 0}'::text[] FROM items;

扩展定义的其他面向用户的提取辅助函数和操作符包括:

  • pb_get_int32(protobuf, int4):按 tag 提取 int4。
  • pb_get_int32_by_name(protobuf, text, text) 与 pb_get_int32_by_name_dot(protobuf, text):按名称提取整数。
  • ->:通过 pb_get_int32_by_name_dot 进行 dot-path 整数查找的简写。
  • pb_get_int32_by_path(protobuf, text[]):支撑 #>。
  • pb_get_text_by_path(protobuf, text[]):支撑 #>>。
  • pb_to_json(protobuf, text):在提供 message name 时转换为 text JSON。

更新与合并

pb_set、pb_insert 和 pb_delete 都是纯函数:它们返回新的 protobuf 值,因此需要用 UPDATE ... SET 持久化修改。上游 0.5.0 文档说明这些 mutation 会自动 compaction,以移除 stale tags。

UPDATE items
SET data = pb_set(data, ARRAY['Outer', 'a'], '42');

UPDATE items
SET data = pb_insert(data, ARRAY['Outer', 'scores', '0'], '100');

UPDATE items
SET data = pb_insert(data, ARRAY['Outer', 'tags', 'key1'], 'value1');

UPDATE items
SET data = pb_delete(data, ARRAY['Outer', 'a']);

使用 || 操作符合并两个 protobuf 值,它会调用 pb_merge:

UPDATE items
SET data = data || other.data
FROM other
WHERE items.id = other.id;

索引与包含

可以对提取字段建立普通表达式索引:

CREATE INDEX idx_items_pb_id
ON items ((data #> '{Outer, inner, id}'::text[]));

SELECT *
FROM items
WHERE (data #> '{Outer, inner, id}'::text[]) = 42;

SQL definitions 还暴露了 protobuf 包含操作符 @>,以及用于 GIN 索引的默认 protobuf_gin_ops operator class:

CREATE INDEX idx_items_data_gin
ON items USING gin (data protobuf_gin_ops);

SELECT * FROM items WHERE data @> '\x0a02082a'::protobuf;

Schema 演进

README 将 schema 演进描述为常规用例:新增字段从旧消息读取为 NULL,deprecated 或 unknown 字段会在遍历时跳过,enum 按标准 varint 读取,未设置的 oneof 字段返回 NULL。

注意事项

  • schema 感知的路径导航需要运行时 schema;没有已注册 descriptor 时,扩展无法解析 message field name。
  • #> 返回 int4,#>> 返回 text;应按预期字段类型选择操作符/函数。
  • mutation 辅助函数不会原地更新行;必须把返回值赋回列。
  • README 中的 benchmark 数字是上游项目 benchmark,不是独立性能保证。

表格阅读器