pg_agent_gate

verify License: Apache 2.0 PostgreSQL 18 and 19

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). As a superuser:

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:

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).

With a model

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).

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 and what it does not cover.

More

Install · How it verifies · The seven verbs · Execution and the record · Testing · Cost · Upgrading · MCP and ygg-agent · Measured · Notes for extension authors

License

Apache License 2.0 – see LICENSE. Copyright 2026 Manuel Reyes Bravo.

The name is not licensed with the code: see TRADEMARK.md. Security reports: SECURITY.md. Contributions: CONTRIBUTING.md.