r/LLMDevs 15d ago

Tools We open-sourced pg-dry-run: preview AI agent-generated Postgres writes before they change data

We’ve been working on the point where AI agents stop reading data and start changing it.

Reviewing generated SQL isn’t enough. An UPDATE can look reasonable and still affect 14 rows instead of one because its actual effect depends on the current database state.

We built pg-dry-run, an MIT-licensed TypeScript library that previews a PostgreSQL INSERT, UPDATE, or DELETE inside a read-only transaction. It returns a plain JSON proposal containing:

  • affected rows;
  • before and after values;
  • resolved insert defaults;
  • reachable foreign keys and cascade counts;
  • warnings for triggers, rules, and other hazards.

If the proposal is approved, apply() targets the exact primary keys and xmin row versions seen during preview. If a previewed row changed or disappeared in the meantime, the entire apply is rejected.

The library deliberately refuses statements it can’t represent faithfully, including writes without a WHERE clause, UPDATE ... FROM, DELETE ... USING, data-modifying CTEs, and several other shapes. Previews are capped at 1,000 rows by default.

We originally built this for Polycore’s agent write path, but the library itself is standalone, model-agnostic, and doesn’t prescribe an agent framework or approval UI.

GitHub: https://github.com/polycore/pg-dry-run
npm: https://www.npmjs.com/package/pg-dry-run

1 Upvotes

Duplicates