~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/ref/prepare.sgml

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
doc/src/sgml/ref/prepare.sgml
 
3
PostgreSQL documentation
 
4
-->
 
5
 
 
6
<refentry id="SQL-PREPARE">
 
7
 <refmeta>
 
8
  <refentrytitle>PREPARE</refentrytitle>
 
9
  <manvolnum>7</manvolnum>
 
10
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 
11
 </refmeta>
 
12
 
 
13
 <refnamediv>
 
14
  <refname>PREPARE</refname>
 
15
  <refpurpose>prepare a statement for execution</refpurpose>
 
16
 </refnamediv>
 
17
 
 
18
 <indexterm zone="sql-prepare">
 
19
  <primary>PREPARE</primary>
 
20
 </indexterm>
 
21
 
 
22
 <indexterm zone="sql-prepare">
 
23
  <primary>prepared statements</primary>
 
24
  <secondary>creating</secondary>
 
25
 </indexterm>
 
26
 
 
27
 <refsynopsisdiv>
 
28
<synopsis>
 
29
PREPARE <replaceable class="PARAMETER">name</replaceable> [ ( <replaceable class="PARAMETER">data_type</replaceable> [, ...] ) ] AS <replaceable class="PARAMETER">statement</replaceable>
 
30
</synopsis>
 
31
 </refsynopsisdiv>
 
32
 
 
33
 <refsect1>
 
34
  <title>Description</title>
 
35
 
 
36
  <para>
 
37
   <command>PREPARE</command> creates a prepared statement. A prepared
 
38
   statement is a server-side object that can be used to optimize
 
39
   performance. When the <command>PREPARE</command> statement is
 
40
   executed, the specified statement is parsed, rewritten, and
 
41
   planned. When an <command>EXECUTE</command> command is subsequently
 
42
   issued, the prepared statement need only be executed. Thus, the
 
43
   parsing, rewriting, and planning stages are only performed once,
 
44
   instead of every time the statement is executed.
 
45
  </para>
 
46
 
 
47
  <para>
 
48
   Prepared statements can take parameters: values that are
 
49
   substituted into the statement when it is executed. When creating
 
50
   the prepared statement, refer to parameters by position, using
 
51
   <literal>$1</>, <literal>$2</>, etc. A corresponding list of
 
52
   parameter data types can optionally be specified. When a
 
53
   parameter's data type is not specified or is declared as
 
54
   <literal>unknown</literal>, the type is inferred from the context
 
55
   in which the parameter is used (if possible). When executing the
 
56
   statement, specify the actual values for these parameters in the
 
57
   <command>EXECUTE</command> statement.  Refer to <xref
 
58
   linkend="sql-execute"> for more
 
59
   information about that.
 
60
  </para>
 
61
 
 
62
  <para>
 
63
   Prepared statements only last for the duration of the current
 
64
   database session. When the session ends, the prepared statement is
 
65
   forgotten, so it must be recreated before being used again. This
 
66
   also means that a single  prepared statement cannot be used by
 
67
   multiple simultaneous database clients; however, each client can create
 
68
   their own prepared statement to use.  The prepared statement can be
 
69
   manually cleaned up using the <xref linkend="sql-deallocate"> command.
 
70
  </para>
 
71
 
 
72
  <para>
 
73
   Prepared statements have the largest performance advantage when a
 
74
   single session is being used to execute a large number of similar
 
75
   statements. The performance difference will be particularly
 
76
   significant if the statements are complex to plan or rewrite, for
 
77
   example, if the query involves a join of many tables or requires
 
78
   the application of several rules. If the statement is relatively simple
 
79
   to plan and rewrite but relatively expensive to execute, the
 
80
   performance advantage of prepared statements will be less noticeable.
 
81
  </para>
 
82
 </refsect1>
 
83
 
 
84
 <refsect1>
 
85
  <title>Parameters</title>
 
86
 
 
87
  <variablelist>
 
88
   <varlistentry>
 
89
    <term><replaceable class="PARAMETER">name</replaceable></term>
 
90
    <listitem>
 
91
     <para>
 
92
      An arbitrary name given to this particular prepared
 
93
      statement. It must be unique within a single session and is
 
94
      subsequently used to execute or deallocate a previously prepared
 
95
      statement.
 
96
     </para>
 
97
    </listitem>
 
98
   </varlistentry>
 
99
 
 
100
   <varlistentry>
 
101
    <term><replaceable class="PARAMETER">data_type</replaceable></term>
 
102
    <listitem>
 
103
     <para>
 
104
      The data type of a parameter to the prepared statement.  If the
 
105
      data type of a particular parameter is unspecified or is
 
106
      specified as <literal>unknown</literal>, it will be inferred
 
107
      from the context in which the parameter is used. To refer to the
 
108
      parameters in the prepared statement itself, use
 
109
      <literal>$1</literal>, <literal>$2</literal>, etc.
 
110
     </para>
 
111
    </listitem>
 
112
   </varlistentry>
 
113
 
 
114
   <varlistentry>
 
115
    <term><replaceable class="PARAMETER">statement</replaceable></term>
 
116
    <listitem>
 
117
     <para>
 
118
      Any <command>SELECT</>, <command>INSERT</>, <command>UPDATE</>,
 
119
      <command>DELETE</>, or <command>VALUES</> statement.
 
120
     </para>
 
121
    </listitem>
 
122
   </varlistentry>
 
123
  </variablelist>
 
124
 </refsect1>
 
125
 
 
126
 <refsect1>
 
127
  <title>Notes</title>
 
128
 
 
129
  <para>
 
130
   In some situations, the query plan produced for a prepared
 
131
   statement will be inferior to the query plan that would have been
 
132
   chosen if the statement had been submitted and executed
 
133
   normally. This is because when the statement is planned and the
 
134
   planner attempts to determine the optimal query plan, the actual
 
135
   values of any parameters specified in the statement are
 
136
   unavailable. <productname>PostgreSQL</productname> collects
 
137
   statistics on the distribution of data in the table, and can use
 
138
   constant values in a statement to make guesses about the likely
 
139
   result of executing the statement. Since this data is unavailable
 
140
   when planning prepared statements with parameters, the chosen plan
 
141
   might be suboptimal. To examine the query plan
 
142
   <productname>PostgreSQL</productname> has chosen for a prepared
 
143
   statement, use <xref linkend="sql-explain">.
 
144
  </para>
 
145
 
 
146
  <para>
 
147
   For more information on query planning and the statistics collected
 
148
   by <productname>PostgreSQL</productname> for that purpose, see
 
149
   the <xref linkend="sql-analyze">
 
150
   documentation.
 
151
  </para>
 
152
 
 
153
  <para>
 
154
   You can see all available prepared statements of a session by querying the
 
155
   <link linkend="view-pg-prepared-statements"><structname>pg_prepared_statements</structname></link>
 
156
   system view.
 
157
  </para>
 
158
 </refsect1>
 
159
 
 
160
 <refsect1 id="sql-prepare-examples">
 
161
  <title id="sql-prepare-examples-title">Examples</title>
 
162
  <para>
 
163
   Create a prepared statement for an <command>INSERT</command>
 
164
   statement, and then execute it:
 
165
<programlisting>
 
166
PREPARE fooplan (int, text, bool, numeric) AS
 
167
    INSERT INTO foo VALUES($1, $2, $3, $4);
 
168
EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);
 
169
</programlisting>
 
170
  </para>
 
171
 
 
172
  <para>
 
173
   Create a prepared statement for a <command>SELECT</command>
 
174
   statement, and then execute it:
 
175
<programlisting>
 
176
PREPARE usrrptplan (int) AS
 
177
    SELECT * FROM users u, logs l WHERE u.usrid=$1 AND u.usrid=l.usrid
 
178
    AND l.date = $2;
 
179
EXECUTE usrrptplan(1, current_date);
 
180
</programlisting>
 
181
 
 
182
   Note that the data type of the second parameter is not specified,
 
183
   so it is inferred from the context in which <literal>$2</> is used.
 
184
  </para>
 
185
 </refsect1>
 
186
 <refsect1>
 
187
  <title>Compatibility</title>
 
188
 
 
189
  <para>
 
190
   The SQL standard includes a <command>PREPARE</command> statement,
 
191
   but it is only for use in embedded SQL. This version of the
 
192
   <command>PREPARE</command> statement also uses a somewhat different
 
193
   syntax.
 
194
  </para>
 
195
 </refsect1>
 
196
 
 
197
 <refsect1>
 
198
  <title>See Also</title>
 
199
 
 
200
  <simplelist type="inline">
 
201
   <member><xref linkend="sql-deallocate"></member>
 
202
   <member><xref linkend="sql-execute"></member>
 
203
  </simplelist>
 
204
 </refsect1>
 
205
</refentry>