# pg_agent_gate [![verify](https://github.com/Manuelreyesbravo/pg_agent_gate/actions/workflows/verify.yml/badge.svg)](https://github.com/Manuelreyesbravo/pg_agent_gate/actions/workflows/verify.yml) [![License: Apache 2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/LICENSE) ![PostgreSQL 18 and 19](https://img.shields.io/badge/PostgreSQL-18%20%7C%2019-336791.svg) **Agents propose, PostgreSQL decides.** An AI agent with a database login can run anything its role allows. When the model writes a `DELETE` without a `WHERE`, a `DROP TABLE`, or a read of another customer's rows, PostgreSQL runs it: it cannot tell the agent from your application. pg_agent_gate is an extension that makes the database tell them apart. In a session that belongs to a registered agent role, SQL does not execute. The agent may only propose one statement. PostgreSQL parses and plans it, checks it against the agent's limits (rows per statement and per time window, DDL or not, assertions that must keep holding), can show the exact before and after, and keeps it only if every check still passes. Every proposal and every refusal is recorded, append-only. It is enforced by hooks inside the server, so no client, driver or protocol gets around it, as long as the library is loaded before the agent's first statement and the agent is not a superuser. ## Try it from psql, with no model PostgreSQL 18 or 19, with `shared_preload_libraries = 'pg_agent_gate'` ([install](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/install.md)). As a superuser: ```sql CREATE EXTENSION pg_agent_gate; CREATE TABLE customers (id int PRIMARY KEY, name text NOT NULL, plan text NOT NULL); INSERT INTO customers VALUES (1, 'Ana', 'free'), (2, 'Bo', 'free'), (3, 'Cy', 'free'); CREATE ROLE billing_agent LOGIN; -- never a superuser GRANT USAGE ON SCHEMA public TO billing_agent; GRANT SELECT, UPDATE ON customers TO billing_agent; SELECT agent_gate.register_agent('billing', 'billing_agent', 'answers billing questions and upgrades plans', p_max_rows => 2, p_allow_ddl => false); ``` Then connect as `billing_agent` and be the agent: ```sql DELETE FROM customers; -- ERROR: ... it proposes, it does not execute SELECT agent_gate.propose('update customers set plan = ''pro'' where id = $1::int', 'upgrade the customer who asked', ARRAY['1']); SELECT agent_gate.dry_run(1); -- the row before and after; nothing is kept SELECT agent_gate.commit(1); -- verified again, then kept SELECT agent_gate.propose_and_commit('update customers set plan = ''free''', 'reset every plan'); -- 3 rows, limit 2: undone SELECT agent_gate.propose('drop table customers', 'clean up'); -- refused: no DDL ``` `1` is the proposal number `propose` returned. `register_agent` takes effect on the role's next connection. `make demo` shows the same role with and without the gate ([demo](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/demo.md)). ## With a model [ygg-agent](https://github.com/Manuelreyesbravo/ygg-agent) is the loop on the model's side: the model writes one SQL statement, it reaches `agent_gate.propose()` over PostgreSQL's own protocol, and the verdict goes back. There is no MCP server in between, and the loop has no power of its own. Measured with pg_agent_gate 0.4.0, on a CPU-only CI runner anyone can re-run, with the same model, the same tasks and a superuser checking what really changed: through a popular Postgres MCP server, 4 of 7 trap tasks broke out. With the database deciding, 0 of 7 ([how it was measured](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/mcp.md)). ## Limits - **Load it first.** Without the library loaded before the agent's first statement (`shared_preload_libraries`, or the `session_preload_libraries` that `register_agent` sets on the role), the gate fails open. `agent_gate.whoami()` says whether it is enforced. - **A superuser is not behind it**, nor is anyone who can edit `postgresql.conf` or restart the server. `register_agent` refuses superusers and roles that can leave the gate. - **Grants still decide what the agent may touch.** The gate adds checks; it does not take a privilege away. A role that only reads more, such as `pg_read_server_files`, can still be registered, and what it grants still works inside a proposed read. - **Intent is not checked.** A valid statement within the agent's privileges and limits runs. Keep `max_rows` small, bind assertions that must keep holding, and look at `dry_run` where it matters. The full list: [threat model](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/threat-model.md) and [what it does not cover](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/limits.md). ## More [Install](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/install.md) · [How it verifies](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/verification.md) · [The seven verbs](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/verbs.md) · [Execution and the record](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/execution-and-record.md) · [Testing](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/testing.md) · [Cost](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/cost.md) · [Upgrading](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/upgrading.md) · [MCP and ygg-agent](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/mcp.md) · [Measured](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/measurements.md) · [Notes for extension authors](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/docs/notes-for-extension-authors.md) ## License Apache License 2.0 -- see [LICENSE](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/LICENSE). Copyright 2026 Manuel Reyes Bravo. The name is not licensed with the code: see [TRADEMARK.md](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/TRADEMARK.md). Security reports: [SECURITY.md](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/SECURITY.md). Contributions: [CONTRIBUTING.md](https://github.com/Manuelreyesbravo/pg_agent_gate/blob/main/CONTRIBUTING.md).