@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.
One walk covers statements and the function bodies inside them
Know if a node is a write target, a read, or inside a function
Pure traversal; pair it with any parser output
Visitors for every node the Postgres parser produces
walk() takes a parsed script, a ParseResult, a SQL node, a PL/pgSQL body or an array, and dispatches on its shape.
Register callbacks per node tag such as SelectStmt, RangeVar or PLpgSQL_stmt_dynexecute, or pass one function for every node.
Each callback gets the enclosing statement, whether it reads or writes, and the name of the function body it came from.
Pass an array of visitors to run several independent checks in a single pass.
Return false to skip a subtree, or abort the whole walk with a reason when a validator has decided.
traverse() replaces, inserts and removes nodes without breaking the walk.
Mix SQL and PL/pgSQL node tags in one visitor and walk any AST the parser returns.
Use the statement context to find writes, and abort as soon as a rule is broken.