~ubuntu-branches/ubuntu/trusty/postgresql-9.3/trusty-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Data Values</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.3.13 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="PL/Python - Python Procedural Language"
HREF="plpython.html"><LINK
REL="PREVIOUS"
TITLE="PL/Python Functions"
HREF="plpython-funcs.html"><LINK
REL="NEXT"
TITLE="Sharing Data"
HREF="plpython-sharing.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2016-05-09T21:13:26"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.3.13 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="PL/Python Functions"
HREF="plpython-funcs.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="plpython.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 43. PL/Python - Python Procedural Language</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Sharing Data"
HREF="plpython-sharing.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="PLPYTHON-DATA"
>43.3. Data Values</A
></H1
><P
>   Generally speaking, the aim of PL/Python is to provide
   a <SPAN
CLASS="QUOTE"
>"natural"</SPAN
> mapping between the PostgreSQL and the
   Python worlds.  This informs the data mapping rules described
   below.
  </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN61198"
>43.3.1. Data Type Mapping</A
></H2
><P
>    Function arguments are converted from their PostgreSQL type to a
    corresponding Python type:
    <P
></P
></P><UL
><LI
><P
>       PostgreSQL <TT
CLASS="TYPE"
>boolean</TT
> is converted to Python <TT
CLASS="TYPE"
>bool</TT
>.
      </P
></LI
><LI
><P
>       PostgreSQL <TT
CLASS="TYPE"
>smallint</TT
> and <TT
CLASS="TYPE"
>int</TT
> are
       converted to Python <TT
CLASS="TYPE"
>int</TT
>.
       PostgreSQL <TT
CLASS="TYPE"
>bigint</TT
> and <TT
CLASS="TYPE"
>oid</TT
> are converted
       to <TT
CLASS="TYPE"
>long</TT
> in Python 2 and to <TT
CLASS="TYPE"
>int</TT
> in
       Python 3.
      </P
></LI
><LI
><P
>       PostgreSQL <TT
CLASS="TYPE"
>real</TT
>, <TT
CLASS="TYPE"
>double</TT
>,
       and <TT
CLASS="TYPE"
>numeric</TT
> are converted to
       Python <TT
CLASS="TYPE"
>float</TT
>.  Note that for
       the <TT
CLASS="TYPE"
>numeric</TT
> this loses information and can lead to
       incorrect results.  This might be fixed in a future
       release.
      </P
></LI
><LI
><P
>       PostgreSQL <TT
CLASS="TYPE"
>bytea</TT
> is converted to
       Python <TT
CLASS="TYPE"
>str</TT
> in Python 2 and to <TT
CLASS="TYPE"
>bytes</TT
>
       in Python 3.  In Python 2, the string should be treated as a
       byte sequence without any character encoding.
      </P
></LI
><LI
><P
>       All other data types, including the PostgreSQL character string
       types, are converted to a Python <TT
CLASS="TYPE"
>str</TT
>.  In Python
       2, this string will be in the PostgreSQL server encoding; in
       Python 3, it will be a Unicode string like all strings.
      </P
></LI
><LI
><P
>       For nonscalar data types, see below.
      </P
></LI
></UL
><P>
   </P
><P
>    Function return values are converted to the declared PostgreSQL
    return data type as follows:
    <P
></P
></P><UL
><LI
><P
>       When the PostgreSQL return type is <TT
CLASS="TYPE"
>boolean</TT
>, the
       return value will be evaluated for truth according to the
       <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Python</I
></SPAN
> rules.  That is, 0 and empty string
       are false, but notably <TT
CLASS="LITERAL"
>'f'</TT
> is true.
      </P
></LI
><LI
><P
>       When the PostgreSQL return type is <TT
CLASS="TYPE"
>bytea</TT
>, the
       return value will be converted to a string (Python 2) or bytes
       (Python 3) using the respective Python built-ins, with the
       result being converted <TT
CLASS="TYPE"
>bytea</TT
>.
      </P
></LI
><LI
><P
>       For all other PostgreSQL return types, the returned Python
       value is converted to a string using the Python
       built-in <TT
CLASS="LITERAL"
>str</TT
>, and the result is passed to the
       input function of the PostgreSQL data type.
      </P
><P
>       Strings in Python 2 are required to be in the PostgreSQL server
       encoding when they are passed to PostgreSQL.  Strings that are
       not valid in the current server encoding will raise an error,
       but not all encoding mismatches can be detected, so garbage
       data can still result when this is not done correctly.  Unicode
       strings are converted to the correct encoding automatically, so
       it can be safer and more convenient to use those.  In Python 3,
       all strings are Unicode strings.
      </P
></LI
><LI
><P
>       For nonscalar data types, see below.
      </P
></LI
></UL
><P>

    Note that logical mismatches between the declared PostgreSQL
    return type and the Python data type of the actual return object
    are not flagged; the value will be converted in any case.
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN61249"
>43.3.2. Null, None</A
></H2
><P
>   If an SQL null value is passed to a
   function, the argument value will appear as <TT
CLASS="SYMBOL"
>None</TT
> in
   Python. For example, the function definition of <CODE
CLASS="FUNCTION"
>pymax</CODE
>
   shown in <A
HREF="plpython-funcs.html"
>Section 43.2</A
> will return the wrong answer for null
   inputs. We could add <TT
CLASS="LITERAL"
>STRICT</TT
> to the function definition
   to make <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> do something more reasonable:
   if a null value is passed, the function will not be called at all,
   but will just return a null result automatically. Alternatively,
   we could check for null inputs in the function body:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION pymax (a integer, b integer)
  RETURNS integer
AS $$
  if (a is None) or (b is None):
    return None
  if a &gt; b:
    return a
  return b
$$ LANGUAGE plpythonu;</PRE
><P>

   As shown above, to return an SQL null value from a PL/Python
   function, return the value <TT
CLASS="SYMBOL"
>None</TT
>. This can be done whether the
   function is strict or not.
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="PLPYTHON-ARRAYS"
>43.3.3. Arrays, Lists</A
></H2
><P
>   SQL array values are passed into PL/Python as a Python list.  To
   return an SQL array value out of a PL/Python function, return a
   Python sequence, for example a list or tuple:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION return_arr()
  RETURNS int[]
AS $$
return (1, 2, 3, 4, 5)
$$ LANGUAGE plpythonu;

SELECT return_arr();
 return_arr  
-------------
 {1,2,3,4,5}
(1 row)</PRE
><P>

   Note that in Python, strings are sequences, which can have
   undesirable effects that might be familiar to Python programmers:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION return_str_arr()
  RETURNS varchar[]
AS $$
return "hello"
$$ LANGUAGE plpythonu;

SELECT return_str_arr();
 return_str_arr
----------------
 {h,e,l,l,o}
(1 row)</PRE
><P>
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN61267"
>43.3.4. Composite Types</A
></H2
><P
>   Composite-type arguments are passed to the function as Python mappings. The
   element names of the mapping are the attribute names of the composite type.
   If an attribute in the passed row has the null value, it has the value
   <TT
CLASS="SYMBOL"
>None</TT
> in the mapping. Here is an example:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE employee (
  name text,
  salary integer,
  age integer
);

CREATE FUNCTION overpaid (e employee)
  RETURNS boolean
AS $$
  if e["salary"] &gt; 200000:
    return True
  if (e["age"] &lt; 30) and (e["salary"] &gt; 100000):
    return True
  return False
$$ LANGUAGE plpythonu;</PRE
><P>
  </P
><P
>   There are multiple ways to return row or composite types from a Python
   function. The following examples assume we have:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TYPE named_value AS (
  name   text,
  value  integer
);</PRE
><P>

   A composite result can be returned as a:

   <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
>Sequence type (a tuple or list, but not a set because
     it is not indexable)</DT
><DD
><P
>       Returned sequence objects must have the same number of items as the
       composite result type has fields. The item with index 0 is assigned to
       the first field of the composite type, 1 to the second and so on. For
       example:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $$
  return [ name, value ]
  # or alternatively, as tuple: return ( name, value )
$$ LANGUAGE plpythonu;</PRE
><P>

       To return a SQL null for any column, insert <TT
CLASS="SYMBOL"
>None</TT
> at
       the corresponding position.
      </P
></DD
><DT
>Mapping (dictionary)</DT
><DD
><P
>       The value for each result type column is retrieved from the mapping
       with the column name as key. Example:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $$
  return { "name": name, "value": value }
$$ LANGUAGE plpythonu;</PRE
><P>

       Any extra dictionary key/value pairs are ignored. Missing keys are
       treated as errors.
       To return a SQL null value for any column, insert
       <TT
CLASS="SYMBOL"
>None</TT
> with the corresponding column name as the key.
      </P
></DD
><DT
>Object (any object providing method <TT
CLASS="LITERAL"
>__getattr__</TT
>)</DT
><DD
><P
>       This works the same as a mapping.
       Example:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $$
  class named_value:
    def __init__ (self, n, v):
      self.name = n
      self.value = v
  return named_value(name, value)

  # or simply
  class nv: pass
  nv.name = name
  nv.value = value
  return nv
$$ LANGUAGE plpythonu;</PRE
><P>
      </P
></DD
></DL
></DIV
><P>
  </P
><P
>    Functions with <TT
CLASS="LITERAL"
>OUT</TT
> parameters are also supported.  For example:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION multiout_simple(OUT i integer, OUT j integer) AS $$
return (1, 2)
$$ LANGUAGE plpythonu;

SELECT * FROM multiout_simple();</PRE
><P>
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN61296"
>43.3.5. Set-returning Functions</A
></H2
><P
>   A <SPAN
CLASS="APPLICATION"
>PL/Python</SPAN
> function can also return sets of
   scalar or composite types. There are several ways to achieve this because
   the returned object is internally turned into an iterator. The following
   examples assume we have composite type:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TYPE greeting AS (
  how text,
  who text
);</PRE
><P>

   A set result can be returned from a:

   <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
>Sequence type (tuple, list, set)</DT
><DD
><P
></P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION greet (how text)
  RETURNS SETOF greeting
AS $$
  # return tuple containing lists as composite types
  # all other combinations work also
  return ( [ how, "World" ], [ how, "PostgreSQL" ], [ how, "PL/Python" ] )
$$ LANGUAGE plpythonu;</PRE
><P>
      </P
></DD
><DT
>Iterator (any object providing <TT
CLASS="SYMBOL"
>__iter__</TT
> and
      <TT
CLASS="SYMBOL"
>next</TT
> methods)</DT
><DD
><P
></P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION greet (how text)
  RETURNS SETOF greeting
AS $$
  class producer:
    def __init__ (self, how, who):
      self.how = how
      self.who = who
      self.ndx = -1

    def __iter__ (self):
      return self

    def next (self):
      self.ndx += 1
      if self.ndx == len(self.who):
        raise StopIteration
      return ( self.how, self.who[self.ndx] )

  return producer(how, [ "World", "PostgreSQL", "PL/Python" ])
$$ LANGUAGE plpythonu;</PRE
><P>
      </P
></DD
><DT
>Generator (<TT
CLASS="LITERAL"
>yield</TT
>)</DT
><DD
><P
></P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION greet (how text)
  RETURNS SETOF greeting
AS $$
  for who in [ "World", "PostgreSQL", "PL/Python" ]:
    yield ( how, who )
$$ LANGUAGE plpythonu;</PRE
><P>

       <DIV
CLASS="WARNING"
><P
></P
><TABLE
CLASS="WARNING"
BORDER="1"
WIDTH="90%"
><TR
><TD
ALIGN="CENTER"
><B
>Warning</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>         Due to Python
         <A
HREF="http://bugs.python.org/issue1483133"
TARGET="_top"
>bug #1483133</A
>,
         some debug versions of Python 2.4
         (configured and compiled with option <TT
CLASS="LITERAL"
>--with-pydebug</TT
>)
         are known to crash the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> server
         when using an iterator to return a set result.
         Unpatched versions of Fedora 4 contain this bug.
         It does not happen in production versions of Python or on patched
         versions of Fedora 4.
        </P
></TD
></TR
></TABLE
></DIV
>
      </P
></DD
></DL
></DIV
><P>
  </P
><P
>    Set-returning functions with <TT
CLASS="LITERAL"
>OUT</TT
> parameters
    (using <TT
CLASS="LITERAL"
>RETURNS SETOF record</TT
>) are also
    supported.  For example:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION multiout_simple_setof(n integer, OUT integer, OUT integer) RETURNS SETOF record AS $$
return [(1, 2)] * n
$$ LANGUAGE plpythonu;

SELECT * FROM multiout_simple_setof(3);</PRE
><P>
   </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="plpython-funcs.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="plpython-sharing.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>PL/Python Functions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="plpython.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Sharing Data</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>