跳转到主要内容

pg_jsonschema

提供JSON Schema校验能力

pg_jsonschema · 提供JSON Schema校验能力

概览

ID扩展软件包版本类别许可证编程语言
2750pg_jsonschemapg_jsonschema0.3.4FEATApache-2.0Rust
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d--否是否是否否

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.3.419 18 17 16 15pg_jsonschema-
RPMPIGSTY0.3.419 18 17 16 15pg_jsonschema_$v-
DEBPIGSTY0.3.419 18 17 16 15postgresql-$v-pg-jsonschema-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
el8.aarch64N/APIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
el9.x86_64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
el9.aarch64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
el10.x86_64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
el10.aarch64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
d12.x86_64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
d12.aarch64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
d13.x86_64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
d13.aarch64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
u22.x86_64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
u22.aarch64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
u24.x86_64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
u24.aarch64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
u26.x86_64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
u26.aarch64PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4PIGSTY 0.3.4
软件包版本平台仓库大小下载
pg_jsonschema_190.3.4el9.x86_64pigsty2.3 MiBpg_jsonschema_19-0.3.4-4PGSTY.el9.x86_64.rpm
pg_jsonschema_190.3.4el9.aarch64pigsty2.2 MiBpg_jsonschema_19-0.3.4-4PGSTY.el9.aarch64.rpm
pg_jsonschema_190.3.4el10.x86_64pigsty2.3 MiBpg_jsonschema_19-0.3.4-4PGSTY.el10.x86_64.rpm
pg_jsonschema_190.3.4el10.aarch64pigsty2.2 MiBpg_jsonschema_19-0.3.4-4PGSTY.el10.aarch64.rpm
postgresql-19-pg-jsonschema0.3.4d12.x86_64pigsty1.6 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~bookworm_amd64.deb
postgresql-19-pg-jsonschema0.3.4d12.aarch64pigsty1.4 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~bookworm_arm64.deb
postgresql-19-pg-jsonschema0.3.4d13.x86_64pigsty1.6 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~trixie_amd64.deb
postgresql-19-pg-jsonschema0.3.4d13.aarch64pigsty1.4 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~trixie_arm64.deb
postgresql-19-pg-jsonschema0.3.4u22.x86_64pigsty1.8 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~jammy_amd64.deb
postgresql-19-pg-jsonschema0.3.4u22.aarch64pigsty1.6 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~jammy_arm64.deb
postgresql-19-pg-jsonschema0.3.4u24.x86_64pigsty1.8 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~noble_amd64.deb
postgresql-19-pg-jsonschema0.3.4u24.aarch64pigsty1.6 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~noble_arm64.deb
postgresql-19-pg-jsonschema0.3.4u26.x86_64pigsty1.7 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~resolute_amd64.deb
postgresql-19-pg-jsonschema0.3.4u26.aarch64pigsty1.6 MiBpostgresql-19-pg-jsonschema_0.3.4-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_jsonschema_180.3.4el8.x86_64pigsty2.1 MiBpg_jsonschema_18-0.3.4-3PIGSTY.el8.x86_64.rpm
pg_jsonschema_180.3.4el8.aarch64pigsty1.8 MiBpg_jsonschema_18-0.3.4-3PIGSTY.el8.aarch64.rpm
pg_jsonschema_180.3.4el9.x86_64pigsty2.3 MiBpg_jsonschema_18-0.3.4-4PGSTY.el9.x86_64.rpm
pg_jsonschema_180.3.4el9.aarch64pigsty2.2 MiBpg_jsonschema_18-0.3.4-4PGSTY.el9.aarch64.rpm
pg_jsonschema_180.3.4el10.x86_64pigsty2.3 MiBpg_jsonschema_18-0.3.4-4PGSTY.el10.x86_64.rpm
pg_jsonschema_180.3.4el10.aarch64pigsty2.2 MiBpg_jsonschema_18-0.3.4-4PGSTY.el10.aarch64.rpm
postgresql-18-pg-jsonschema0.3.4d12.x86_64pigsty1.6 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~bookworm_amd64.deb
postgresql-18-pg-jsonschema0.3.4d12.aarch64pigsty1.4 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~bookworm_arm64.deb
postgresql-18-pg-jsonschema0.3.4d13.x86_64pigsty1.6 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~trixie_amd64.deb
postgresql-18-pg-jsonschema0.3.4d13.aarch64pigsty1.4 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~trixie_arm64.deb
postgresql-18-pg-jsonschema0.3.4u22.x86_64pigsty1.8 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~jammy_amd64.deb
postgresql-18-pg-jsonschema0.3.4u22.aarch64pigsty1.6 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~jammy_arm64.deb
postgresql-18-pg-jsonschema0.3.4u24.x86_64pigsty1.8 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~noble_amd64.deb
postgresql-18-pg-jsonschema0.3.4u24.aarch64pigsty1.6 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~noble_arm64.deb
postgresql-18-pg-jsonschema0.3.4u26.x86_64pigsty1.7 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~resolute_amd64.deb
postgresql-18-pg-jsonschema0.3.4u26.aarch64pigsty1.6 MiBpostgresql-18-pg-jsonschema_0.3.4-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_jsonschema_170.3.4el8.x86_64pigsty2.0 MiBpg_jsonschema_17-0.3.4-3PIGSTY.el8.x86_64.rpm
pg_jsonschema_170.3.4el8.aarch64pigsty1.8 MiBpg_jsonschema_17-0.3.4-3PIGSTY.el8.aarch64.rpm
pg_jsonschema_170.3.4el9.x86_64pigsty2.3 MiBpg_jsonschema_17-0.3.4-4PGSTY.el9.x86_64.rpm
pg_jsonschema_170.3.4el9.aarch64pigsty2.2 MiBpg_jsonschema_17-0.3.4-4PGSTY.el9.aarch64.rpm
pg_jsonschema_170.3.4el10.x86_64pigsty2.3 MiBpg_jsonschema_17-0.3.4-4PGSTY.el10.x86_64.rpm
pg_jsonschema_170.3.4el10.aarch64pigsty2.2 MiBpg_jsonschema_17-0.3.4-4PGSTY.el10.aarch64.rpm
postgresql-17-pg-jsonschema0.3.4d12.x86_64pigsty1.6 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~bookworm_amd64.deb
postgresql-17-pg-jsonschema0.3.4d12.aarch64pigsty1.4 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~bookworm_arm64.deb
postgresql-17-pg-jsonschema0.3.4d13.x86_64pigsty1.6 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~trixie_amd64.deb
postgresql-17-pg-jsonschema0.3.4d13.aarch64pigsty1.4 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~trixie_arm64.deb
postgresql-17-pg-jsonschema0.3.4u22.x86_64pigsty1.8 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~jammy_amd64.deb
postgresql-17-pg-jsonschema0.3.4u22.aarch64pigsty1.6 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~jammy_arm64.deb
postgresql-17-pg-jsonschema0.3.4u24.x86_64pigsty1.8 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~noble_amd64.deb
postgresql-17-pg-jsonschema0.3.4u24.aarch64pigsty1.6 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~noble_arm64.deb
postgresql-17-pg-jsonschema0.3.4u26.x86_64pigsty1.7 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~resolute_amd64.deb
postgresql-17-pg-jsonschema0.3.4u26.aarch64pigsty1.6 MiBpostgresql-17-pg-jsonschema_0.3.4-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_jsonschema_160.3.4el8.x86_64pigsty2.0 MiBpg_jsonschema_16-0.3.4-3PIGSTY.el8.x86_64.rpm
pg_jsonschema_160.3.4el8.aarch64pigsty1.8 MiBpg_jsonschema_16-0.3.4-3PIGSTY.el8.aarch64.rpm
pg_jsonschema_160.3.4el9.x86_64pigsty2.3 MiBpg_jsonschema_16-0.3.4-4PGSTY.el9.x86_64.rpm
pg_jsonschema_160.3.4el9.aarch64pigsty2.2 MiBpg_jsonschema_16-0.3.4-4PGSTY.el9.aarch64.rpm
pg_jsonschema_160.3.4el10.x86_64pigsty2.3 MiBpg_jsonschema_16-0.3.4-4PGSTY.el10.x86_64.rpm
pg_jsonschema_160.3.4el10.aarch64pigsty2.2 MiBpg_jsonschema_16-0.3.4-4PGSTY.el10.aarch64.rpm
postgresql-16-pg-jsonschema0.3.4d12.x86_64pigsty1.6 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~bookworm_amd64.deb
postgresql-16-pg-jsonschema0.3.4d12.aarch64pigsty1.4 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~bookworm_arm64.deb
postgresql-16-pg-jsonschema0.3.4d13.x86_64pigsty1.6 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~trixie_amd64.deb
postgresql-16-pg-jsonschema0.3.4d13.aarch64pigsty1.4 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~trixie_arm64.deb
postgresql-16-pg-jsonschema0.3.4u22.x86_64pigsty1.8 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~jammy_amd64.deb
postgresql-16-pg-jsonschema0.3.4u22.aarch64pigsty1.6 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~jammy_arm64.deb
postgresql-16-pg-jsonschema0.3.4u24.x86_64pigsty1.8 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~noble_amd64.deb
postgresql-16-pg-jsonschema0.3.4u24.aarch64pigsty1.6 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~noble_arm64.deb
postgresql-16-pg-jsonschema0.3.4u26.x86_64pigsty1.7 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~resolute_amd64.deb
postgresql-16-pg-jsonschema0.3.4u26.aarch64pigsty1.6 MiBpostgresql-16-pg-jsonschema_0.3.4-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_jsonschema_150.3.4el8.x86_64pigsty2.0 MiBpg_jsonschema_15-0.3.4-3PIGSTY.el8.x86_64.rpm
pg_jsonschema_150.3.4el8.aarch64pigsty1.8 MiBpg_jsonschema_15-0.3.4-3PIGSTY.el8.aarch64.rpm
pg_jsonschema_150.3.4el9.x86_64pigsty2.3 MiBpg_jsonschema_15-0.3.4-4PGSTY.el9.x86_64.rpm
pg_jsonschema_150.3.4el9.aarch64pigsty2.1 MiBpg_jsonschema_15-0.3.4-4PGSTY.el9.aarch64.rpm
pg_jsonschema_150.3.4el10.x86_64pigsty2.3 MiBpg_jsonschema_15-0.3.4-4PGSTY.el10.x86_64.rpm
pg_jsonschema_150.3.4el10.aarch64pigsty2.2 MiBpg_jsonschema_15-0.3.4-4PGSTY.el10.aarch64.rpm
postgresql-15-pg-jsonschema0.3.4d12.x86_64pigsty1.6 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~bookworm_amd64.deb
postgresql-15-pg-jsonschema0.3.4d12.aarch64pigsty1.4 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~bookworm_arm64.deb
postgresql-15-pg-jsonschema0.3.4d13.x86_64pigsty1.6 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~trixie_amd64.deb
postgresql-15-pg-jsonschema0.3.4d13.aarch64pigsty1.4 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~trixie_arm64.deb
postgresql-15-pg-jsonschema0.3.4u22.x86_64pigsty1.8 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~jammy_amd64.deb
postgresql-15-pg-jsonschema0.3.4u22.aarch64pigsty1.6 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~jammy_arm64.deb
postgresql-15-pg-jsonschema0.3.4u24.x86_64pigsty1.7 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~noble_amd64.deb
postgresql-15-pg-jsonschema0.3.4u24.aarch64pigsty1.6 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~noble_arm64.deb
postgresql-15-pg-jsonschema0.3.4u26.x86_64pigsty1.7 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~resolute_amd64.deb
postgresql-15-pg-jsonschema0.3.4u26.aarch64pigsty1.6 MiBpostgresql-15-pg-jsonschema_0.3.4-4PGSTY~resolute_arm64.deb

源码

pig build pkg pg_jsonschema;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install pg_jsonschema;		# install by package name

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

在数据库中创建扩展。

CREATE EXTENSION pg_jsonschema;

用法

pg_jsonschema: 提供 JSON Schema 校验的 PostgreSQL 扩展

pg_jsonschema 为 PostgreSQL 添加了 JSON Schema 校验函数,可通过检查约束对 JSON/JSONB 列实施模式验证。

函数

函数描述
json_matches_schema(schema json, instance json)验证 JSON 实例是否符合 schema,返回布尔值
jsonb_matches_schema(schema json, instance jsonb)验证 JSONB 实例是否符合 schema,返回布尔值
jsonschema_is_valid(schema json)检查 JSON schema 本身是否合法
jsonschema_validation_errors(schema json, instance json)返回校验错误消息数组

表约束

使用检查约束强制文档结构:

CREATE TABLE customer (
    id serial PRIMARY KEY,
    metadata json,
    CHECK (
        json_matches_schema(
            '{
                "type": "object",
                "properties": {
                    "tags": {
                        "type": "array",
                        "items": {
                            "type": "string",
                            "maxLength": 16
                        }
                    }
                }
            }',
            metadata
        )
    )
);

-- 有效插入(通过检查约束)
INSERT INTO customer(metadata) VALUES ('{"tags": ["vip", "darkmode-ui"]}');

-- 无效插入(被检查约束拒绝)
INSERT INTO customer(metadata) VALUES ('{"tags": [1, 3]}');
-- ERROR: new row violates check constraint

错误检查

获取详细的校验错误信息:

SELECT jsonschema_validation_errors('{"maxLength": 4}', '"123456789"');
-- 返回: {"\"123456789\" is longer than 4 characters"}

模式校验

使用前验证 schema 是否格式正确:

SELECT jsonschema_is_valid('{
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age":  {"type": "integer", "minimum": 0}
    },
    "required": ["name"]
}');
-- 返回: true

表格阅读器