pgproto
pgproto · 原生 Protobuf 解析、修改、索引与 JSON 转换支持
概览
| ID | 扩展 | 软件包 | 版本 | 类别 | 许可证 | 编程语言 |
|---|---|---|---|---|---|---|
| 4130 | pgproto | pgproto | 0.5.0 | UTIL | PostgreSQL | C |
| 属性 | 可执行文件 | 动态库 | 需要预加载 | 需要建扩展 | 可迁移 | 可信 |
|---|---|---|---|---|---|---|
| --s-d-r | 否 | 是 | 否 | 是 | 是 | 否 |
| 关联 | |
|---|---|
| 相关扩展 | pg_protobuf decoderbufs wal2json pgoutput test_decoding pgmqtt kafka_fdw pgq pg_logicalinspect |
软件包
| Linux / PG | PG19 | PG18 | PG17 | PG16 | PG15 |
|---|---|---|---|---|---|
| el8.x86_64 | N/A | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| el8.aarch64 | N/A | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| el9.x86_64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| el9.aarch64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| el10.x86_64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| el10.aarch64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| d12.x86_64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| d12.aarch64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| d13.x86_64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| d13.aarch64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| u22.x86_64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| u22.aarch64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| u24.x86_64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| u24.aarch64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| u26.x86_64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
| u26.aarch64 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 | PIGSTY 0.5.0 |
源码
安装
在数据库中创建扩展。
用法
来源: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 库。
Schema 注册与存储
pgproto 需要运行时 protobuf descriptor,才能用名称/路径提取解释二进制 payload。可以把序列化后的 FileDescriptorSet 注册到 pb_schemas,也可以在适合的工作流中调用 SQL 注册辅助函数:
把序列化后的 protobuf bytes 存入 protobuf 列:
0.5.0 SQL 还安装了从 protobuf 到 bytea 的便捷 cast,因此需要时可以使用 length(data::bytea) 这类面向 byte 的函数。
查询
对嵌套、repeated 和 map 字段使用路径操作符:
扩展定义的其他面向用户的提取辅助函数和操作符包括:
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。
使用 || 操作符合并两个 protobuf 值,它会调用 pb_merge:
索引与包含
可以对提取字段建立普通表达式索引:
SQL definitions 还暴露了 protobuf 包含操作符 @>,以及用于 GIN 索引的默认 protobuf_gin_ops operator class:
Schema 演进
README 将 schema 演进描述为常规用例:新增字段从旧消息读取为 NULL,deprecated 或 unknown 字段会在遍历时跳过,enum 按标准 varint 读取,未设置的 oneof 字段返回 NULL。
注意事项
- schema 感知的路径导航需要运行时 schema;没有已注册 descriptor 时,扩展无法解析 message field name。
#>返回int4,#>>返回text;应按预期字段类型选择操作符/函数。- mutation 辅助函数不会原地更新行;必须把返回值赋回列。
- README 中的 benchmark 数字是上游项目 benchmark,不是独立性能保证。