split

Splits sql on the ; that separate statements, and on no other ;.

A ; does not separate anything when it stands inside '...', inside E'...' where a backslash escapes what follows it, inside "...", inside $tag$...$tag$ - which is where a PL/pgSQL body keeps most of its semicolons - inside a -- comment, inside a /* */ comment nested to any depth, or inside parentheses, where CREATE RULE ... DO (a; b) puts one legitimately.

Return

The statements in the order they appear. A stretch holding nothing but whitespace and comments is not a statement and is not returned, so a trailing ; adds nothing and a script of comments alone comes back empty.

Parameters

sql

The script, one statement or many.

Throws

UNCLOSED_TOKEN where a quote, dollar quote or block comment opens and never closes. The whole point of the walk is telling which ; are separators, and past an unterminated construct none of them can be told apart.