| The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide
by The PostgreSQL Global Development Group Paperback (6"x9"), 478 pages ISBN 9781906966065 RRP £14.95 ($19.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
9.5.2 Executing a Command With No Result
For any SQL command that does not return rows, for example
INSERT without a RETURNING clause, you can
execute the command within a PL/pgSQL function
just by writing the command.
Any PL/pgSQL variable name appearing in the command text is treated as a parameter, and then the current value of the variable is provided as the parameter value at run time. This is exactly like the processing described earlier for expressions; for details see section 9.10.1 Variable Substitution.
When executing a SQL command in this way, PL/pgSQL plans the command just once and re-uses the plan on subsequent executions, for the life of the database connection. The implications of this are discussed in detail in section 9.10.2 Plan Caching.
Sometimes it is useful to evaluate an expression or SELECT
query but discard the result, for example when calling a function
that has side-effects but no useful result value. To do
this in PL/pgSQL, use the
PERFORM statement:
PERFORM query;
This executes query and discards the
result. Write the query the same
way you would write an SQL SELECT command, but replace the
initial keyword SELECT with PERFORM.
PL/pgSQL variables will be
substituted into the query just as for commands that return no result,
and the plan is cached in the same way. Also, the special variable
FOUND is set to true if the query produced at
least one row, or false if it produced no rows (see
section 9.5.5 Obtaining the Result Status).
Note: One might expect that writing
SELECTdirectly would accomplish this result, but at present the only accepted way to do it isPERFORM. A SQL command that can return rows, such asSELECT, will be rejected as an error unless it has anINTOclause as discussed in the next section.
An example:
PERFORM create_mv('cs_session_page_requests_mv', my_query);
| ISBN 9781906966065 | The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide | See the print edition |