Contents
Setup
PostgreSQL 18 or later (dry_run uses RETURNING old/new). Built with
pgrx 0.19.2; the test suite
passes on 18 and 19 (tested on 18.7 and 19.0). Cargo.lock is committed, so a build
from a tag uses the crates its suites ran against.
cargo install cargo-pgrx --version 0.19.2 --locked
cargo pgrx init --pg18 /path/to/pg_config # --pg19 for 19; registers it, downloads nothing
cargo pgrx install --release --pg-config /path/to/pg_config
# postgresql.conf -- recommended
shared_preload_libraries = 'pg_agent_gate'
CREATE EXTENSION pg_agent_gate;
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 => 50, p_allow_ddl => false);
-- optional: every write this agent keeps must leave this assertion holding
SELECT agent_gate.bind_assertion('billing', 'no_customer_without_plan');
register_agent sets agent_gate.agent on the role. If the library is not in
shared_preload_libraries, it also sets session_preload_libraries on that
role, so the gate is loaded before the agent’s first statement. That setting
replaces any session_preload_libraries the role already had: add other
libraries back after registering (ALTER ROLE ... SET session_preload_libraries
= 'pg_agent_gate, other'). It refuses superuser roles: a superuser can unset a
superuser-only setting, and an agent that can leave the gate is not behind it.
For the same reason it refuses REPLICATION and BYPASSRLS roles, and members
(direct or inherited) of a superuser, of such a role, or of pg_write_all_data,
pg_execute_server_program or pg_write_server_files. A session loaded this
way ends (FATAL) if its client set any agent_gate.* parameter at startup. It
takes effect on the role’s next connection.