## Execution * Runs with the **agent's own privileges**. The gate adds verification; `GRANT` is still the authorization. * Inside a subtransaction, decided **after** it runs: * a write touching more rows than the agent's `max_rows` is undone -- counted over everything the statement changed, triggers and functions included; a DDL's count is what it changed in tables that existed before it (0.4.3); * deferred constraints are fired **inside the gate** (`SET CONSTRAINTS ALL IMMEDIATE`), not at the caller's commit where nobody is watching; * assertions bound to the agent (from [pg_living_assertions](https://pgxn.org/dist/pg_living_assertions/)) are run after the change: `broken` or `erroring` undoes it -- a check that cannot run is not a check that passed. * A read's subtransaction is **always** rolled back: a read has nothing to keep. ## The record `agent_gate_internal.proposals` and `.executions` are append-only (triggers refuse `UPDATE` and `DELETE`) and survive `pg_dump`. Each execution says its outcome -- `kept`, `read`, `rolled_back`, `aborted` or `refused` -- and the last two must say why (a `CHECK`). Writes keep the before/after of up to 50 rows. **Reads keep nothing but counts**: copying what an agent read into an audit table would copy the data itself. Only the gate writes the record. The writing functions are `SECURITY DEFINER` and first ask the gate whether the caller is the gate's own code -- a flag no SQL can set, kept down while the agent's own SQL runs. A proposal cannot forge its history; an agent calling those functions by name is refused by the hook. **What a rollback takes, the server log keeps.** The record is written in the caller's transaction, and an agent session may control its transaction (a driver such as psycopg opens `BEGIN` by itself). So `ROLLBACK`, `ROLLBACK TO SAVEPOINT`, or a session that disconnects without `COMMIT` takes those rows with it -- a refused attempt and a read included. The gate cannot keep a row its caller rolls back, and refusing transaction blocks would break every driver that opens one. Instead, when an abort takes rows from the record, each one is written to the server log at `LOG` -- outside every transaction, never sent to the agent's session -- on one line, with the agent's text JSON-escaped: ``` LOG: pg_agent_gate: rolled back with the transaction, so not in the record: proposal 1 of agent rollback (role ...): kind=ddl ok=false intent="probe one" sql="drop table shop.orders" ``` `acts()` and the tables show what was committed; the server log shows what an abort took (since 0.2.10, from an external audit of 0.2.8; `tests/rollback.sh`). A server crash before the log line is written loses it, as it would lose any. ### How durable the record is `agent_gate.attempt_durability` (superuser-only): * **A change the gate keeps is always committed with the durability the server is configured with.** That flush also carries the record of the proposal that led to it, which sits earlier in the WAL: no change exists on disk without its proposal and its execution next to it. * **`fast` (default):** the record of an *attempt* -- a proposal, a dry run, a read, a refusal, anything that changed no data -- rides on an asynchronous commit. A server crash inside the WAL writer's window can lose the record of an attempt that changed nothing. * **`durable`:** every record pays its own flush -- so `propose` + `commit` pay two, and `propose_and_commit` pays **one**: the proposal and its execution ride on the same commit. Counted, not inferred: `tests/flushes.sh` reads the WAL fsyncs of client backends from `pg_stat_io` around 50 acts of every kind, and gets exactly 2.00 and 1.00 per act (0.00 for a fast read, 1.00 for any kept change). Never relaxed in a transaction that had already written something before the first verb ran: those writes are not the gate's to relax.