跳转到主要内容

pg_describe

不执行查询即可报告其参数与结果列元数据

pg_describe · 不执行查询即可报告其参数与结果列元数据

概览

ID扩展软件包版本类别许可证编程语言
4350pg_describepg_describe1.0.0UTILMITC
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d-r否是否是是否
关联
相关扩展colnames ddlx pg_readme pglinter
Uses PostgreSQL parser and analyzer without invoking the executor; upstream and PIGSTY packages require PostgreSQL 17 or newer.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY1.0.019 18 17 16 15pg_describe-
RPMPIGSTY1.0.019 18 17 16 15pg_describe_$v-
DEBPIGSTY1.0.019 18 17 16 15postgresql-$v-pg-describe-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
el8.aarch64N/APIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
el9.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
el9.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
el10.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
el10.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
d12.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
d12.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
d13.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
d13.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
u22.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
u22.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
u24.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
u24.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
u26.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
u26.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/AN/A
软件包版本平台仓库大小下载
pg_describe_191.0.0el9.x86_64pigsty42.0 KiBpg_describe_19-1.0.0-1PGSTY.el9.x86_64.rpm
pg_describe_191.0.0el9.aarch64pigsty41.8 KiBpg_describe_19-1.0.0-1PGSTY.el9.aarch64.rpm
pg_describe_191.0.0el10.x86_64pigsty41.8 KiBpg_describe_19-1.0.0-1PGSTY.el10.x86_64.rpm
pg_describe_191.0.0el10.aarch64pigsty42.0 KiBpg_describe_19-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-19-pg-describe1.0.0d12.x86_64pigsty36.1 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-19-pg-describe1.0.0d12.aarch64pigsty35.9 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-19-pg-describe1.0.0d13.x86_64pigsty36.0 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-19-pg-describe1.0.0d13.aarch64pigsty36.0 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-19-pg-describe1.0.0u22.x86_64pigsty37.9 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-19-pg-describe1.0.0u22.aarch64pigsty37.9 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-19-pg-describe1.0.0u24.x86_64pigsty37.3 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~noble_amd64.deb
postgresql-19-pg-describe1.0.0u24.aarch64pigsty37.0 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~noble_arm64.deb
postgresql-19-pg-describe1.0.0u26.x86_64pigsty37.3 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-19-pg-describe1.0.0u26.aarch64pigsty37.3 KiBpostgresql-19-pg-describe_1.0.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_describe_181.0.0el8.x86_64pigsty41.8 KiBpg_describe_18-1.0.0-1PGSTY.el8.x86_64.rpm
pg_describe_181.0.0el8.aarch64pigsty42.0 KiBpg_describe_18-1.0.0-1PGSTY.el8.aarch64.rpm
pg_describe_181.0.0el9.x86_64pigsty42.0 KiBpg_describe_18-1.0.0-1PGSTY.el9.x86_64.rpm
pg_describe_181.0.0el9.aarch64pigsty41.8 KiBpg_describe_18-1.0.0-1PGSTY.el9.aarch64.rpm
pg_describe_181.0.0el10.x86_64pigsty41.8 KiBpg_describe_18-1.0.0-1PGSTY.el10.x86_64.rpm
pg_describe_181.0.0el10.aarch64pigsty42.0 KiBpg_describe_18-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-describe1.0.0d12.x86_64pigsty36.1 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-describe1.0.0d12.aarch64pigsty35.9 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-describe1.0.0d13.x86_64pigsty36.0 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-18-pg-describe1.0.0d13.aarch64pigsty36.0 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-18-pg-describe1.0.0u22.x86_64pigsty37.9 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-18-pg-describe1.0.0u22.aarch64pigsty37.9 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-18-pg-describe1.0.0u24.x86_64pigsty37.3 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~noble_amd64.deb
postgresql-18-pg-describe1.0.0u24.aarch64pigsty37.0 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~noble_arm64.deb
postgresql-18-pg-describe1.0.0u26.x86_64pigsty37.3 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-18-pg-describe1.0.0u26.aarch64pigsty37.3 KiBpostgresql-18-pg-describe_1.0.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_describe_171.0.0el8.x86_64pigsty41.7 KiBpg_describe_17-1.0.0-1PGSTY.el8.x86_64.rpm
pg_describe_171.0.0el8.aarch64pigsty41.9 KiBpg_describe_17-1.0.0-1PGSTY.el8.aarch64.rpm
pg_describe_171.0.0el9.x86_64pigsty42.0 KiBpg_describe_17-1.0.0-1PGSTY.el9.x86_64.rpm
pg_describe_171.0.0el9.aarch64pigsty41.8 KiBpg_describe_17-1.0.0-1PGSTY.el9.aarch64.rpm
pg_describe_171.0.0el10.x86_64pigsty41.8 KiBpg_describe_17-1.0.0-1PGSTY.el10.x86_64.rpm
pg_describe_171.0.0el10.aarch64pigsty41.9 KiBpg_describe_17-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-describe1.0.0d12.x86_64pigsty36.0 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-describe1.0.0d12.aarch64pigsty35.9 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-describe1.0.0d13.x86_64pigsty36.0 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-17-pg-describe1.0.0d13.aarch64pigsty35.9 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-17-pg-describe1.0.0u22.x86_64pigsty40.3 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-17-pg-describe1.0.0u22.aarch64pigsty40.3 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-17-pg-describe1.0.0u24.x86_64pigsty37.3 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~noble_amd64.deb
postgresql-17-pg-describe1.0.0u24.aarch64pigsty37.0 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~noble_arm64.deb
postgresql-17-pg-describe1.0.0u26.x86_64pigsty37.2 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-17-pg-describe1.0.0u26.aarch64pigsty37.2 KiBpostgresql-17-pg-describe_1.0.0-1PGSTY~resolute_arm64.deb

源码

pig build pkg pg_describe;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install pg_describe;		# install by package name

pig install pg_describe -v 19;   # install for PG 19
pig install pg_describe -v 18;   # install for PG 18
pig install pg_describe -v 17;   # install for PG 17

在数据库中创建扩展。

CREATE EXTENSION pg_describe;

用法

来源:

pg_describe 可以在不执行 SQL 语句的情况下报告其参数和结果列。它使用 PostgreSQL 的解析和分析能力,推断参数类型、线路协议可见的结果类型、源列来源,以及考虑外连接后的可空性。适用于代码生成、迁移检查和查询契约工具。

描述查询

CREATE EXTENSION pg_describe;

SELECT *
FROM pg_describe(
  'SELECT id, email FROM users WHERE id = $1'
);

kind = 'param' 的行描述 $1、$2 以及后续参数。kind = 'column' 的行描述结果列顺序、名称、类型 OID/名称、源表/列、基础 NOT NULL 状态,以及最终表达式是否确定为非空。

检查连接可空性

SELECT *
FROM pg_describe($query$
  SELECT o.id, c.email
  FROM orders AS o
  LEFT JOIN customers AS c ON c.id = o.customer_id
  WHERE o.placed_at >= $1
$query$);

即使 customers.email 声明为 NOT NULL,result_not_null 仍为 false,因为左连接可能以空值扩展该行。生成可空客户端类型时,这一区别很有用。

执行与安全边界

  • 语句会被解析和分析,但不会执行。描述 DELETE、易变函数调用或高开销查询不会运行该语句。
  • 正常的名称解析和权限检查仍然适用。调用者不能使用 pg_describe 检查其自身无权引用的对象。
  • 参数类型必须能够从上下文推断;有歧义的 $n 参数仍会产生 PostgreSQL 分析错误。
  • 结果描述的是 PostgreSQL 分析后的输出,而不是应用稍后组装的动态 SQL。

要求与注意事项

  • 上游 1.0.0 要求 PostgreSQL 17;PostgreSQL 16 被描述为可能可用但未经测试。Pigsty 软件包面向 PostgreSQL 17 和 18。
  • 扩展可重定位,不需要预加载或重启。
  • 配套的 pg-describe-gen TypeScript 工具是独立的 npm 软件包。PostgreSQL 扩展无需它也能工作。
  • 这是一个较新的 API。请在 CI 中固定扩展/工具版本,并在模式迁移时一并审查生成的变更。

表格阅读器