| Server IP : 172.67.134.114 / Your IP : 162.159.115.42 Web Server : Apache/2.4.37 System : Linux almalinux.duckdns.org 4.18.0-553.111.1.el8_10.x86_64 #1 SMP Sun Mar 8 20:06:07 EDT 2026 x86_64 User : ricodeal ( 1046) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/share/doc/postgresql-docs/html/ |
Upload File : |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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>PREPARE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="[email protected]" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-notify.html" title="NOTIFY" /><link rel="next" href="sql-prepare-transaction.html" title="PREPARE TRANSACTION" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">PREPARE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-notify.html" title="NOTIFY">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 10.23 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-prepare-transaction.html" title="PREPARE TRANSACTION">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-PREPARE"><div class="titlepage"></div><a id="id-1.9.3.152.1" class="indexterm"></a><a id="id-1.9.3.152.2" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">PREPARE</span></h2><p>PREPARE — prepare a statement for execution</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">PREPARE <em class="replaceable"><code>name</code></em> [ ( <em class="replaceable"><code>data_type</code></em> [, ...] ) ] AS <em class="replaceable"><code>statement</code></em></pre></div><div class="refsect1" id="id-1.9.3.152.6"><h2>Description</h2><p> <code class="command">PREPARE</code> creates a prepared statement. A prepared
statement is a server-side object that can be used to optimize
performance. When the <code class="command">PREPARE</code> statement is
executed, the specified statement is parsed, analyzed, and rewritten.
When an <code class="command">EXECUTE</code> command is subsequently
issued, the prepared statement is planned and executed. This division
of labor avoids repetitive parse analysis work, while allowing
the execution plan to depend on the specific parameter values supplied.
</p><p> Prepared statements can take parameters: values that are
substituted into the statement when it is executed. When creating
the prepared statement, refer to parameters by position, using
<code class="literal">$1</code>, <code class="literal">$2</code>, etc. A corresponding list of
parameter data types can optionally be specified. When a
parameter's data type is not specified or is declared as
<code class="literal">unknown</code>, the type is inferred from the context
in which the parameter is first used (if possible). When executing the
statement, specify the actual values for these parameters in the
<code class="command">EXECUTE</code> statement. Refer to <a class="xref" href="sql-execute.html" title="EXECUTE"><span class="refentrytitle">EXECUTE</span></a> for more
information about that.
</p><p> Prepared statements only last for the duration of the current
database session. When the session ends, the prepared statement is
forgotten, so it must be recreated before being used again. This
also means that a single prepared statement cannot be used by
multiple simultaneous database clients; however, each client can create
their own prepared statement to use. Prepared statements can be
manually cleaned up using the <a class="xref" href="sql-deallocate.html" title="DEALLOCATE"><span class="refentrytitle">DEALLOCATE</span></a> command.
</p><p> Prepared statements potentially have the largest performance advantage
when a single session is being used to execute a large number of similar
statements. The performance difference will be particularly
significant if the statements are complex to plan or rewrite, e.g.,
if the query involves a join of many tables or requires
the application of several rules. If the statement is relatively simple
to plan and rewrite but relatively expensive to execute, the
performance advantage of prepared statements will be less noticeable.
</p></div><div class="refsect1" id="id-1.9.3.152.7"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p> An arbitrary name given to this particular prepared
statement. It must be unique within a single session and is
subsequently used to execute or deallocate a previously prepared
statement.
</p></dd><dt><span class="term"><em class="replaceable"><code>data_type</code></em></span></dt><dd><p> The data type of a parameter to the prepared statement. If the
data type of a particular parameter is unspecified or is
specified as <code class="literal">unknown</code>, it will be inferred
from the context in which the parameter is first used. To refer to the
parameters in the prepared statement itself, use
<code class="literal">$1</code>, <code class="literal">$2</code>, etc.
</p></dd><dt><span class="term"><em class="replaceable"><code>statement</code></em></span></dt><dd><p> Any <code class="command">SELECT</code>, <code class="command">INSERT</code>, <code class="command">UPDATE</code>,
<code class="command">DELETE</code>, or <code class="command">VALUES</code> statement.
</p></dd></dl></div></div><div class="refsect1" id="SQL-PREPARE-NOTES"><h2>Notes</h2><p> Prepared statements can use generic plans rather than re-planning with
each set of supplied <code class="command">EXECUTE</code> values. This occurs
immediately for prepared statements with no parameters; otherwise
it occurs only after five or more executions produce plans whose
estimated cost average (including planning overhead) is more expensive
than the generic plan cost estimate. Once a generic plan is chosen,
it is used for the remaining lifetime of the prepared statement.
Using <code class="command">EXECUTE</code> values which are rare in columns with
many duplicates can generate custom plans that are so much cheaper
than the generic plan, even after adding planning overhead, that the
generic plan might never be used.
</p><p> A generic plan assumes that each value supplied to
<code class="command">EXECUTE</code> is one of the column's distinct values
and that column values are uniformly distributed. For example,
if statistics record three distinct column values, a generic plan
assumes a column equality comparison will match 33% of processed rows.
Column statistics also allow generic plans to accurately compute the
selectivity of unique columns. Comparisons on non-uniformly-distributed
columns and specification of non-existent values affects the average
plan cost, and hence if and when a generic plan is chosen.
</p><p> To examine the query plan <span class="productname">PostgreSQL</span> is using
for a prepared statement, use <a class="xref" href="sql-explain.html" title="EXPLAIN"><span class="refentrytitle">EXPLAIN</span></a>, e.g.,
<code class="command">EXPLAIN EXECUTE</code>.
If a generic plan is in use, it will contain parameter symbols
<code class="literal">$<em class="replaceable"><code>n</code></em></code>, while a custom plan will have the
supplied parameter values substituted into it.
The row estimates in the generic plan reflect the selectivity
computed for the parameters.
</p><p> For more information on query planning and the statistics collected
by <span class="productname">PostgreSQL</span> for that purpose, see
the <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a>
documentation.
</p><p> Although the main point of a prepared statement is to avoid repeated parse
analysis and planning of the statement, <span class="productname">PostgreSQL</span> will
force re-analysis and re-planning of the statement before using it
whenever database objects used in the statement have undergone
definitional (DDL) changes since the previous use of the prepared
statement. Also, if the value of <a class="xref" href="runtime-config-client.html#GUC-SEARCH-PATH">search_path</a> changes
from one use to the next, the statement will be re-parsed using the new
<code class="varname">search_path</code>. (This latter behavior is new as of
<span class="productname">PostgreSQL</span> 9.3.) These rules make use of a
prepared statement semantically almost equivalent to re-submitting the
same query text over and over, but with a performance benefit if no object
definitions are changed, especially if the best plan remains the same
across uses. An example of a case where the semantic equivalence is not
perfect is that if the statement refers to a table by an unqualified name,
and then a new table of the same name is created in a schema appearing
earlier in the <code class="varname">search_path</code>, no automatic re-parse will occur
since no object used in the statement changed. However, if some other
change forces a re-parse, the new table will be referenced in subsequent
uses.
</p><p> You can see all prepared statements available in the session by querying the
<a class="link" href="view-pg-prepared-statements.html" title="51.76. pg_prepared_statements"><code class="structname">pg_prepared_statements</code></a>
system view.
</p></div><div class="refsect1" id="SQL-PREPARE-EXAMPLES"><h2>Examples</h2><p> Create a prepared statement for an <code class="command">INSERT</code>
statement, and then execute it:
</p><pre class="programlisting">PREPARE fooplan (int, text, bool, numeric) AS
INSERT INTO foo VALUES($1, $2, $3, $4);
EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);</pre><p>
</p><p> Create a prepared statement for a <code class="command">SELECT</code>
statement, and then execute it:
</p><pre class="programlisting">PREPARE usrrptplan (int) AS
SELECT * FROM users u, logs l WHERE u.usrid=$1 AND u.usrid=l.usrid
AND l.date = $2;
EXECUTE usrrptplan(1, current_date);</pre><p>
Note that the data type of the second parameter is not specified,
so it is inferred from the context in which <code class="literal">$2</code> is used.
</p></div><div class="refsect1" id="id-1.9.3.152.10"><h2>Compatibility</h2><p> The SQL standard includes a <code class="command">PREPARE</code> statement,
but it is only for use in embedded SQL. This version of the
<code class="command">PREPARE</code> statement also uses a somewhat different
syntax.
</p></div><div class="refsect1" id="id-1.9.3.152.11"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-deallocate.html" title="DEALLOCATE"><span class="refentrytitle">DEALLOCATE</span></a>, <a class="xref" href="sql-execute.html" title="EXECUTE"><span class="refentrytitle">EXECUTE</span></a></span></div></div><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navfooter"><hr></hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-notify.html" title="NOTIFY">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-prepare-transaction.html" title="PREPARE TRANSACTION">Next</a></td></tr><tr><td width="40%" align="left" valign="top">NOTIFY </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 10.23 Documentation">Home</a></td><td width="40%" align="right" valign="top"> PREPARE TRANSACTION</td></tr></table></div></body></html>