codekingpro/portable-devtools
114k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>SPI_prepare</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="spi-spi-execute-with-args.html" title="SPI_execute_with_args" /><link rel="next" href="spi-spi-prepare-cursor.html" title="SPI_prepare_cursor" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">SPI_prepare</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="spi-spi-execute-with-args.html" title="SPI_execute_with_args">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="spi-interface.html" title="47.1. Interface Functions">Up</a></td><th width="60%" align="center">47.1. Interface Functions</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="spi-spi-prepare-cursor.html" title="SPI_prepare_cursor">Next</a></td></tr></table><hr /></div><div class="refentry" id="SPI-SPI-PREPARE"><div class="titlepage"></div><a id="id-1.8.12.8.8.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SPI_prepare</span></h2><p>SPI_prepare — prepare a statement, without executing it yet</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">3SPIPlanPtr SPI_prepare(const char * <em class="parameter"><code>command</code></em>, int <em class="parameter"><code>nargs</code></em>, Oid * <em class="parameter"><code>argtypes</code></em>)4</pre></div><div class="refsect1" id="id-1.8.12.8.8.5"><h2>Description</h2><p>5 <code class="function">SPI_prepare</code> creates and returns a prepared6 statement for the specified command, but doesn't execute the command.7 The prepared statement can later be executed repeatedly using8 <code class="function">SPI_execute_plan</code>.9 </p><p>10 When the same or a similar command is to be executed repeatedly, it11 is generally advantageous to perform parse analysis only once, and12 might furthermore be advantageous to re-use an execution plan for the13 command.14 <code class="function">SPI_prepare</code> converts a command string into a15 prepared statement that encapsulates the results of parse analysis.16 The prepared statement also provides a place for caching an execution plan17 if it is found that generating a custom plan for each execution is not18 helpful.19 </p><p>20 A prepared command can be generalized by writing parameters21 (<code class="literal">$1</code>, <code class="literal">$2</code>, etc.) in place of what would be22 constants in a normal command. The actual values of the parameters23 are then specified when <code class="function">SPI_execute_plan</code> is called.24 This allows the prepared command to be used over a wider range of25 situations than would be possible without parameters.26 </p><p>27 The statement returned by <code class="function">SPI_prepare</code> can be used28 only in the current invocation of the C function, since29 <code class="function">SPI_finish</code> frees memory allocated for such a30 statement. But the statement can be saved for longer using the functions31 <code class="function">SPI_keepplan</code> or <code class="function">SPI_saveplan</code>.32 </p></div><div class="refsect1" id="id-1.8.12.8.8.6"><h2>Arguments</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">const char * <em class="parameter"><code>command</code></em></code></span></dt><dd><p>33 command string34 </p></dd><dt><span class="term"><code class="literal">int <em class="parameter"><code>nargs</code></em></code></span></dt><dd><p>35 number of input parameters (<code class="literal">$1</code>, <code class="literal">$2</code>, etc.)36 </p></dd><dt><span class="term"><code class="literal">Oid * <em class="parameter"><code>argtypes</code></em></code></span></dt><dd><p>37 pointer to an array containing the <acronym class="acronym">OID</acronym>s of38 the data types of the parameters39 </p></dd></dl></div></div><div class="refsect1" id="id-1.8.12.8.8.7"><h2>Return Value</h2><p>40 <code class="function">SPI_prepare</code> returns a non-null pointer to an41 <code class="type">SPIPlan</code>, which is an opaque struct representing a prepared42 statement. On error, <code class="symbol">NULL</code> will be returned,43 and <code class="varname">SPI_result</code> will be set to one of the same44 error codes used by <code class="function">SPI_execute</code>, except that45 it is set to <code class="symbol">SPI_ERROR_ARGUMENT</code> if46 <em class="parameter"><code>command</code></em> is <code class="symbol">NULL</code>, or if47 <em class="parameter"><code>nargs</code></em> is less than 0, or if <em class="parameter"><code>nargs</code></em> is48 greater than 0 and <em class="parameter"><code>argtypes</code></em> is <code class="symbol">NULL</code>.49 </p></div><div class="refsect1" id="id-1.8.12.8.8.8"><h2>Notes</h2><p>50 If no parameters are defined, a generic plan will be created at the51 first use of <code class="function">SPI_execute_plan</code>, and used for all52 subsequent executions as well. If there are parameters, the first few uses53 of <code class="function">SPI_execute_plan</code> will generate custom plans54 that are specific to the supplied parameter values. After enough uses55 of the same prepared statement, <code class="function">SPI_execute_plan</code> will56 build a generic plan, and if that is not too much more expensive than the57 custom plans, it will start using the generic plan instead of re-planning58 each time. If this default behavior is unsuitable, you can alter it by59 passing the <code class="literal">CURSOR_OPT_GENERIC_PLAN</code> or60 <code class="literal">CURSOR_OPT_CUSTOM_PLAN</code> flag to61 <code class="function">SPI_prepare_cursor</code>, to force use of generic or custom62 plans respectively.63 </p><p>64 Although the main point of a prepared statement is to avoid repeated parse65 analysis and planning of the statement, <span class="productname">PostgreSQL</span> will66 force re-analysis and re-planning of the statement before using it67 whenever database objects used in the statement have undergone68 definitional (DDL) changes since the previous use of the prepared69 statement. Also, if the value of <a class="xref" href="runtime-config-client.html#GUC-SEARCH-PATH">search_path</a> changes70 from one use to the next, the statement will be re-parsed using the new71 <code class="varname">search_path</code>. (This latter behavior is new as of72 <span class="productname">PostgreSQL</span> 9.3.) See <a class="xref" href="sql-prepare.html" title="PREPARE"><span class="refentrytitle">PREPARE</span></a> for more information about the behavior of prepared73 statements.74 </p><p>75 This function should only be called from a connected C function.76 </p><p>77 <code class="type">SPIPlanPtr</code> is declared as a pointer to an opaque struct type in78 <code class="filename">spi.h</code>. It is unwise to try to access its contents79 directly, as that makes your code much more likely to break in80 future revisions of <span class="productname">PostgreSQL</span>.81 </p><p>82 The name <code class="type">SPIPlanPtr</code> is somewhat historical, since the data83 structure no longer necessarily contains an execution plan.84 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="spi-spi-execute-with-args.html" title="SPI_execute_with_args">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="spi-interface.html" title="47.1. Interface Functions">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="spi-spi-prepare-cursor.html" title="SPI_prepare_cursor">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SPI_execute_with_args </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> SPI_prepare_cursor</td></tr></table></div></body></html>