Blog

PostgreSQL scripts: now checked

Shaun Hardneck · · 4 min read

Dark ScriptPreflight cover showing the top lines of a report: result, reach and undo.

ScriptPreflight now checks PostgreSQL scripts. Upload a .pgsql or .psql file, a .sql file written for PostgreSQL, or paste a script and choose PostgreSQL in the language menu, and you get the same report as for any other script: what it changes, how far it reaches, whether it can be undone, and every finding with its rule ID and line.

psql scripts, schema migrations and maintenance jobs are where a missing WHERE clause, a TRUNCATE ... CASCADE or a grant to PUBLIC does its damage. They are often run once, by hand, against production.

How a PostgreSQL script is checked

The script is parsed with libpg_query, the parser from the PostgreSQL server itself, in an isolated process. It is never run, and no database is contacted. The rules then read every statement, including DO blocks and PL/pgSQL function bodies in the script:

  • Data changes: DELETE, UPDATE and MERGE with no WHERE, a WHERE that was commented out or cut off by a stray semicolon, joins that are not on a key, and data-modifying CTEs
  • Removals: DROP and TRUNCATE, including CASCADE, and dropped schemas, databases, roles and partitions
  • Access: roles and role attributes, grants to PUBLIC, default privileges, SECURITY DEFINER functions and row-level security
  • The server: ALTER SYSTEM, database and role settings, replication slots and subscriptions, extensions, server file and program access, and logging turned off
  • psql itself: meta-commands are recorded and never run; \! and COPY ... PROGRAM shell text is checked by the Bash rules

Ratings follow how far a change reaches

A finding is rated High or Critical only when the script itself shows that the change reaches broadly or cannot be undone: a DELETE with no WHERE, a condition that matches every row, a DROP or TRUNCATE of a table that holds data, or a loop that is shown to reach every row or every table of a kind.

Most real changes are bounded by a condition only the data can answer, such as WHERE status = 'closed' or a loop over a filtered query. ScriptPreflight never connects to your database, so it cannot count those rows. Instead of guessing, it rates such a change Medium and says in the finding that how many rows it reaches depends on the data. Findings in the classes whose rating is never lowered this way, such as role attributes, privileges, ALTER SYSTEM, server file access and audit settings, keep their rating whatever the condition.

How we decided it was ready

A new language is switched on only after it passes a sealed evaluation: scripts written separately from the rules, kept closed while the rules were built, and scored once. PostgreSQL took fourteen rounds and nine private validation sets before the final sealed set was scored. Earlier sealed sets were used up along the way and are now regression data. The set that passed held 60 dangerous scripts, 60 benign scripts and 8 scripts that should be refused:

  • 0 dangerous scripts called clean
  • Critical and High recall 100%
  • Critical and High precision 94.4% raw, and 100% with 3 label conflicts excluded, pending independent review
  • a useful result on 98.3% of the benign scripts
  • all 8 out-of-scope scripts refused

The three label conflicts are scripts where the label and the rules' fixed rating disagree: two dangerous scripts about transaction handling and session control that were labelled below High, and one benign script in a privilege class whose High rating is never lowered. One benign script was reported as not assessed rather than given a result, and one dangerous script was rated Low against a Medium label; neither was called clean.

The coverage page lists every category, the parser version and what a report cannot see.

What it cannot see

ScriptPreflight reads what is in the script you submit. It never connects to a server, so it does not know how many rows a table holds, which objects exist, what CASCADE will reach or which privileges a role already has. Functions and procedures that are not defined in the script block a clean result, psql \i includes are not followed, and psql variable values are not known. Function bodies in other languages and dynamic SQL built from values known only at run time are not visible, and the report says so in its coverage.

A clean report means no known issue in what we could check, not a promise that nothing can go wrong. Keep your review, your backups and your change window.

Try it on the next migration you're about to run. The free trial includes three preflights, no card required.