1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.34 2006/10/16 17:28:03 momjian Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.35 2006/10/21 18:33:05 tgl Exp $ -->
2
2
3
3
<chapter id="plpython">
4
4
<title>PL/Python - Python Procedural Language</title>
@@ -61,11 +61,11 @@ $$ LANGUAGE plpythonu;
61
61
62
62
<para>
63
63
The body of a function is simply a Python script. When the function
64
- is called, all unnamed arguments are passed as elements to the array
65
- <varname>args[]</varname> and named arguments as ordinary variables to the
66
- Python script. The result is returned from the Python code in the usual way,
67
- with <literal>return</literal> or <literal>yield</literal> (in case of
68
- a resultset statement).
64
+ is called, its arguments are passed as elements of the array
65
+ <varname>args[]</varname>; named arguments are also passed as ordinary
66
+ variables to the Python script. The result is returned from the Python code
67
+ in the usual way, with <literal>return</literal> or
68
+ <literal>yield</literal> (in case of a resultset statement).
69
69
</para>
70
70
71
71
<para>
@@ -101,9 +101,9 @@ def __plpython_procedure_pymax_23456():
101
101
the global <varname>args</varname> list. In the
102
102
<function>pymax</function> example, <varname>args[0]</varname> contains
103
103
whatever was passed in as the first argument and
104
- <varname>args[1]</varname> contains the second argument's value. Alternatively,
105
- one can use named parameters as shown in the example above. This greatly simplifies
106
- the reading and writing of <application>PL/Python</application> code .
104
+ <varname>args[1]</varname> contains the second argument's
105
+ value. Alternatively, one can use named parameters as shown in the example
106
+ above. Use of named parameters is usually more readable .
107
107
</para>
108
108
109
109
<para>
@@ -161,31 +161,27 @@ $$ LANGUAGE plpythonu;
161
161
162
162
<para>
163
163
There are multiple ways to return row or composite types from a Python
164
- scripts. In following examples we assume to have:
164
+ function. The following examples assume we have:
165
165
166
166
<programlisting>
167
- CREATE TABLE named_value (
168
- name text,
169
- value integer
170
- );
171
- </programlisting>
172
- or
173
- <programlisting>
174
167
CREATE TYPE named_value AS (
175
168
name text,
176
169
value integer
177
170
);
178
171
</programlisting>
179
172
173
+ A composite result can be returned as a:
174
+
180
175
<variablelist>
181
176
<varlistentry>
182
- <term>Sequence types ( tuple or list) , but not <literal> set</literal> ( because
177
+ <term>Sequence type (a tuple or list, but not a set because
183
178
it is not indexable)</term>
184
179
<listitem>
185
180
<para>
186
- Returned sequence objects must have the same number of items as
187
- composite types have fields. Item with index 0 is assigned to the first field
188
- of the composite type, 1 to second and so on. For example:
181
+ Returned sequence objects must have the same number of items as the
182
+ composite result type has fields. The item with index 0 is assigned to
183
+ the first field of the composite type, 1 to the second and so on. For
184
+ example:
189
185
190
186
<programlisting>
191
187
CREATE FUNCTION make_pair (name text, value integer)
@@ -196,7 +192,7 @@ AS $$
196
192
$$ LANGUAGE plpythonu;
197
193
</programlisting>
198
194
199
- To return SQL null in any column, insert <symbol>None</symbol> at
195
+ To return a SQL null for any column, insert <symbol>None</symbol> at
200
196
the corresponding position.
201
197
</para>
202
198
</listitem>
@@ -206,8 +202,8 @@ $$ LANGUAGE plpythonu;
206
202
<term>Mapping (dictionary)</term>
207
203
<listitem>
208
204
<para>
209
- Value for a composite type's column is retrieved from the mapping with
210
- the column name as key. Example:
205
+ The value for each result type column is retrieved from the mapping
206
+ with the column name as key. Example:
211
207
212
208
<programlisting>
213
209
CREATE FUNCTION make_pair (name text, value integer)
@@ -217,8 +213,9 @@ AS $$
217
213
$$ LANGUAGE plpythonu;
218
214
</programlisting>
219
215
220
- Additional dictionary key/value pairs are ignored. Missing keys are
221
- treated as errors, i.e. to return an SQL null value for any column, insert
216
+ Any extra dictionary key/value pairs are ignored. Missing keys are
217
+ treated as errors.
218
+ To return a SQL null value for any column, insert
222
219
<symbol>None</symbol> with the corresponding column name as the key.
223
220
</para>
224
221
</listitem>
@@ -228,6 +225,7 @@ $$ LANGUAGE plpythonu;
228
225
<term>Object (any object providing method <literal>__getattr__</literal>)</term>
229
226
<listitem>
230
227
<para>
228
+ This works the same as a mapping.
231
229
Example:
232
230
233
231
<programlisting>
@@ -261,9 +259,9 @@ $$ LANGUAGE plpythonu;
261
259
262
260
<para>
263
261
A <application>PL/Python</application> function can also return sets of
264
- scalar or composite types. There are serveral ways to achieve this because
265
- the returned object is internally turned into an iterator. For following
266
- examples, let's assume to have composite type:
262
+ scalar or composite types. There are several ways to achieve this because
263
+ the returned object is internally turned into an iterator. The following
264
+ examples assume we have composite type:
267
265
268
266
<programlisting>
269
267
CREATE TYPE greeting AS (
@@ -272,10 +270,11 @@ CREATE TYPE greeting AS (
272
270
);
273
271
</programlisting>
274
272
275
- Currently known iterable types are:
273
+ A set result can be returned from a:
274
+
276
275
<variablelist>
277
276
<varlistentry>
278
- <term>Sequence types (tuple, list, set)</term>
277
+ <term>Sequence type (tuple, list, set)</term>
279
278
<listitem>
280
279
<para>
281
280
<programlisting>
@@ -341,19 +340,17 @@ $$ LANGUAGE plpythonu;
341
340
<ulink url="http://sourceforge.net/tracker/index.php?func=detail&aid=1483133&group_id=5470&atid=105470">bug #1483133</ulink>,
342
341
some debug versions of Python 2.4
343
342
(configured and compiled with option <literal>--with-pydebug</literal>)
344
- are known to crash the <productname>PostgreSQL</productname> server.
343
+ are known to crash the <productname>PostgreSQL</productname> server
344
+ when using an iterator to return a set result.
345
345
Unpatched versions of Fedora 4 contain this bug.
346
- It does not happen in production version of Python or on patched
346
+ It does not happen in production versions of Python or on patched
347
347
versions of Fedora 4.
348
348
</para>
349
349
</warning>
350
350
</para>
351
351
</listitem>
352
352
</varlistentry>
353
353
</variablelist>
354
-
355
- Whenever new iterable types are added to Python language,
356
- <application>PL/Python</application> is ready to use it.
357
354
</para>
358
355
359
356
<para>
0 commit comments