Skip to content
mentoor

Topic 4 of 5 · 5 min read

Stopping destructive agent database commands

Goal: Set an approval rule and a recovery check that would stop an agent from wiping a production table.

Before an agent gets write access, put a human approval step in front of destructive statements and prove you can get the data back. Both parts are needed: approval reduces the chance of a bad command, and recovery limits the damage when one gets through.

Check yourself · 1 of 3

Which statement should always need a person's approval from an agent?

Choose one answer

Why it matters

An agent that is told to "clean up test data" can read that as DELETE FROM orders with no WHERE, or TRUNCATE, or DROP TABLE. These are valid commands that run without a warning. It can also run them against the wrong database because the connection string in its environment was production. The agent may not know it did anything wrong.

How to set it up

  1. Classify statements. Read-only (SELECT, EXPLAIN) can run freely. Writes (INSERT, UPDATE) need a scoped role. Destructive ones (DROP, TRUNCATE, DELETE or UPDATE without WHERE, ALTER ... DROP COLUMN) always need a person.
  2. Enforce it outside the model. Put the check in the code or proxy that executes the SQL, or in the agent harness's permission rules. A line in a prompt can be skipped; a gate cannot.
  3. Show the person the effect. Run the statement in a transaction and report the row count, or show EXPLAIN, then ask for approval before COMMIT.
  4. Withhold the privileges. Do not grant DROP or ownership to the agent role. Without them, the statement fails whatever the approval says.
  5. Check recovery first. Before granting write access, confirm a backup exists, restore it somewhere, and know whether point-in-time recovery is on. PostgreSQL gets point-in-time recovery from a base backup plus archived WAL, which lets you restore to a chosen moment.

Before and after

// Before: the agent's SQL runs as written
await pool.query(agentSql);

// After: destructive statements wait for a person
const destructive = /\b(drop|truncate|alter\s+table\s+\S+\s+drop)\b/i.test(agentSql)
  || /^\s*(delete|update)\b(?!.*\bwhere\b)/is.test(agentSql);
if (destructive) {
  await requireHumanApproval({ sql: agentSql, role: "agent_ops" });
}
await pool.query(agentSql);

A regex is a floor, not a wall: it can miss comments and nested statements. It works best next to missing grants.

What goes wrong

  • Backups never restored. A backup file you have never loaded is a guess. Restore one on a schedule and time it.
  • Approval fatigue. If a person approves forty prompts an hour, they stop reading. Keep the gate for destructive statements only.
  • Recovery measured in days. Daily backups lose up to a day of writes. Point-in-time recovery narrows that, if it is enabled before the incident.
  • Same credentials in every environment. One DATABASE_URL copied around turns a dev experiment into a production wipe.

Examples

The unscoped delete

The task says "remove test accounts". The agent writes DELETE FROM accounts; because it assumed the table held only test data. A gate that blocks DELETE with no WHERE stops it. A better gate runs it in a transaction, reports "1,204,331 rows would be deleted", and a person sees the number is wrong.

A recovery check before write access

Before enabling writes: restore last night's backup into a scratch instance, check that a known recent row exists, and write down how long it took. Confirm WAL archiving is on so recovery to a chosen moment is possible. If any step fails, the agent stays read-only.

Key points

  • Classify statements and require a person to approve DROP, TRUNCATE and DELETE or UPDATE without WHERE.
  • Enforce the gate in the code or proxy that runs the SQL, not in the prompt.
  • Withholding privileges such as DROP and table ownership makes the statement fail whatever is approved.
  • Show the effect before approval, for example the row count from a transaction that has not committed.
  • Restore a backup and confirm point-in-time recovery works before granting any write access.

Common mistakes

  • Relying on a prompt line such as "never delete without a WHERE clause" as the only protection.
  • Granting the agent role ownership of tables, which includes the right to drop them.
  • Assuming backups work because the backup job reports success.
  • Requiring approval for every statement, so people click through without reading.

Check yourself · 2 of 3

Where should the rule 'destructive SQL needs approval' be enforced?

Choose one answer

Check yourself · 3 of 3

A team wants to grant an agent write access to production. Daily backups run and the job shows green. What should they do first?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic