11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.50 2006/03/01 06:30:32 neilc Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.51 2006/03/05 16:40:51 adunstan Exp $
33-->
44
55 <chapter id="plperl">
@@ -296,7 +296,7 @@ BEGIN { strict->import(); }
296296 </para>
297297
298298 <para>
299- PL/Perl provides three additional Perl commands:
299+ PL/Perl provides additional Perl commands:
300300
301301 <variablelist>
302302 <varlistentry>
@@ -306,9 +306,13 @@ BEGIN { strict->import(); }
306306 </indexterm>
307307
308308 <term><literal><function>spi_exec_query</>(<replaceable>query</replaceable> [, <replaceable>max-rows</replaceable>])</literal></term>
309- <term><literal><function>spi_exec_query</>(<replaceable>command</replaceable>)</literal></term>
310309 <term><literal><function>spi_query</>(<replaceable>command</replaceable>)</literal></term>
311- <term><literal><function>spi_fetchrow</>(<replaceable>command</replaceable>)</literal></term>
310+ <term><literal><function>spi_fetchrow</>(<replaceable>cursor</replaceable>)</literal></term>
311+ <term><literal><function>spi_prepare</>(<replaceable>command</replaceable>, <replaceable>argument types</replaceable>)</literal></term>
312+ <term><literal><function>spi_exec_prepared</>(<replaceable>plan</replaceable>)</literal></term>
313+ <term><literal><function>spi_query_prepared</>(<replaceable>plan</replaceable> [, <replaceable>attributes</replaceable>], <replaceable>arguments</replaceable>)</literal></term>
314+ <term><literal><function>spi_cursor_close</>(<replaceable>cursor</replaceable>)</literal></term>
315+ <term><literal><function>spi_freeplan</>(<replaceable>plan</replaceable>)</literal></term>
312316
313317 <listitem>
314318 <para>
@@ -419,6 +423,66 @@ $$ LANGUAGE plperlu;
419423SELECT * from lotsa_md5(500);
420424</programlisting>
421425 </para>
426+
427+ <para>
428+ <literal>spi_prepare</literal>, <literal>spi_query_prepared</literal>, <literal>spi_exec_prepared</literal>,
429+ and <literal>spi_freeplan</literal> implement the same functionality but for prepared queries. Once
430+ a query plan is prepared by a call to <literal>spi_prepare</literal>, the plan can be used instead
431+ of the string query, either in <literal>spi_exec_prepared</literal>, where the result is the same as returned
432+ by <literal>spi_exec_query</literal>, or in <literal>spi_query_prepared</literal> which returns a cursor
433+ exactly as <literal>spi_query</literal> does, which can be later passed to <literal>spi_fetchrow</literal>.
434+ </para>
435+
436+ <para>
437+ The advantage of prepared queries is that is it possible to use one prepared plan for more
438+ than one query execution. After the plan is not needed anymore, it must be freed with
439+ <literal>spi_freeplan</literal>:
440+ </para>
441+
442+ <para>
443+ <programlisting>
444+ CREATE OR REPLACE FUNCTION init() RETURNS INTEGER AS $$
445+ $_SHARED{my_plan} = spi_prepare( 'SELECT (now() + $1)::date AS now', 'INTERVAL');
446+ $$ LANGUAGE plperl;
447+
448+ CREATE OR REPLACE FUNCTION add_time( INTERVAL ) RETURNS TEXT AS $$
449+ return spi_exec_prepared(
450+ $_SHARED{my_plan},
451+ $_[0],
452+ )->{rows}->[0]->{now};
453+ $$ LANGUAGE plperl;
454+
455+ CREATE OR REPLACE FUNCTION done() RETURNS INTEGER AS $$
456+ spi_freeplan( $_SHARED{my_plan});
457+ undef $_SHARED{my_plan};
458+ $$ LANGUAGE plperl;
459+
460+ SELECT init();
461+ SELECT add_time('1 day'), add_time('2 days'), add_time('3 days');
462+ SELECT done();
463+
464+ add_time | add_time | add_time
465+ ------------+------------+------------
466+ 2005-12-10 | 2005-12-11 | 2005-12-12
467+ </programlisting>
468+ </para>
469+
470+ <para>
471+ Note that the parameter subscript in <literal>spi_prepare</literal> is defined via
472+ $1, $2, $3, etc, so avoid declaring query strings in double quotes that might easily
473+ lead to hard-to-catch bugs.
474+ </para>
475+
476+ <para>
477+ <literal>spi_cursor_close</literal> can be used to abort sequence of
478+ <literal>spi_fetchrow</literal> calls. Normally, the call to
479+ <literal>spi_fetchrow</literal> that returns <literal>undef</literal> is
480+ the signal that there are no more rows to read. Also
481+ that call automatically frees the cursor associated with the query. If it is desired not
482+ to read all retuned rows, <literal>spi_cursor_close</literal> must be
483+ called to avoid memory leaks.
484+ </para>
485+
422486
423487 </listitem>
424488 </varlistentry>
0 commit comments