From 5d723d05c04fc0f589d4bb5ecb8780c4c0cb4302 Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Sun, 5 Mar 2006 16:40:51 +0000 Subject: Prepared queries for PLPerl, plus fixing a small plperl memory leak. Patch and docs from Dmitry Karasik, slightly editorialised. --- doc/src/sgml/plperl.sgml | 72 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/plperl.sgml b/doc/src/sgml/plperl.sgml index 0bde2450737..c71a8276e07 100644 --- a/doc/src/sgml/plperl.sgml +++ b/doc/src/sgml/plperl.sgml @@ -1,5 +1,5 @@ @@ -296,7 +296,7 @@ BEGIN { strict->import(); } - PL/Perl provides three additional Perl commands: + PL/Perl provides additional Perl commands: @@ -306,9 +306,13 @@ BEGIN { strict->import(); } spi_exec_query(query [, max-rows]) - spi_exec_query(command) spi_query(command) - spi_fetchrow(command) + spi_fetchrow(cursor) + spi_prepare(command, argument types) + spi_exec_prepared(plan) + spi_query_prepared(plan [, attributes], arguments) + spi_cursor_close(cursor) + spi_freeplan(plan) @@ -419,6 +423,66 @@ $$ LANGUAGE plperlu; SELECT * from lotsa_md5(500); + + + spi_prepare, spi_query_prepared, spi_exec_prepared, + and spi_freeplan implement the same functionality but for prepared queries. Once + a query plan is prepared by a call to spi_prepare, the plan can be used instead + of the string query, either in spi_exec_prepared, where the result is the same as returned + by spi_exec_query, or in spi_query_prepared which returns a cursor + exactly as spi_query does, which can be later passed to spi_fetchrow. + + + + The advantage of prepared queries is that is it possible to use one prepared plan for more + than one query execution. After the plan is not needed anymore, it must be freed with + spi_freeplan: + + + + +CREATE OR REPLACE FUNCTION init() RETURNS INTEGER AS $$ + $_SHARED{my_plan} = spi_prepare( 'SELECT (now() + $1)::date AS now', 'INTERVAL'); +$$ LANGUAGE plperl; + +CREATE OR REPLACE FUNCTION add_time( INTERVAL ) RETURNS TEXT AS $$ + return spi_exec_prepared( + $_SHARED{my_plan}, + $_[0], + )->{rows}->[0]->{now}; +$$ LANGUAGE plperl; + +CREATE OR REPLACE FUNCTION done() RETURNS INTEGER AS $$ + spi_freeplan( $_SHARED{my_plan}); + undef $_SHARED{my_plan}; +$$ LANGUAGE plperl; + +SELECT init(); +SELECT add_time('1 day'), add_time('2 days'), add_time('3 days'); +SELECT done(); + + add_time | add_time | add_time +------------+------------+------------ + 2005-12-10 | 2005-12-11 | 2005-12-12 + + + + + Note that the parameter subscript in spi_prepare is defined via + $1, $2, $3, etc, so avoid declaring query strings in double quotes that might easily + lead to hard-to-catch bugs. + + + + spi_cursor_close can be used to abort sequence of + spi_fetchrow calls. Normally, the call to + spi_fetchrow that returns undef is + the signal that there are no more rows to read. Also + that call automatically frees the cursor associated with the query. If it is desired not + to read all retuned rows, spi_cursor_close must be + called to avoid memory leaks. + + -- cgit v1.2.3