1
by Martin Pitt
Import upstream version 9.3~beta1 |
1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
2 |
<HTML |
|
3 |
><HEAD |
|
4 |
><TITLE |
|
5 |
>Structure of PL/pgSQL</TITLE |
|
6 |
><META |
|
7 |
NAME="GENERATOR" |
|
8 |
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK |
|
9 |
REV="MADE" |
|
10 |
HREF="mailto:pgsql-docs@postgresql.org"><LINK |
|
11 |
REL="HOME" |
|
1.1.11
by Martin Pitt
Import upstream version 9.3.12 |
12 |
TITLE="PostgreSQL 9.3.12 Documentation" |
1
by Martin Pitt
Import upstream version 9.3~beta1 |
13 |
HREF="index.html"><LINK |
14 |
REL="UP" |
|
15 |
TITLE="PL/pgSQL - SQL Procedural Language" |
|
16 |
HREF="plpgsql.html"><LINK |
|
17 |
REL="PREVIOUS" |
|
18 |
TITLE="Overview" |
|
19 |
HREF="plpgsql-overview.html"><LINK |
|
20 |
REL="NEXT" |
|
21 |
TITLE="Declarations" |
|
22 |
HREF="plpgsql-declarations.html"><LINK |
|
23 |
REL="STYLESHEET" |
|
24 |
TYPE="text/css" |
|
25 |
HREF="stylesheet.css"><META |
|
26 |
HTTP-EQUIV="Content-Type" |
|
27 |
CONTENT="text/html; charset=ISO-8859-1"><META |
|
28 |
NAME="creation" |
|
1.1.11
by Martin Pitt
Import upstream version 9.3.12 |
29 |
CONTENT="2016-03-28T20:28:06"></HEAD |
1
by Martin Pitt
Import upstream version 9.3~beta1 |
30 |
><BODY |
31 |
CLASS="SECT1" |
|
32 |
><DIV |
|
33 |
CLASS="NAVHEADER" |
|
34 |
><TABLE |
|
35 |
SUMMARY="Header navigation table" |
|
36 |
WIDTH="100%" |
|
37 |
BORDER="0" |
|
38 |
CELLPADDING="0" |
|
39 |
CELLSPACING="0" |
|
40 |
><TR |
|
41 |
><TH |
|
42 |
COLSPAN="5" |
|
43 |
ALIGN="center" |
|
44 |
VALIGN="bottom" |
|
45 |
><A |
|
46 |
HREF="index.html" |
|
1.1.11
by Martin Pitt
Import upstream version 9.3.12 |
47 |
>PostgreSQL 9.3.12 Documentation</A |
1
by Martin Pitt
Import upstream version 9.3~beta1 |
48 |
></TH |
49 |
></TR |
|
50 |
><TR |
|
51 |
><TD |
|
52 |
WIDTH="10%" |
|
53 |
ALIGN="left" |
|
54 |
VALIGN="top" |
|
55 |
><A |
|
56 |
TITLE="Overview" |
|
57 |
HREF="plpgsql-overview.html" |
|
58 |
ACCESSKEY="P" |
|
59 |
>Prev</A |
|
60 |
></TD |
|
61 |
><TD |
|
62 |
WIDTH="10%" |
|
63 |
ALIGN="left" |
|
64 |
VALIGN="top" |
|
65 |
><A |
|
66 |
HREF="plpgsql.html" |
|
67 |
ACCESSKEY="U" |
|
68 |
>Up</A |
|
69 |
></TD |
|
70 |
><TD |
|
71 |
WIDTH="60%" |
|
72 |
ALIGN="center" |
|
73 |
VALIGN="bottom" |
|
74 |
>Chapter 40. <SPAN |
|
75 |
CLASS="APPLICATION" |
|
76 |
>PL/pgSQL</SPAN |
|
77 |
> - <ACRONYM |
|
78 |
CLASS="ACRONYM" |
|
79 |
>SQL</ACRONYM |
|
80 |
> Procedural Language</TD |
|
81 |
><TD |
|
82 |
WIDTH="20%" |
|
83 |
ALIGN="right" |
|
84 |
VALIGN="top" |
|
85 |
><A |
|
86 |
TITLE="Declarations" |
|
87 |
HREF="plpgsql-declarations.html" |
|
88 |
ACCESSKEY="N" |
|
89 |
>Next</A |
|
90 |
></TD |
|
91 |
></TR |
|
92 |
></TABLE |
|
93 |
><HR |
|
94 |
ALIGN="LEFT" |
|
95 |
WIDTH="100%"></DIV |
|
96 |
><DIV |
|
97 |
CLASS="SECT1" |
|
98 |
><H1 |
|
99 |
CLASS="SECT1" |
|
100 |
><A |
|
101 |
NAME="PLPGSQL-STRUCTURE" |
|
102 |
>40.2. Structure of <SPAN |
|
103 |
CLASS="APPLICATION" |
|
104 |
>PL/pgSQL</SPAN |
|
105 |
></A |
|
106 |
></H1 |
|
107 |
><P |
|
108 |
> <SPAN |
|
109 |
CLASS="APPLICATION" |
|
110 |
>PL/pgSQL</SPAN |
|
111 |
> is a block-structured language.
|
|
112 |
The complete text of a function definition must be a |
|
113 |
<I |
|
114 |
CLASS="FIRSTTERM" |
|
115 |
>block</I |
|
116 |
>. A block is defined as:
|
|
117 |
||
118 |
</P><PRE |
|
119 |
CLASS="SYNOPSIS" |
|
120 |
>[<SPAN |
|
121 |
CLASS="OPTIONAL" |
|
122 |
> <<<TT |
|
123 |
CLASS="REPLACEABLE" |
|
124 |
><I |
|
125 |
>label</I |
|
126 |
></TT |
|
127 |
>>> </SPAN |
|
128 |
>]
|
|
129 |
[<SPAN |
|
130 |
CLASS="OPTIONAL" |
|
131 |
> DECLARE
|
|
132 |
<TT |
|
133 |
CLASS="REPLACEABLE" |
|
134 |
><I |
|
135 |
>declarations</I |
|
136 |
></TT |
|
137 |
> </SPAN |
|
138 |
>]
|
|
139 |
BEGIN |
|
140 |
<TT |
|
141 |
CLASS="REPLACEABLE" |
|
142 |
><I |
|
143 |
>statements</I |
|
144 |
></TT |
|
145 |
>
|
|
146 |
END [<SPAN |
|
147 |
CLASS="OPTIONAL" |
|
148 |
> <TT |
|
149 |
CLASS="REPLACEABLE" |
|
150 |
><I |
|
151 |
>label</I |
|
152 |
></TT |
|
153 |
> </SPAN |
|
154 |
>];</PRE |
|
155 |
><P> |
|
156 |
</P |
|
157 |
><P |
|
158 |
> Each declaration and each statement within a block is terminated
|
|
159 |
by a semicolon. A block that appears within another block must |
|
160 |
have a semicolon after <TT |
|
161 |
CLASS="LITERAL" |
|
162 |
>END</TT |
|
163 |
>, as shown above;
|
|
164 |
however the final <TT |
|
165 |
CLASS="LITERAL" |
|
166 |
>END</TT |
|
167 |
> that
|
|
168 |
concludes a function body does not require a semicolon. |
|
169 |
</P |
|
170 |
><DIV |
|
171 |
CLASS="TIP" |
|
172 |
><BLOCKQUOTE |
|
173 |
CLASS="TIP" |
|
174 |
><P |
|
175 |
><B |
|
176 |
>Tip: </B |
|
177 |
> A common mistake is to write a semicolon immediately after
|
|
178 |
<TT |
|
179 |
CLASS="LITERAL" |
|
180 |
>BEGIN</TT |
|
181 |
>. This is incorrect and will result in a syntax error.
|
|
182 |
</P |
|
183 |
></BLOCKQUOTE |
|
184 |
></DIV |
|
185 |
><P |
|
186 |
> A <TT |
|
187 |
CLASS="REPLACEABLE" |
|
188 |
><I |
|
189 |
>label</I |
|
190 |
></TT |
|
191 |
> is only needed if you want to
|
|
192 |
identify the block for use |
|
193 |
in an <TT |
|
194 |
CLASS="LITERAL" |
|
195 |
>EXIT</TT |
|
196 |
> statement, or to qualify the names of the
|
|
197 |
variables declared in the block. If a label is given after |
|
198 |
<TT |
|
199 |
CLASS="LITERAL" |
|
200 |
>END</TT |
|
201 |
>, it must match the label at the block's beginning.
|
|
202 |
</P |
|
203 |
><P |
|
204 |
> All key words are case-insensitive.
|
|
205 |
Identifiers are implicitly converted to lower case |
|
206 |
unless double-quoted, just as they are in ordinary SQL commands. |
|
207 |
</P |
|
208 |
><P |
|
209 |
> Comments work the same way in <SPAN |
|
210 |
CLASS="APPLICATION" |
|
211 |
>PL/pgSQL</SPAN |
|
212 |
> code as in
|
|
213 |
ordinary SQL. A double dash (<TT |
|
214 |
CLASS="LITERAL" |
|
215 |
>--</TT |
|
216 |
>) starts a comment
|
|
217 |
that extends to the end of the line. A <TT |
|
218 |
CLASS="LITERAL" |
|
219 |
>/*</TT |
|
220 |
> starts a
|
|
221 |
block comment that extends to the matching occurrence of |
|
222 |
<TT |
|
223 |
CLASS="LITERAL" |
|
224 |
>*/</TT |
|
225 |
>. Block comments nest.
|
|
226 |
</P |
|
227 |
><P |
|
228 |
> Any statement in the statement section of a block
|
|
229 |
can be a <I |
|
230 |
CLASS="FIRSTTERM" |
|
231 |
>subblock</I |
|
232 |
>. Subblocks can be used for
|
|
233 |
logical grouping or to localize variables to a small group |
|
234 |
of statements. Variables declared in a subblock mask any |
|
235 |
similarly-named variables of outer blocks for the duration |
|
236 |
of the subblock; but you can access the outer variables anyway |
|
237 |
if you qualify their names with their block's label. For example: |
|
238 |
</P><PRE |
|
239 |
CLASS="PROGRAMLISTING" |
|
240 |
>CREATE FUNCTION somefunc() RETURNS integer AS $$
|
|
241 |
<< outerblock >> |
|
242 |
DECLARE |
|
243 |
quantity integer := 30; |
|
244 |
BEGIN |
|
245 |
RAISE NOTICE 'Quantity here is %', quantity; -- Prints 30 |
|
246 |
quantity := 50; |
|
247 |
-- |
|
248 |
-- Create a subblock |
|
249 |
-- |
|
250 |
DECLARE |
|
251 |
quantity integer := 80; |
|
252 |
BEGIN |
|
253 |
RAISE NOTICE 'Quantity here is %', quantity; -- Prints 80 |
|
254 |
RAISE NOTICE 'Outer quantity here is %', outerblock.quantity; -- Prints 50 |
|
255 |
END; |
|
256 |
||
257 |
RAISE NOTICE 'Quantity here is %', quantity; -- Prints 50 |
|
258 |
||
259 |
RETURN quantity; |
|
260 |
END; |
|
261 |
$$ LANGUAGE plpgsql;</PRE |
|
262 |
><P> |
|
263 |
</P |
|
264 |
><DIV |
|
265 |
CLASS="NOTE" |
|
266 |
><BLOCKQUOTE |
|
267 |
CLASS="NOTE" |
|
268 |
><P |
|
269 |
><B |
|
270 |
>Note: </B |
|
271 |
> There is actually a hidden <SPAN |
|
272 |
CLASS="QUOTE" |
|
273 |
>"outer block"</SPAN |
|
274 |
> surrounding the body
|
|
275 |
of any <SPAN |
|
276 |
CLASS="APPLICATION" |
|
277 |
>PL/pgSQL</SPAN |
|
278 |
> function. This block provides the
|
|
279 |
declarations of the function's parameters (if any), as well as some |
|
280 |
special variables such as <TT |
|
281 |
CLASS="LITERAL" |
|
282 |
>FOUND</TT |
|
283 |
> (see
|
|
284 |
<A |
|
285 |
HREF="plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS" |
|
286 |
>Section 40.5.5</A |
|
287 |
>). The outer block is
|
|
288 |
labeled with the function's name, meaning that parameters and special |
|
289 |
variables can be qualified with the function's name. |
|
290 |
</P |
|
291 |
></BLOCKQUOTE |
|
292 |
></DIV |
|
293 |
><P |
|
294 |
> It is important not to confuse the use of
|
|
295 |
<TT |
|
296 |
CLASS="COMMAND" |
|
297 |
>BEGIN</TT |
|
298 |
>/<TT |
|
299 |
CLASS="COMMAND" |
|
300 |
>END</TT |
|
301 |
> for grouping statements in
|
|
302 |
<SPAN |
|
303 |
CLASS="APPLICATION" |
|
304 |
>PL/pgSQL</SPAN |
|
305 |
> with the similarly-named SQL commands
|
|
306 |
for transaction |
|
307 |
control. <SPAN |
|
308 |
CLASS="APPLICATION" |
|
309 |
>PL/pgSQL</SPAN |
|
310 |
>'s <TT |
|
311 |
CLASS="COMMAND" |
|
312 |
>BEGIN</TT |
|
313 |
>/<TT |
|
314 |
CLASS="COMMAND" |
|
315 |
>END</TT |
|
316 |
>
|
|
317 |
are only for grouping; they do not start or end a transaction. |
|
318 |
Functions and trigger procedures are always executed within a transaction |
|
319 |
established by an outer query — they cannot start or commit that
|
|
320 |
transaction, since there would be no context for them to execute in. |
|
321 |
However, a block containing an <TT |
|
322 |
CLASS="LITERAL" |
|
323 |
>EXCEPTION</TT |
|
324 |
> clause effectively
|
|
325 |
forms a subtransaction that can be rolled back without affecting the |
|
326 |
outer transaction. For more about that see <A |
|
327 |
HREF="plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING" |
|
328 |
>Section 40.6.6</A |
|
329 |
>.
|
|
330 |
</P |
|
331 |
></DIV |
|
332 |
><DIV |
|
333 |
CLASS="NAVFOOTER" |
|
334 |
><HR |
|
335 |
ALIGN="LEFT" |
|
336 |
WIDTH="100%"><TABLE |
|
337 |
SUMMARY="Footer navigation table" |
|
338 |
WIDTH="100%" |
|
339 |
BORDER="0" |
|
340 |
CELLPADDING="0" |
|
341 |
CELLSPACING="0" |
|
342 |
><TR |
|
343 |
><TD |
|
344 |
WIDTH="33%" |
|
345 |
ALIGN="left" |
|
346 |
VALIGN="top" |
|
347 |
><A |
|
348 |
HREF="plpgsql-overview.html" |
|
349 |
ACCESSKEY="P" |
|
350 |
>Prev</A |
|
351 |
></TD |
|
352 |
><TD |
|
353 |
WIDTH="34%" |
|
354 |
ALIGN="center" |
|
355 |
VALIGN="top" |
|
356 |
><A |
|
357 |
HREF="index.html" |
|
358 |
ACCESSKEY="H" |
|
359 |
>Home</A |
|
360 |
></TD |
|
361 |
><TD |
|
362 |
WIDTH="33%" |
|
363 |
ALIGN="right" |
|
364 |
VALIGN="top" |
|
365 |
><A |
|
366 |
HREF="plpgsql-declarations.html" |
|
367 |
ACCESSKEY="N" |
|
368 |
>Next</A |
|
369 |
></TD |
|
370 |
></TR |
|
371 |
><TR |
|
372 |
><TD |
|
373 |
WIDTH="33%" |
|
374 |
ALIGN="left" |
|
375 |
VALIGN="top" |
|
376 |
>Overview</TD |
|
377 |
><TD |
|
378 |
WIDTH="34%" |
|
379 |
ALIGN="center" |
|
380 |
VALIGN="top" |
|
381 |
><A |
|
382 |
HREF="plpgsql.html" |
|
383 |
ACCESSKEY="U" |
|
384 |
>Up</A |
|
385 |
></TD |
|
386 |
><TD |
|
387 |
WIDTH="33%" |
|
388 |
ALIGN="right" |
|
389 |
VALIGN="top" |
|
390 |
>Declarations</TD |
|
391 |
></TR |
|
392 |
></TABLE |
|
393 |
></DIV |
|
394 |
></BODY |
|
395 |
></HTML |
|
396 |
>
|