@pgsql/traverse

Walk Any Postgres AST

Babel-style visitors for Postgres ASTs: SQL statements and PL/pgSQL function bodies in one walk. Every callback knows its statement, whether it reads or writes, and which function it came from.

SQL and PL/pgSQL

One walk covers statements and the function bodies inside them

Statement Context

Know if a node is a write target, a read, or inside a function

No WASM

Pure traversal; pair it with any parser output

@pgsql/traverse Features

Visitors for every node the Postgres parser produces

One Entry Point

walk() takes a parsed script, a ParseResult, a SQL node, a PL/pgSQL body or an array, and dispatches on its shape.

Visitors by Node Tag

Register callbacks per node tag such as SelectStmt, RangeVar or PLpgSQL_stmt_dynexecute, or pass one function for every node.

Statement Context

Each callback gets the enclosing statement, whether it reads or writes, and the name of the function body it came from.

Composable Policies

Pass an array of visitors to run several independent checks in a single pass.

Skip or Abort

Return false to skip a subtree, or abort the whole walk with a reason when a validator has decided.

Safe Mutation

traverse() replaces, inserts and removes nodes without breaking the walk.

Visit by Node Type

Mix SQL and PL/pgSQL node tags in one visitor and walk any AST the parser returns.

Validate with Context

Use the statement context to find writes, and abort as soon as a rule is broken.