All expressions used in PL/pgSQL statements are processed using the server's main SQL executor. For example, when you write a PL/pgSQL statement like
PL/pgSQL will evaluate the expression by feeding a query like
to the main SQL engine. While forming the
SELECT command, any occurrences of PL/pgSQL variable names are replaced by
parameters, as discussed in detail in Section 43.11.1.
This allows the query plan for the
SELECT to be prepared just once and then reused
for subsequent evaluations with different values of the
variables. Thus, what really happens on first use of an
expression is essentially a
command. For example, if we have declared two integer variables
y, and we write
IF x < y THEN ...
what happens behind the scenes is equivalent to
statement_name(integer, integer) AS SELECT $1 < $2;
and then this prepared statement is
EXECUTEd for each execution of the
IF statement, with the current values of the
PL/pgSQL variables supplied as
parameter values. Normally these details are not important to a
PL/pgSQL user, but they are
useful to know when trying to diagnose a problem. More
information appears in Section 43.11.2.
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.