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_statement_variables / bind_expression_variables before
handing the statement to the engine, after the binder has checked
each reference against the columns and relations the statement can
see, so that a name that is both a variable and a column resolves
as plpgsql.variable_conflict directs.
Modules§
- runtime_
diagnostics - SQLSTATE matching, row diagnostics, and
RAISEformatting.
Structs§
- Compile
Options - The options a body declares before its first block, which take precedence over the session’s settings.
- PLpgSQL
Block [DECLARE ...] BEGIN ... [EXCEPTION ...] ENDblock.- PLpgSQL
Compilation Identity - Only a clone of the same compilation shares first-use sites. A new parse, including replacement or a specialization, receives a fresh identity.
- PLpgSQL
Cursor - PLpgSQL
Cursor Argument - PLpgSQL
Exception Arm - One
WHEN cond [OR cond ...] THEN stmtsarm of an exception section. - PLpgSQL
Fragment - The validation tree is absent when
PostgreSQLcompiles a runtime structure without checking SQL that execution might never reach. - PLpgSQL
Function - A parsed
PL/pgSQLfunction body: the flat datum table plus the outermost block. - PLpgSQL
RowField name -> datumslot of a row target.- PLpgSQL
Source - One syntactic occurrence, shared by clones but never interned by its text. This carries no execution state; the execution owner supplies the cache.
- PLpgSQL
Var - Scalar
PL/pgSQLvariable (declared variable, parameter, loop counter, or an internal compiler temporary). - PLpgSQL
Variable Bindings - Plpgsql
Catalog - An immutable catalog snapshot for one parse.
search_pathis the caller’s effective namespace order, including pg_catalog where appropriate. Built-in types remain available through PostgreSQL’s own fallback catalog. - Plpgsql
Type - The pg_type attributes needed to distinguish scalar, domain, array, and composite declarations.
- Raise
Diagnostic - Raise
Option - Resolved
Variable - Runtime datum value together with the concrete SQL type declared by PL/pgSQL. The type is optional for composite fields and pseudo-types whose runtime carrier already identifies their category.
Enums§
- Into
Target - Assignment /
INTOtarget. - PLpgSQL
Compile Mode - PLpgSQL
Cursor Count - Constant or run-time expression controlling cursor movement.
- PLpgSQL
Cursor Open - Query source selected by one
OPENstatement. - PLpgSQL
Datum - One entry in the function’s flat datum table.
varno/dnoreferences inside statements index into this table. - PLpgSQL
Parse Mode PostgreSQL’s raw grammar selected for an embedded PL/pgSQL expression.- 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. - PLpgSQL
Variable Reference - Raise
Level RAISEseverity.- Raise
Option Kind - Variable
Conflict - How a name in an embedded statement that is both a
PL/pgSQLvariable and a column or relation the statement can see resolves, asplpgsql.variable_conflictand#variable_conflictchoose.
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_
expression_ variables - Bind the variables of an embedded expression, which
PostgreSQLanalyzes asSELECT expression: only a subquery lets a name of the expression meet a column, so an expression without one binds every variable it names. - 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.
- bind_
statement_ variables - Bind the variables of an embedded statement, resolving each name that a variable takes against the columns and relations the statement can see as
conflictdirects. - compile_
expression_ text - Compile a bare expression through
PostgreSQL’s PL/pgSQL expression parser. - compile_
options - Read the options a body declares before its first block, as
pl_gram.y’scomp_optionsdoes: each#starts an option, between whitespace and comments. The parser has accepted the body, so an option this reader meets is well formed. - 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.
- parameter_
type_ mismatch - parameterize_
statement_ variables - Reuse the ordinary PL name/column conflict resolver, then number only names it selected as variables. Repeated reads receive fresh invocation values.
- parse_
do_ block - Parse a
DO $$ ... $$body throughPostgreSQL’s native inline-code path. - parse_
do_ block_ with_ catalog - Parse an anonymous block using the engine’s catalog type snapshot.
- parse_
function - parse_
function_ with_ catalog - Parse a stored routine using the engine’s catalog type snapshot.
- parse_
function_ with_ catalog_ mode
Type Aliases§
- PLpgSQL
Cursor Arguments - PLpgSQL
Expression - PLpgSQL
Statement - Variable
Site Resolver - Resolve how the variable sites of a statement resolve in it: the statement with each site as the positional parameter numbered after it, the sites’ values as typed parameters, and the sites’ names as written.