argument causes that query to be run and the result to
be returned in a result object.
</para>
<para>
If <replaceable>limit</replaceable> is specified and is greater than
zero, then <function>plpy.execute</function> retrieves at
most <replaceable>limit</replaceable> rows, much as if the query
included a <literal>LIMIT</literal>
clause. Omitting <replaceable>limit</replaceable> or specifying it as
zero results in no row limit.
</para>
<para>
The result object emulates a list or dictionary object. The result
object can be accessed by row number and column name. For example:
<programlisting>
rv = plpy.execute("SELECT * FROM my_table", 5)
</programlisting>
returns up to 5 rows from <literal>my_table</literal>. If
<literal>my_table</literal> has a column
<literal>my_column</literal>, it would be accessed as:
<programlisting>
foo = rv[i]["my_column"]
</programlisting>
The number of rows returned can be obtained using the built-in
<function>len</function> function.
</para>
<para>
The result object has these additional methods:
<variablelist>
<varlistentry>
<term><literal><function>nrows</function>()</literal></term>
<listitem>
<para>
Returns the number of rows processed by the command. Note that this
is not necessarily the same as the number of rows returned. For
example, an <command>UPDATE</command> command will set this value but
won't return any rows (unless <literal>RETURNING</literal> is used).
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal><function>status</function>()</literal></term>
<listitem>
<para>
The <function>SPI_execute()</function> return value.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal><function>colnames</function>()</literal></term>
<term><literal><function>coltypes</function>()</literal></term>
<term><literal><function>coltypmods</function>()</literal></term>
<listitem>
<para>
Return a list of column names, list of column type OIDs, and list of
type-specific type modifiers for the columns, respectively.
</para>
<para>
These methods raise an exception when called on a result object from
a command that did not produce a result set, e.g.,
<command>UPDATE</command> without <literal>RETURNING</literal>, or
<command>DROP TABLE</command>. But it is OK to use these methods on
a result set containing zero rows.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal><function>__str__</function>()</literal></term>
<listitem>
<para>
The standard <literal>__str__</literal> method is defined so that it
is possible for example to debug query execution results
using <literal>plpy.debug(rv)</literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
The result object can be modified.
</para>
<para>
Note that calling <literal>plpy.execute</literal> will cause the entire
result set to be read into memory. Only use that function when you are
sure that the result set will be relatively small. If you don't want to
risk excessive memory usage when fetching large results,
use <literal>plpy.cursor</literal> rather
than <literal>plpy.execute</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>plpy.<function>prepare</function>(<replaceable>query</replaceable> [, <replaceable>argtypes</replaceable>])</literal></term>
<term><literal>plpy.<function>execute</function>(<replaceable>plan</replaceable> [, <replaceable>arguments</replaceable>