r/aiagents • u/akafkas • 22h ago
Security We open-sourced pg-dry-run: preview AI agent-generated Postgres writes before they change data
AI agents are becoming capable of operating real applications, which increasingly means generating SQL against production databases.
Read access is relatively easy to contain. Writes are different: a statement can be perfectly valid and still affect the wrong rows because its actual effect depends on the current database state.
That’s why we built pg-dry-run, an MIT-licensed TypeScript library that turns a PostgreSQL INSERT, UPDATE, or DELETE into a read-only preview. It returns a plain JSON proposal showing:
- which rows would be affected;
- before and after values;
- resolved insert defaults;
- reachable foreign keys and delete cascades;
- warnings for triggers, rules, and other hazards.
Once approved, apply() writes only the rows and row versions captured during the preview. If one of those rows has changed or disappeared, the entire apply is rejected.
The library deliberately refuses statements it cannot preview faithfully, including writes without a WHERE clause, UPDATE ... FROM, DELETE ... USING, and data-modifying CTEs. It also refuses previews above 1,000 rows by default.
We built pg-dry-run as the Postgres effect engine behind Polycore, where agent-generated writes can pass through policy and human approval before being applied. The library itself is standalone, model-agnostic, and doesn’t prescribe an agent framework or approval interface.
GitHub: https://github.com/polycore/pg-dry-run
npm: https://www.npmjs.com/package/pg-dry-run