PostgreSQL 9.4.1 Documentation | |||
---|---|---|---|
Prev | Up | Chapter 40.PL/pgSQL -SQL Procedural Language | Next |
40.4. Expressions
All expressions used inPL/pgSQL statements are processed using the server's mainSQL executor. For example, when you write aPL/pgSQL statement like PL/pgSQL will evaluate the expression by feeding a query like to the main SQL engine. While forming theSELECT command, any occurrences ofPL/pgSQL variable names are replaced by parameters, as discussed in detail inSection 40.10.1. This allows the query plan for theSELECT 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 aPREPARE command. For example, if we have declared two integer variablesx andy, and we write what happens behind the scenes is equivalent to and then this prepared statement isEXECUTEd for each execution of theIF statement, with the current values of thePL/pgSQL variables supplied as parameter values. Normally these details are not important to aPL/pgSQL user, but they are useful to know when trying to diagnose a problem. More information appears inSection 40.10.2.IFexpression THEN ...
SELECTexpression
IF x < y THEN ...
PREPAREstatement_name(integer, integer) AS SELECT $1 < $2;