跳转到主要内容

pg_agent_gate

为 Agent 数据库角色提供 SQL 提议、校验、受控执行与人工审批。

pg_agent_gate · 为 Agent 数据库角色提供 SQL 提议、校验、受控执行与人工审批。

概览

ID扩展软件包版本类别许可证编程语言
7470pg_agent_gatepg_agent_gate0.4.4SECApache-2.0Rust
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--sLd--否是是是否否
关联
模式agent_gate agent_gate_internal
相关扩展pg_living_assertions pg_policy
Preload before agent statements. Requires READ COMMITTED. Trusted code and external effects remain outside the sandbox.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.4.419 18 17 16 15pg_agent_gate-
RPMPIGSTY0.4.419 18 17 16 15pg_agent_gate_$v-
DEBPIGSTY0.4.419 18 17 16 15postgresql-$v-pg-agent-gate-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.4.1N/AN/AN/A
el8.aarch64N/APIGSTY 0.4.1N/AN/AN/A
el9.x86_64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
el9.aarch64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
el10.x86_64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
el10.aarch64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
d12.x86_64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
d12.aarch64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
d13.x86_64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
d13.aarch64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
u22.x86_64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
u22.aarch64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
u24.x86_64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
u24.aarch64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
u26.x86_64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
u26.aarch64PIGSTY 0.4.4PIGSTY 0.4.4N/AN/AN/A
软件包版本平台仓库大小下载
pg_agent_gate_190.4.4el9.x86_64pigsty500.4 KiBpg_agent_gate_19-0.4.4-1PGSTY.el9.x86_64.rpm
pg_agent_gate_190.4.4el9.aarch64pigsty479.6 KiBpg_agent_gate_19-0.4.4-1PGSTY.el9.aarch64.rpm
pg_agent_gate_190.4.4el10.x86_64pigsty500.9 KiBpg_agent_gate_19-0.4.4-1PGSTY.el10.x86_64.rpm
pg_agent_gate_190.4.4el10.aarch64pigsty480.4 KiBpg_agent_gate_19-0.4.4-1PGSTY.el10.aarch64.rpm
postgresql-19-pg-agent-gate0.4.4d12.x86_64pigsty412.2 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~bookworm_amd64.deb
postgresql-19-pg-agent-gate0.4.4d12.aarch64pigsty365.5 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~bookworm_arm64.deb
postgresql-19-pg-agent-gate0.4.4d13.x86_64pigsty412.3 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~trixie_amd64.deb
postgresql-19-pg-agent-gate0.4.4d13.aarch64pigsty367.3 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~trixie_arm64.deb
postgresql-19-pg-agent-gate0.4.4u22.x86_64pigsty420.8 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~jammy_amd64.deb
postgresql-19-pg-agent-gate0.4.4u22.aarch64pigsty396.8 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~jammy_arm64.deb
postgresql-19-pg-agent-gate0.4.4u24.x86_64pigsty466.6 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~noble_amd64.deb
postgresql-19-pg-agent-gate0.4.4u24.aarch64pigsty391.9 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~noble_arm64.deb
postgresql-19-pg-agent-gate0.4.4u26.x86_64pigsty414.7 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~resolute_amd64.deb
postgresql-19-pg-agent-gate0.4.4u26.aarch64pigsty391.0 KiBpostgresql-19-pg-agent-gate_0.4.4-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_agent_gate_180.4.1el8.x86_64pigsty538.8 KiBpg_agent_gate_18-0.4.1-1PGSTY.el8.x86_64.rpm
pg_agent_gate_180.4.1el8.aarch64pigsty470.2 KiBpg_agent_gate_18-0.4.1-1PGSTY.el8.aarch64.rpm
pg_agent_gate_180.4.4el9.x86_64pigsty496.9 KiBpg_agent_gate_18-0.4.4-1PGSTY.el9.x86_64.rpm
pg_agent_gate_180.4.4el9.aarch64pigsty476.2 KiBpg_agent_gate_18-0.4.4-1PGSTY.el9.aarch64.rpm
pg_agent_gate_180.4.4el10.x86_64pigsty497.4 KiBpg_agent_gate_18-0.4.4-1PGSTY.el10.x86_64.rpm
pg_agent_gate_180.4.4el10.aarch64pigsty476.9 KiBpg_agent_gate_18-0.4.4-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-agent-gate0.4.4d12.x86_64pigsty436.8 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-agent-gate0.4.4d12.aarch64pigsty391.5 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-agent-gate0.4.4d13.x86_64pigsty436.5 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~trixie_amd64.deb
postgresql-18-pg-agent-gate0.4.4d13.aarch64pigsty392.3 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~trixie_arm64.deb
postgresql-18-pg-agent-gate0.4.4u22.x86_64pigsty469.0 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~jammy_amd64.deb
postgresql-18-pg-agent-gate0.4.4u22.aarch64pigsty446.3 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~jammy_arm64.deb
postgresql-18-pg-agent-gate0.4.4u24.x86_64pigsty463.3 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~noble_amd64.deb
postgresql-18-pg-agent-gate0.4.4u24.aarch64pigsty440.1 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~noble_arm64.deb
postgresql-18-pg-agent-gate0.4.4u26.x86_64pigsty462.7 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~resolute_amd64.deb
postgresql-18-pg-agent-gate0.4.4u26.aarch64pigsty438.7 KiBpostgresql-18-pg-agent-gate_0.4.4-1PGSTY~resolute_arm64.deb

源码

pig build pkg pg_agent_gate;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install pg_agent_gate;		# install by package name

pig install pg_agent_gate -v 19;   # install for PG 19
pig install pg_agent_gate -v 18;   # install for PG 18

将扩展加入 shared_preload_libraries,详见配置说明。

shared_preload_libraries = 'pg_agent_gate';

在数据库中创建扩展。

CREATE EXTENSION pg_agent_gate;

用法

来源:

pg_agent_gate 0.4.4 在 PostgreSQL 18/19 上通过提议、检查、试运行和受控执行处理已注册 Agent 角色,并可要求提交前经过人工审批。必须在第一条 Agent 语句之前加载动态库,可采用共享预加载,或配置角色会话预加载后重新连接。

核心用法

CREATE EXTENSION pg_agent_gate;
SELECT agent_gate.whoami();
SELECT agent_gate.discover('customers');
SELECT agent_gate.propose('SELECT 1', 'Check connectivity');

运行边界

由超级用户安装,为普通角色登记有限的表权限、行数限制与 DDL 允许列表。API 包括 agent_gate.whoami、agent_gate.discover、agent_gate.propose、agent_gate.dry_run、agent_gate.commit 和 agent_gate.acts。这些操作要求 READ COMMITTED,试运行与提交前会再次核对提议时的会话设置,调用已知状态修改内置函数的读取请求会被拒绝。重新登记同名角色不会继承旧提议。agent_gate.max_proposal_bytes 与 agent_gate.max_intent_bytes 限制输入大小。已有重复提交的提议可能阻止升级,需要检查处理。可选绑定检查使用 pg_living_assertions。回滚无法撤销所有序列或外部副作用,已有特权代码仍须可信。服务器日志可能包含 SQL 字面量;尝试记录受外层事务影响,默认快速持久化模式可能在崩溃时丢失近期记录。

人工审批

调用 agent_gate.register_agent 时设置 p_requires_approval,即可要求该 Agent 的提议经过审批。agent_gate_approver 的成员通过 agent_gate.approve 记录同意后,仍由 Agent 提交自己的提议。审批本身不执行 SQL,也不绕过行数限制、断言或提交时检查。Agent 不能自行审批,propose_and_commit 也不能绕过审批要求。

固定模式 agent_gate_internal 保存提议、执行和审批记录,Agent 不直接访问这些表。执行记录的 approved_by 字段保存审批者身份。

写入预算与角色成员

0.4.0 引入默认累计写入预算:每小时允许修改该 Agent 单条语句行数上限所规定的行数。agent_gate.set_budget 调整额度和窗口;agent_gate.reset_budget 重新开始计算额度,不改写记录。已提交写入按实际行影响计数,包括级联与触发器;并发提交会串行核对预算。

Agent 角色不能有成员。登记函数会拒绝这样的角色;升级时会对已有成员关系发出警告,管理员必须撤销这些关系。每个 Agent 应使用自己的登录角色连接,连接代理不能通过角色切换进入 Agent 身份。

0.4.3 的函数与会话检查

0.4.3 动态库在隐藏函数调用实际执行时进行检查,包括类型支持函数、聚合、默认值和约束。它拒绝传给已知执行函数的 SQL 文本、Agent 可修改的函数、受保护上下文变化以及未计入控制范围的状态变化。DDL 引起的行变化也计入行数限制。这些检查会有意拒绝普通 PostgreSQL 角色原本能够执行的部分语句。

受保护上下文包括角色身份、行级安全设置、搜索路径和应用占位参数。上下文变化会导致拒绝,而不是自动恢复原值。Agent 所有对象附带的原生代码受到限制。可选的 pg_living_assertions 0.5.5 可能泄漏上下文并导致拒绝,0.5.6 已修复该问题。不要将 Agent 可控制的模式放入其他用户的搜索路径,因为 DDL 可以创建之后以其他用户权限运行的代码。

动态库与 SQL 升级

替换动态库后,重启或重新连接所有预加载它的进程,再逐库升级:

ALTER EXTENSION pg_agent_gate UPDATE TO '0.4.4';

0.4.1 至 0.4.3 迁移不改变模式对象。更早版本的升级路径仍会添加预算和审批对象;升级后应检查已有 Agent 的预算。0.4.2 未单独发布。超级用户、操作系统和可信 DBA 代码不在这个保护边界内;获准执行的语句仍可能违背人的意图。

0.4.4 版本

0.4.4 更新记录的版本号,不改变 0.4.3 的库行为或 SQL 对象。发行审核明确覆盖 PostgreSQL 18.7 与 19.0。防止后续角色成员关系变更的保护依赖 shared_preload_libraries,仅在角色级会话预加载无法在其他会话中提供这一保证。

表格阅读器