Expand description
PL/pgSQL function bodies: typed AST, parser, and the variable
binding rewriter.
Bodies are parsed with libpg_query’s PL/pgSQL parser
(pg_query::parse_plpgsql), which returns the same JSON dump
PostgreSQL itself produces. This module lowers that JSON into a
typed AST whose embedded SQL fragments are pre-compiled into
Expr / Statement values, ready for execution against the
engine.
Variable references inside embedded SQL are plain column
references after compilation. At execution time the interpreter
rewrites them into literals through VariableResolver /
bind_expr / bind_statement before handing the statement to
the engine. This matches plpgsql.variable_conflict = use_variable resolution: a name that is both a PL/pgSQL
variable and a column of a queried table resolves to the variable
(stock PostgreSQL raises an ambiguity error instead).
Structs§
- PLpgSQL
Block [DECLARE ...] BEGIN ... [EXCEPTION ...] ENDblock.- PLpgSQL
Cursor - PLpgSQL
Cursor Argument - PLpgSQL
Exception Arm - One
WHEN cond [OR cond ...] THEN stmtsarm of an exception section. - PLpgSQL
Function - A parsed
PL/pgSQLfunction body: the flat datum table plus the outermost block. - PLpgSQL
RowField name -> datumslot of a row target.- PLpgSQL
Var - Scalar
PL/pgSQLvariable (declared variable, parameter, loop counter, or an internal compiler temporary).
Enums§
- Into
Target - Assignment /
INTOtarget. - PLpgSQL
Datum - One entry in the function’s flat datum table.
varno/dnoreferences inside statements index into this table. - PLpgSQL
Return Value - Value source for
RETURNandRETURN NEXT.PostgreSQL18 stores a simple datum reference inretvarno, distinct from a general SQL expression. - PLpgSQL
Stmt - Executable
PL/pgSQLstatement. - Raise
Level RAISEseverity.
Traits§
- Variable
Resolver - Resolves routine variables while a compiled expression / statement is being specialized for one execution.
Functions§
- bind_
expr - Rewrite an expression, substituting resolvable variable references with literals. References the resolver declines stay untouched.
- bind_
select - Rewrite a
SELECTbody, substituting resolvable variables. - bind_
statement - Rewrite a full statement, substituting resolvable variables in every expression position. Statements without expression payloads pass through unchanged.
- compile_
expression_ text - Compile a bare expression through
PostgreSQL’s PL/pgSQL expression parser. - condition_
sqlstate - Map a PL/pgSQL condition name to the SQLSTATE used by
RAISE condition_name. - condition_
sqlstates - Return every SQLSTATE matched by a PL/pgSQL exception condition.
- parse_
do_ block - Parse a
DO $$ ... $$body by wrapping it into an anonymous void-returning function. - parse_
function