Skip to main content

Module plpgsql

Module plpgsql 

Source
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 RAISE formatting.

Structs§

CompileOptions
The options a body declares before its first block, which take precedence over the session’s settings.
PLpgSQLBlock
[DECLARE ...] BEGIN ... [EXCEPTION ...] END block.
PLpgSQLCompilationIdentity
Only a clone of the same compilation shares first-use sites. A new parse, including replacement or a specialization, receives a fresh identity.
PLpgSQLCursor
PLpgSQLCursorArgument
PLpgSQLExceptionArm
One WHEN cond [OR cond ...] THEN stmts arm of an exception section.
PLpgSQLFragment
The validation tree is absent when PostgreSQL compiles a runtime structure without checking SQL that execution might never reach.
PLpgSQLFunction
A parsed PL/pgSQL function body: the flat datum table plus the outermost block.
PLpgSQLRowField
name -> datum slot of a row target.
PLpgSQLSource
One syntactic occurrence, shared by clones but never interned by its text. This carries no execution state; the execution owner supplies the cache.
PLpgSQLVar
Scalar PL/pgSQL variable (declared variable, parameter, loop counter, or an internal compiler temporary).
PLpgSQLVariableBindings
PlpgsqlCatalog
An immutable catalog snapshot for one parse. search_path is the caller’s effective namespace order, including pg_catalog where appropriate. Built-in types remain available through PostgreSQL’s own fallback catalog.
PlpgsqlType
The pg_type attributes needed to distinguish scalar, domain, array, and composite declarations.
RaiseDiagnostic
RaiseOption
ResolvedVariable
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§

IntoTarget
Assignment / INTO target.
PLpgSQLCompileMode
PLpgSQLCursorCount
Constant or run-time expression controlling cursor movement.
PLpgSQLCursorOpen
Query source selected by one OPEN statement.
PLpgSQLDatum
One entry in the function’s flat datum table. varno / dno references inside statements index into this table.
PLpgSQLParseMode
PostgreSQL’s raw grammar selected for an embedded PL/pgSQL expression.
PLpgSQLReturnValue
Value source for RETURN and RETURN NEXT. PostgreSQL 18 stores a simple datum reference in retvarno, distinct from a general SQL expression.
PLpgSQLStmt
Executable PL/pgSQL statement.
PLpgSQLVariableReference
RaiseLevel
RAISE severity.
RaiseOptionKind
VariableConflict
How a name in an embedded statement that is both a PL/pgSQL variable and a column or relation the statement can see resolves, as plpgsql.variable_conflict and #variable_conflict choose.

Traits§

VariableResolver
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 PostgreSQL analyzes as SELECT 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 SELECT body, 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 conflict directs.
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’s comp_options does: 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 through PostgreSQL’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§

PLpgSQLCursorArguments
PLpgSQLExpression
PLpgSQLStatement
VariableSiteResolver
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.