|The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide
by The PostgreSQL Global Development Group
Paperback (6"x9"), 478 pages
RRP £14.95 ($19.95)
Sales of this book support the PostgreSQL project! Get a printed copy>>>
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
IF expression THEN ...
PL/pgSQL will evaluate the expression by feeding a query like
to the main SQL engine. While forming the
any occurrences of PL/pgSQL variable names
are replaced by parameters, as discussed in detail in
section 9.10.1 Variable Substitution.
This allows the query plan for the
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
PREPARE 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
PREPARE 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
The query plan prepared in this way is saved for the life of the database
connection, as described in
section 9.10.2 Plan Caching. Normally these details are
not important to a PL/pgSQL user, but
they are useful to know when trying to diagnose a problem.
|ISBN 9781906966065||The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide||See the print edition|