pg_protobuf
提供Protobuf函数支持
仓库
afiskon/pg_protobuf
https://github.com/afiskon/pg_protobuf
源码
pg_protobuf-1.0.tar.gz
pg_protobuf-1.0.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_protobuf | 1.0 | UTIL | MIT | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 4260 | pg_protobuf | 否 | 是 | 否 | 是 | 否 | 是 | - |
| 相关扩展 | pgjq pgqr gzip bzip zstd http pg_net pg_curl |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.0 | 1817161514 | pg_protobuf | - |
| RPM | PIGSTY | 1.0 | 1817161514 | pg_protobuf_$v | - |
| DEB | PIGSTY | 1.0 | 1817161514 | postgresql-$v-pg-protobuf | - |
构建
您可以使用 pig build 命令构建 pg_protobuf 扩展的 RPM / DEB 包:
pig build pkg pg_protobuf # 构建 RPM / DEB 包
安装
您可以直接安装 pg_protobuf 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pg_protobuf; # 当前活跃 PG 版本安装
pig ext install -y pg_protobuf -v 18 # PG 18
pig ext install -y pg_protobuf -v 17 # PG 17
pig ext install -y pg_protobuf -v 16 # PG 16
pig ext install -y pg_protobuf -v 15 # PG 15
pig ext install -y pg_protobuf -v 14 # PG 14
dnf install -y pg_protobuf_18 # PG 18
dnf install -y pg_protobuf_17 # PG 17
dnf install -y pg_protobuf_16 # PG 16
dnf install -y pg_protobuf_15 # PG 15
dnf install -y pg_protobuf_14 # PG 14
apt install -y postgresql-18-pg-protobuf # PG 18
apt install -y postgresql-17-pg-protobuf # PG 17
apt install -y postgresql-16-pg-protobuf # PG 16
apt install -y postgresql-15-pg-protobuf # PG 15
apt install -y postgresql-14-pg-protobuf # PG 14
创建扩展:
CREATE EXTENSION pg_protobuf;
用法
提供无需 schema 定义即可在 SQL 中直接解码 Protocol Buffer 二进制数据的函数。
函数
protobuf_decode(bytea) RETURNS cstring– 将 protobuf 二进制数据解码为可读格式protobuf_get_int(bytea, int) RETURNS bigint– 按字段编号提取整数字段protobuf_get_string(bytea, int) RETURNS text– 按字段编号提取字符串字段protobuf_get_bytes(bytea, int) RETURNS bytea– 按字段编号提取原始字节protobuf_get_bool(bytea, int) RETURNS boolean– 按字段编号提取布尔字段protobuf_get_float(bytea, int) RETURNS real– 按字段编号提取浮点字段protobuf_get_double(bytea, int) RETURNS double precision– 按字段编号提取双精度字段protobuf_get_sfixed32(bytea, int) RETURNS int– 提取有符号定长 32 位字段protobuf_get_sfixed64(bytea, int) RETURNS bigint– 提取有符号定长 64 位字段protobuf_get_int_multi(bytea, int) RETURNS bigint[]– 提取重复整数字段protobuf_get_string_multi(bytea, int) RETURNS text[]– 提取重复字符串字段protobuf_get_bytes_multi(bytea, int) RETURNS bytea[]– 提取重复字节字段protobuf_get_bool_multi(bytea, int) RETURNS boolean[]– 提取重复布尔字段protobuf_get_float_multi(bytea, int) RETURNS real[]– 提取重复浮点字段protobuf_get_double_multi(bytea, int) RETURNS double precision[]– 提取重复双精度字段protobuf_get_sfixed32_multi(bytea, int) RETURNS int[]– 提取重复 sfixed32 字段protobuf_get_sfixed64_multi(bytea, int) RETURNS bigint[]– 提取重复 sfixed64 字段
示例
CREATE EXTENSION pg_protobuf;
-- 创建包含 protobuf 数据的表
CREATE TABLE heroes (x bytea);
-- 为特定字段定义访问函数
CREATE FUNCTION hero_name(x bytea) RETURNS text AS $$
BEGIN
RETURN protobuf_get_string(x, 512);
END $$ LANGUAGE plpgsql IMMUTABLE;
CREATE FUNCTION hero_hp(x bytea) RETURNS bigint AS $$
BEGIN
RETURN protobuf_get_int(x, 2);
END $$ LANGUAGE plpgsql IMMUTABLE;
-- 在 protobuf 字段上创建索引
CREATE INDEX hero_name_idx ON heroes USING btree(hero_name(x));
-- 按 protobuf 字段查询
SELECT hero_name(x) FROM heroes ORDER BY hero_name(x) LIMIT 10;
限制
- 不支持修改 Protobuf 数据
- 枚举值可通过
protobuf_get_int读取 - 不直接支持无符号类型(PostgreSQL 中没有无符号整数)
*_multi函数不支持[packed=true](可使用protobuf_get_bytes*替代)