A PostgreSQL DO block ran statements nobody reviewed

VERICTO-081 Critical In CI (offline CLI): BLOCKED

DO $$ BEGIN DELETE FROM users; END $$; runs a procedural body once, and that body can contain anything: here, a DELETE without WHERE. From the outside it is a single opaque string, so reviews and tools that read the statement do not see what it does. If the block changed data you did not intend to change, recover it like any other mass change.

How to recover the data

  • If the DO block ran inside a transaction that is still open, run ROLLBACK.
  • Read the block's body to see exactly which tables it touched; the DO statement itself returns no row count.
  • Stop the writers on those tables, restore a point-in-time copy from just before the block into a separate instance, and copy the affected rows back by primary key.

Why it happens

DO is handy for one-off fixes that need a loop or a condition, so it appears in hotfix scripts and migrations. The same convenience means a destructive statement can sit inside dynamic SQL (EXECUTE format(...)) or a loop, where nothing outside the block can see it.

How to fix it

Replace the block with the explicit statements it runs, so each one can be read, reviewed and checked:

DELETE FROM users WHERE id = 42;

When you really need procedural logic, put it in a named function created by a reviewed migration, and call it with arguments that limit what it touches.

How to stop it from happening again

VERICTO-081 · DO anonymous code block · Severity: Critical · AST node: DoStmt

The statement the rule reports

-- fix.sql
DO $$ BEGIN DELETE FROM users; END $$;

Real CLI output

$ npx @vericto/vericto-cli@2.3.0 check --offline fix.sql
note: offline mode: 42 standard rules at default severities, engine 5.1.0. Custom rules, overrides, sensitive columns, allowlists and the audit trail apply online only.
✖ fix.sql  BLOCKED  VERICTO-081 critical
    DoStmt
    fix: Replace the anonymous DO block with explicit, reviewable statements
1 blocked · 0 allowed · 0 flagged · 0 monitored   (ruleset standard@engine-5.1.0 · mode: offline (engine 5.1.0, 42 standard rules, no workspace policy))

✖ Vericto blocked 1 statement  (ruleset standard@engine-5.1.0 · mode: offline (engine 5.1.0, 42 standard rules, no workspace policy))
  ✖ fix.sql  BLOCKED  VERICTO-081 critical
      DoStmt
      fix: Replace the anonymous DO block with explicit, reviewable statements

This is Vericto enforcing a rule — the exit code is intentional, not a crash.
To allow a finding on purpose, add an inline comment on the statement:
    -- vericto:ignore[VERICTO-081] <reason>   (a reason is required)

The CLI blocks it and exits with code 1, so the CI job fails before the merge.

Try it free, no account

The CLI checks your .sql files on your machine with the 42 standard rules: no sign-up, no API key, and no SQL leaves your computer. It is open source (AGPL-3.0-only).

npx @vericto/vericto-cli@2.3.0 check --offline migrations/

On every pull request

The vericto/check-sql GitHub Action (Apache-2.0) runs the same offline check on the .sql files a PR changes and annotates the findings on the PR. Save it as .github/workflows/vericto.yml:

on: pull_request
jobs:
  vericto:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with: { fetch-depth: 0 }
      - uses: vericto/check-sql@v1
        with: { offline: true, upload-sarif: false }

At runtime, for AI agents

The SQL an AI agent writes never goes through a PR. The Vericto TCP proxy checks each statement between the application or agent and the database, and the MCP check_sql tool lets the agent check SQL before it runs it. Both use the runtime profile, which also blocks schema changes.

The AST condition in the engine

The rule is a deterministic predicate over the AST, in src/rules/evaluator.rs of the open source engine (AGPL-3.0-only), at this page's commit:

"VERICTO-081" => find(stmts, |s| s.kind == StatementKind::DoBlock)
    .map(|s| {
        let mut v = violation(
            rule,
            s,
            Some("Replace the anonymous DO block with explicit, reviewable statements".to_string()),
        );
        v.ast_node_path = "DoStmt".to_string();
        v
    }),

Frequently asked questions