OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / doc / postgresql / html / plpython.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >PL/Python - Python Procedural Language</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="Server Programming"
16 HREF="server-programming.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Missing Features"
19 HREF="plperl-missing.html"><LINK
20 REL="NEXT"
21 TITLE="Trigger Functions"
22 HREF="plpython-trigger.html"><LINK
23 REL="STYLESHEET"
24 TYPE="text/css"
25 HREF="stylesheet.css"><META
26 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="CHAPTER"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="plperl-missing.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="plperl.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 ></TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="spi.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="plpython-trigger.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="CHAPTER"
92 ><H1
93 ><A
94 NAME="PLPYTHON"
95 ></A
96 >Chapter 40. PL/Python - Python Procedural Language</H1
97 ><DIV
98 CLASS="TOC"
99 ><DL
100 ><DT
101 ><B
102 >Table of Contents</B
103 ></DT
104 ><DT
105 >40.1. <A
106 HREF="plpython.html#PLPYTHON-FUNCS"
107 >PL/Python Functions</A
108 ></DT
109 ><DT
110 >40.2. <A
111 HREF="plpython-trigger.html"
112 >Trigger Functions</A
113 ></DT
114 ><DT
115 >40.3. <A
116 HREF="plpython-database.html"
117 >Database Access</A
118 ></DT
119 ></DL
120 ></DIV
121 ><A
122 NAME="AEN33007"
123 ></A
124 ><A
125 NAME="AEN33009"
126 ></A
127 ><P
128 >  The <SPAN
129 CLASS="APPLICATION"
130 >PL/Python</SPAN
131 > procedural language allows
132   <SPAN
133 CLASS="PRODUCTNAME"
134 >PostgreSQL</SPAN
135 > functions to be written in the
136   <A
137 HREF="http://www.python.org"
138 TARGET="_top"
139 >Python</A
140 > language.
141  </P
142 ><P
143 >  To install PL/Python in a particular database, use
144   <TT
145 CLASS="LITERAL"
146 >createlang plpythonu <VAR
147 CLASS="REPLACEABLE"
148 >dbname</VAR
149 ></TT
150 >.
151  </P
152 ><DIV
153 CLASS="TIP"
154 ><BLOCKQUOTE
155 CLASS="TIP"
156 ><P
157 ><B
158 >Tip: </B
159 >    If a language is installed into <TT
160 CLASS="LITERAL"
161 >template1</TT
162 >, all subsequently
163     created databases will have the language installed automatically.
164    </P
165 ></BLOCKQUOTE
166 ></DIV
167 ><P
168 >  As of <SPAN
169 CLASS="PRODUCTNAME"
170 >PostgreSQL</SPAN
171 > 7.4, PL/Python is only
172   available as an <SPAN
173 CLASS="QUOTE"
174 >"untrusted"</SPAN
175 > language (meaning it does not
176   offer any way of restricting what users can do in it).  It has
177   therefore been renamed to <TT
178 CLASS="LITERAL"
179 >plpythonu</TT
180 >.  The trusted
181   variant <TT
182 CLASS="LITERAL"
183 >plpython</TT
184 > may become available again in future,
185   if a new secure execution mechanism is developed in Python.
186  </P
187 ><DIV
188 CLASS="NOTE"
189 ><BLOCKQUOTE
190 CLASS="NOTE"
191 ><P
192 ><B
193 >Note: </B
194 >   Users of source packages must specially enable the build of
195    PL/Python during the installation process.  (Refer to the
196    installation instructions for more information.)  Users of binary
197    packages might find PL/Python in a separate subpackage.
198   </P
199 ></BLOCKQUOTE
200 ></DIV
201 ><DIV
202 CLASS="SECT1"
203 ><H1
204 CLASS="SECT1"
205 ><A
206 NAME="PLPYTHON-FUNCS"
207 >40.1. PL/Python Functions</A
208 ></H1
209 ><P
210 >   The Python code you write gets transformed into a Python function.  E.g.,
211 </P><PRE
212 CLASS="PROGRAMLISTING"
213 >CREATE FUNCTION myfunc(text) RETURNS text
214     AS 'return args[0]'
215     LANGUAGE plpythonu;</PRE
216 ><P>
217
218    gets transformed into
219
220 </P><PRE
221 CLASS="PROGRAMLISTING"
222 >def __plpython_procedure_myfunc_23456():
223         return args[0]</PRE
224 ><P>
225
226    assuming that 23456 is the OID of the function.
227   </P
228 ><P
229 >   If you do not provide a return value, Python returns the default
230    <TT
231 CLASS="SYMBOL"
232 >None</TT
233 >. The language module translates Python's
234    <TT
235 CLASS="SYMBOL"
236 >None</TT
237 > into the SQL null
238    value.<A
239 NAME="AEN33036"
240 ></A
241 >
242   </P
243 ><P
244 >   The <SPAN
245 CLASS="PRODUCTNAME"
246 >PostgreSQL</SPAN
247 > function parameters are available in
248    the global <VAR
249 CLASS="VARNAME"
250 >args</VAR
251 > list.  In the
252    <CODE
253 CLASS="FUNCTION"
254 >myfunc</CODE
255 > example, <VAR
256 CLASS="VARNAME"
257 >args[0]</VAR
258 > contains
259    whatever was passed in as the text argument.  For
260    <TT
261 CLASS="LITERAL"
262 >myfunc2(text, integer)</TT
263 >, <VAR
264 CLASS="VARNAME"
265 >args[0]</VAR
266 >
267    would contain the <TT
268 CLASS="TYPE"
269 >text</TT
270 > argument and
271    <VAR
272 CLASS="VARNAME"
273 >args[1]</VAR
274 > the <TT
275 CLASS="TYPE"
276 >integer</TT
277 > argument.
278   </P
279 ><P
280 >   The global dictionary <VAR
281 CLASS="VARNAME"
282 >SD</VAR
283 > is available to store
284    data between function calls.  This variable is private static data.
285    The global dictionary <VAR
286 CLASS="VARNAME"
287 >GD</VAR
288 > is public data,
289    available to all Python functions within a session.  Use with
290    care.<A
291 NAME="AEN33052"
292 ></A
293 >
294   </P
295 ><P
296 >   Each function gets its own execution environment in the
297    Python interpreter, so that global data and function arguments from
298    <CODE
299 CLASS="FUNCTION"
300 >myfunc</CODE
301 > are not available to
302    <CODE
303 CLASS="FUNCTION"
304 >myfunc2</CODE
305 >.  The exception is the data in the
306    <VAR
307 CLASS="VARNAME"
308 >GD</VAR
309 > dictionary, as mentioned above.
310   </P
311 ></DIV
312 ></DIV
313 ><DIV
314 CLASS="NAVFOOTER"
315 ><HR
316 ALIGN="LEFT"
317 WIDTH="100%"><TABLE
318 SUMMARY="Footer navigation table"
319 WIDTH="100%"
320 BORDER="0"
321 CELLPADDING="0"
322 CELLSPACING="0"
323 ><TR
324 ><TD
325 WIDTH="33%"
326 ALIGN="left"
327 VALIGN="top"
328 ><A
329 HREF="plperl-missing.html"
330 ACCESSKEY="P"
331 >Prev</A
332 ></TD
333 ><TD
334 WIDTH="34%"
335 ALIGN="center"
336 VALIGN="top"
337 ><A
338 HREF="index.html"
339 ACCESSKEY="H"
340 >Home</A
341 ></TD
342 ><TD
343 WIDTH="33%"
344 ALIGN="right"
345 VALIGN="top"
346 ><A
347 HREF="plpython-trigger.html"
348 ACCESSKEY="N"
349 >Next</A
350 ></TD
351 ></TR
352 ><TR
353 ><TD
354 WIDTH="33%"
355 ALIGN="left"
356 VALIGN="top"
357 >Missing Features</TD
358 ><TD
359 WIDTH="34%"
360 ALIGN="center"
361 VALIGN="top"
362 ><A
363 HREF="server-programming.html"
364 ACCESSKEY="U"
365 >Up</A
366 ></TD
367 ><TD
368 WIDTH="33%"
369 ALIGN="right"
370 VALIGN="top"
371 >Trigger Functions</TD
372 ></TR
373 ></TABLE
374 ></DIV
375 ></BODY
376 ></HTML
377 >