~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to doc/src/sgml/xfunc.sgml

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
$PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.96.4.3 2005-02-21 06:12:41 neilc Exp $
 
3
-->
 
4
 
 
5
 <sect1 id="xfunc">
 
6
  <title>User-Defined Functions</title>
 
7
 
 
8
  <indexterm zone="xfunc">
 
9
   <primary>function</primary>
 
10
   <secondary>user-defined</secondary>
 
11
  </indexterm>
 
12
 
 
13
  <para>
 
14
   <productname>PostgreSQL</productname> provides four kinds of
 
15
   functions:
 
16
 
 
17
   <itemizedlist>
 
18
    <listitem>
 
19
     <para>
 
20
      query language functions (functions written in
 
21
      <acronym>SQL</acronym>) (<xref linkend="xfunc-sql">)
 
22
     </para>
 
23
    </listitem>
 
24
    <listitem>
 
25
     <para>
 
26
      procedural language functions (functions written in, for
 
27
      example, <application>PL/pgSQL</> or <application>PL/Tcl</>)
 
28
      (<xref linkend="xfunc-pl">)
 
29
     </para>
 
30
    </listitem>
 
31
    <listitem>
 
32
     <para>
 
33
      internal functions (<xref linkend="xfunc-internal">)
 
34
     </para>
 
35
    </listitem>
 
36
    <listitem>
 
37
     <para>
 
38
      C-language functions (<xref linkend="xfunc-c">)
 
39
     </para>
 
40
    </listitem>
 
41
   </itemizedlist>
 
42
  </para>
 
43
 
 
44
  <para>
 
45
   Every kind
 
46
   of  function  can take base types, composite types, or
 
47
   combinations of these as arguments (parameters). In addition,
 
48
   every kind of function can return a base type or
 
49
   a composite type.  Functions may also be defined to return
 
50
   sets of base or composite values.
 
51
  </para>
 
52
 
 
53
  <para>
 
54
   Many kinds of functions can take or return certain pseudo-types
 
55
   (such as polymorphic types), but the available facilities vary.
 
56
   Consult the description of each kind of function for more details.
 
57
  </para>
 
58
 
 
59
  <para>
 
60
   It's easiest to define <acronym>SQL</acronym>
 
61
   functions, so we'll start by discussing those.
 
62
   Most of the concepts presented for <acronym>SQL</acronym> functions
 
63
   will carry over to the other types of functions.
 
64
  </para>
 
65
 
 
66
  <para>
 
67
   Throughout this chapter, it can be useful to look at the reference
 
68
   page of the <xref linkend="sql-createfunction"
 
69
   endterm="sql-createfunction-title"> command to
 
70
   understand the examples better.  Some examples from this chapter
 
71
   can be found in <filename>funcs.sql</filename> and
 
72
   <filename>funcs.c</filename> in the <filename>src/tutorial</>
 
73
   directory in the <productname>PostgreSQL</productname> source
 
74
   distribution.
 
75
  </para>
 
76
  </sect1>
 
77
 
 
78
  <sect1 id="xfunc-sql">
 
79
   <title>Query Language (<acronym>SQL</acronym>) Functions</title>
 
80
 
 
81
   <indexterm zone="xfunc-sql">
 
82
    <primary>function</primary>
 
83
    <secondary>user-defined</secondary>
 
84
    <tertiary>in SQL</tertiary>
 
85
   </indexterm>
 
86
 
 
87
   <para>
 
88
    SQL functions execute an arbitrary list of SQL statements, returning
 
89
    the result of the last query in the list.
 
90
    In the simple (non-set)
 
91
    case, the first row of the last query's result will be returned.
 
92
    (Bear in mind that <quote>the first row</quote> of a multirow
 
93
    result is not well-defined unless you use <literal>ORDER BY</>.)
 
94
    If the last query happens
 
95
    to return no rows at all, the null value will be returned.
 
96
   </para>
 
97
 
 
98
   <para>
 
99
    <indexterm><primary>SETOF</><seealso>function</></> Alternatively,
 
100
    an SQL function may be declared to return a set, by specifying the
 
101
    function's return type as <literal>SETOF
 
102
    <replaceable>sometype</></literal>.<indexterm><primary>SETOF</></>
 
103
    In this case all rows of the last query's result are returned.
 
104
    Further details appear below.
 
105
   </para>
 
106
 
 
107
   <para>
 
108
    The body of an SQL function must be a list of SQL
 
109
    statements separated by semicolons.  A semicolon after the last
 
110
    statement is optional.  Unless the function is declared to return
 
111
    <type>void</>, the last statement must be a <command>SELECT</>.
 
112
   </para>
 
113
 
 
114
    <para>
 
115
     Any collection of commands in the  <acronym>SQL</acronym>
 
116
     language can be packaged together and defined as a function.
 
117
     Besides <command>SELECT</command> queries, the commands can include data
 
118
     modification queries (<command>INSERT</command>,
 
119
     <command>UPDATE</command>, and <command>DELETE</command>), as well as
 
120
     other SQL commands. (The only exception is that you can't put
 
121
     <command>BEGIN</>, <command>COMMIT</>, <command>ROLLBACK</>, or
 
122
     <command>SAVEPOINT</> commands into a <acronym>SQL</acronym> function.)
 
123
     However, the final command 
 
124
     must be a <command>SELECT</command> that returns whatever is
 
125
     specified as the function's return type.  Alternatively, if you
 
126
     want to define a SQL function that performs actions but has no
 
127
     useful value to return, you can define it as returning <type>void</>.
 
128
     In that case, the function body must not end with a <command>SELECT</command>.
 
129
     For example, this function removes rows with negative salaries from
 
130
     the <literal>emp</> table:
 
131
 
 
132
<screen>
 
133
CREATE FUNCTION clean_emp() RETURNS void AS '
 
134
    DELETE FROM emp
 
135
        WHERE salary &lt; 0;
 
136
' LANGUAGE SQL;
 
137
 
 
138
SELECT clean_emp();
 
139
 
 
140
 clean_emp
 
141
-----------
 
142
 
 
143
(1 row)
 
144
</screen>
 
145
    </para>
 
146
 
 
147
   <para>
 
148
    The syntax of the <command>CREATE FUNCTION</command> command requires
 
149
    the function body to be written as a string constant.  It is usually
 
150
    most convenient to use dollar quoting (see <xref
 
151
    linkend="sql-syntax-dollar-quoting">) for the string constant.
 
152
    If you choose to use regular single-quoted string constant syntax,
 
153
    you must escape single quote marks (<literal>'</>) and backslashes
 
154
    (<literal>\</>) used in the body of the function, typically by
 
155
    doubling them (see <xref linkend="sql-syntax-strings">).
 
156
   </para>
 
157
 
 
158
   <para>
 
159
    Arguments to the SQL function are referenced in the function
 
160
    body using the syntax <literal>$<replaceable>n</></>: <literal>$1</>
 
161
    refers to the first argument, <literal>$2</> to the second, and so on.
 
162
    If an argument is of a composite type, then the dot notation,
 
163
    e.g., <literal>$1.name</literal>, may be used to access attributes
 
164
    of the argument.  The arguments can only be used as data values,
 
165
    not as identifiers.  Thus for example this is reasonable:
 
166
<programlisting>
 
167
INSERT INTO mytable VALUES ($1);
 
168
</programlisting>
 
169
but this will not work:
 
170
<programlisting>
 
171
INSERT INTO $1 VALUES (42);
 
172
</programlisting>
 
173
   </para>
 
174
 
 
175
   <sect2>
 
176
    <title><acronym>SQL</acronym> Functions on Base Types</title>
 
177
 
 
178
    <para>
 
179
     The simplest possible <acronym>SQL</acronym> function has no arguments and
 
180
     simply returns a base type, such as <type>integer</type>:
 
181
     
 
182
<screen>
 
183
CREATE FUNCTION one() RETURNS integer AS $$
 
184
    SELECT 1 AS result;
 
185
$$ LANGUAGE SQL;
 
186
 
 
187
-- Alternative syntax for string literal:
 
188
CREATE FUNCTION one() RETURNS integer AS '
 
189
    SELECT 1 AS result;
 
190
' LANGUAGE SQL;
 
191
 
 
192
SELECT one();
 
193
 
 
194
 one
 
195
-----
 
196
   1
 
197
</screen>
 
198
    </para>
 
199
 
 
200
    <para>
 
201
     Notice that we defined a column alias within the function body for the result of the function
 
202
     (with  the  name <literal>result</>),  but this column alias is not visible
 
203
     outside the function.  Hence,  the  result  is labeled <literal>one</>
 
204
     instead of <literal>result</>.
 
205
    </para>
 
206
 
 
207
    <para>
 
208
     It is almost as easy to define <acronym>SQL</acronym> functions  
 
209
     that take base types as arguments.  In the example below, notice
 
210
     how we refer to the arguments within the function as <literal>$1</>
 
211
     and <literal>$2</>.
 
212
 
 
213
<screen>
 
214
CREATE FUNCTION add_em(integer, integer) RETURNS integer AS $$
 
215
    SELECT $1 + $2;
 
216
$$ LANGUAGE SQL;
 
217
 
 
218
SELECT add_em(1, 2) AS answer;
 
219
 
 
220
 answer
 
221
--------
 
222
      3
 
223
</screen>
 
224
    </para>
 
225
 
 
226
    <para>
 
227
     Here is a more useful function, which might be used to debit a
 
228
     bank account:
 
229
 
 
230
<programlisting>
 
231
CREATE FUNCTION tf1 (integer, numeric) RETURNS integer AS $$
 
232
    UPDATE bank 
 
233
        SET balance = balance - $2
 
234
        WHERE accountno = $1;
 
235
    SELECT 1;
 
236
$$ LANGUAGE SQL;
 
237
</programlisting>
 
238
 
 
239
     A user could execute this function to debit account 17 by $100.00 as
 
240
     follows:
 
241
 
 
242
<programlisting>
 
243
SELECT tf1(17, 100.0);
 
244
</programlisting>
 
245
    </para>
 
246
 
 
247
    <para>
 
248
     In practice one would probably like a more useful result from the
 
249
     function than a constant 1, so a more likely definition
 
250
     is
 
251
 
 
252
<programlisting>
 
253
CREATE FUNCTION tf1 (integer, numeric) RETURNS numeric AS $$
 
254
    UPDATE bank 
 
255
        SET balance = balance - $2
 
256
        WHERE accountno = $1;
 
257
    SELECT balance FROM bank WHERE accountno = $1;
 
258
$$ LANGUAGE SQL;
 
259
</programlisting>
 
260
 
 
261
     which adjusts the balance and returns the new balance.
 
262
    </para>
 
263
   </sect2>
 
264
 
 
265
   <sect2>
 
266
    <title><acronym>SQL</acronym> Functions on Composite Types</title>
 
267
 
 
268
    <para>
 
269
     When writing  functions with arguments of composite
 
270
     types, we must  not  only  specify  which
 
271
     argument  we  want (as we did above with <literal>$1</> and <literal>$2</literal>) but
 
272
     also the desired attribute (field) of  that  argument.   For  example,
 
273
     suppose that 
 
274
     <type>emp</type> is a table containing employee data, and therefore
 
275
     also the name of the composite type of each row of the table.  Here
 
276
     is a function <function>double_salary</function> that computes what someone's
 
277
     salary would be if it were doubled:
 
278
 
 
279
<screen>
 
280
CREATE TABLE emp (
 
281
    name        text,
 
282
    salary      numeric,
 
283
    age         integer,
 
284
    cubicle     point
 
285
);
 
286
 
 
287
CREATE FUNCTION double_salary(emp) RETURNS numeric AS $$
 
288
    SELECT $1.salary * 2 AS salary;
 
289
$$ LANGUAGE SQL;
 
290
 
 
291
SELECT name, double_salary(emp.*) AS dream
 
292
    FROM emp
 
293
    WHERE emp.cubicle ~= point '(2,1)';
 
294
 
 
295
 name | dream
 
296
------+-------
 
297
 Bill |  8400
 
298
</screen>
 
299
    </para>
 
300
 
 
301
    <para>
 
302
     Notice the use of the syntax <literal>$1.salary</literal>
 
303
     to select one field of the argument row value.  Also notice
 
304
     how the calling <command>SELECT</> command uses <literal>*</>
 
305
     to select
 
306
     the entire current row of a table as a composite value.  The table
 
307
     row can alternatively be referenced using just the table name,
 
308
     like this:
 
309
<screen>
 
310
SELECT name, double_salary(emp) AS dream
 
311
    FROM emp
 
312
    WHERE emp.cubicle ~= point '(2,1)';
 
313
</screen>
 
314
     but this usage is deprecated since it's easy to get confused.
 
315
    </para>
 
316
 
 
317
    <para>
 
318
     Sometimes it is handy to construct a composite argument value
 
319
     on-the-fly.  This can be done with the <literal>ROW</> construct.
 
320
     For example, we could adjust the data being passed to the function:
 
321
<screen>
 
322
SELECT name, double_salary(ROW(name, salary*1.1, age, cubicle)) AS dream
 
323
    FROM emp;
 
324
</screen>
 
325
    </para>
 
326
 
 
327
    <para>
 
328
     It is also possible to build a function that returns a composite type.
 
329
     This is an example of a function 
 
330
     that returns a single <type>emp</type> row:
 
331
 
 
332
<programlisting>
 
333
CREATE FUNCTION new_emp() RETURNS emp AS $$
 
334
    SELECT text 'None' AS name,
 
335
        1000.0 AS salary,
 
336
        25 AS age,
 
337
        point '(2,2)' AS cubicle;
 
338
$$ LANGUAGE SQL;
 
339
</programlisting>
 
340
 
 
341
     In this example we have specified each of  the  attributes
 
342
     with  a  constant value, but any computation
 
343
     could have been substituted for these constants.
 
344
    </para>
 
345
 
 
346
    <para>
 
347
     Note two important things about defining the function:
 
348
 
 
349
     <itemizedlist>
 
350
      <listitem>
 
351
       <para>
 
352
        The select list order in the query must be exactly the same as
 
353
        that in which the columns appear in the table associated
 
354
        with the composite type.  (Naming the columns, as we did above,
 
355
        is irrelevant to the system.)
 
356
       </para>
 
357
      </listitem>
 
358
      <listitem>
 
359
       <para>
 
360
        You must typecast the expressions to match the
 
361
        definition of the composite type, or you will get errors like this:
 
362
<screen>
 
363
<computeroutput>
 
364
ERROR:  function declared to return emp returns varchar instead of text at column 1
 
365
</computeroutput>
 
366
</screen>
 
367
       </para>
 
368
      </listitem>
 
369
     </itemizedlist>
 
370
    </para>     
 
371
 
 
372
    <para>
 
373
     A different way to define the same function is:
 
374
 
 
375
<programlisting>
 
376
CREATE FUNCTION new_emp() RETURNS emp AS $$
 
377
    SELECT ROW('None', 1000.0, 25, '(2,2)')::emp;
 
378
$$ LANGUAGE SQL;
 
379
</programlisting>
 
380
 
 
381
     Here we wrote a <command>SELECT</> that returns just a single
 
382
     column of the correct composite type.  This isn't really better
 
383
     in this situation, but it is a handy alternative in some cases
 
384
     &mdash; for example, if we need to compute the result by calling
 
385
     another function that returns the desired composite value.
 
386
    </para>     
 
387
 
 
388
    <para>
 
389
     We could call this function directly in either of two ways:
 
390
 
 
391
<screen>
 
392
SELECT new_emp();
 
393
 
 
394
         new_emp
 
395
--------------------------
 
396
 (None,1000.0,25,"(2,2)")
 
397
 
 
398
SELECT * FROM new_emp();
 
399
 
 
400
 name | salary | age | cubicle
 
401
------+--------+-----+---------
 
402
 None | 1000.0 |  25 | (2,2)
 
403
</screen>
 
404
 
 
405
     The second way is described more fully in <xref
 
406
     linkend="xfunc-sql-table-functions">.
 
407
    </para>     
 
408
 
 
409
    <para>
 
410
     When you use a function that returns a composite type,
 
411
     you might want only one field (attribute) from its result.
 
412
     You can do that with syntax like this:
 
413
 
 
414
<screen>
 
415
SELECT (new_emp()).name;
 
416
 
 
417
 name
 
418
------
 
419
 None
 
420
</screen>
 
421
 
 
422
     The extra parentheses are needed to keep the parser from getting
 
423
     confused.  If you try to do it without them, you get something like this:
 
424
 
 
425
<screen>
 
426
SELECT new_emp().name;
 
427
ERROR:  syntax error at or near "." at character 17
 
428
LINE 1: SELECT new_emp().name;
 
429
                        ^
 
430
</screen>
 
431
    </para>
 
432
 
 
433
    <para>
 
434
     Another option is to use
 
435
     functional notation for extracting an attribute.  The  simple  way 
 
436
     to explain this is that we can use the
 
437
     notations <literal>attribute(table)</>  and  <literal>table.attribute</>
 
438
     interchangeably.
 
439
 
 
440
<screen>
 
441
SELECT name(new_emp());
 
442
 
 
443
 name
 
444
------
 
445
 None
 
446
</screen>
 
447
 
 
448
<screen>
 
449
-- This is the same as:
 
450
-- SELECT emp.name AS youngster FROM emp WHERE emp.age &lt; 30;
 
451
 
 
452
SELECT name(emp) AS youngster FROM emp WHERE age(emp) &lt; 30;
 
453
 
 
454
 youngster
 
455
-----------
 
456
 Sam
 
457
 Andy
 
458
</screen>
 
459
    </para>
 
460
 
 
461
    <tip>
 
462
     <para>
 
463
      The equivalence between functional notation and attribute notation
 
464
      makes it possible to use functions on composite types to emulate
 
465
      <quote>computed fields</>.
 
466
      <indexterm>
 
467
       <primary>computed field</primary>
 
468
      </indexterm>
 
469
      <indexterm>
 
470
       <primary>field</primary>
 
471
       <secondary>computed</secondary>
 
472
      </indexterm>
 
473
      For example, using the previous definition
 
474
      for <literal>double_salary(emp)</>, we can write
 
475
 
 
476
<screen>
 
477
SELECT emp.name, emp.double_salary FROM emp;
 
478
</screen>
 
479
 
 
480
      An application using this wouldn't need to be directly aware that
 
481
      <literal>double_salary</> isn't a real column of the table.
 
482
      (You can also emulate computed fields with views.)
 
483
     </para>
 
484
    </tip>
 
485
 
 
486
    <para>
 
487
     Another way to use a function returning a row result is to pass the
 
488
     result to another function that accepts the correct row type as input:
 
489
 
 
490
<screen>
 
491
CREATE FUNCTION getname(emp) RETURNS text AS $$
 
492
    SELECT $1.name;
 
493
$$ LANGUAGE SQL;
 
494
 
 
495
SELECT getname(new_emp());
 
496
 getname
 
497
---------
 
498
 None
 
499
(1 row)
 
500
</screen>
 
501
    </para>     
 
502
 
 
503
    <para>
 
504
     Another way to use a function that returns a composite type is to
 
505
     call it as a table function, as described below.
 
506
    </para>
 
507
   </sect2>
 
508
 
 
509
   <sect2 id="xfunc-sql-table-functions">
 
510
    <title><acronym>SQL</acronym> Functions as Table Sources</title>
 
511
 
 
512
    <para>
 
513
     All SQL functions may be used in the <literal>FROM</> clause of a query,
 
514
     but it is particularly useful for functions returning composite types.
 
515
     If the function is defined to return a base type, the table function
 
516
     produces a one-column table.  If the function is defined to return
 
517
     a composite type, the table function produces a column for each attribute
 
518
     of the composite type.
 
519
    </para>
 
520
 
 
521
    <para>
 
522
     Here is an example:
 
523
 
 
524
<screen>
 
525
CREATE TABLE foo (fooid int, foosubid int, fooname text);
 
526
INSERT INTO foo VALUES (1, 1, 'Joe');
 
527
INSERT INTO foo VALUES (1, 2, 'Ed');
 
528
INSERT INTO foo VALUES (2, 1, 'Mary');
 
529
 
 
530
CREATE FUNCTION getfoo(int) RETURNS foo AS $$
 
531
    SELECT * FROM foo WHERE fooid = $1;
 
532
$$ LANGUAGE SQL;
 
533
 
 
534
SELECT *, upper(fooname) FROM getfoo(1) AS t1;
 
535
 
 
536
 fooid | foosubid | fooname | upper
 
537
-------+----------+---------+-------
 
538
     1 |        1 | Joe     | JOE
 
539
(2 rows)
 
540
</screen>
 
541
 
 
542
     As the example shows, we can work with the columns of the function's
 
543
     result just the same as if they were columns of a regular table.
 
544
    </para>
 
545
 
 
546
    <para>
 
547
     Note that we only got one row out of the function.  This is because
 
548
     we did not use <literal>SETOF</>.  That is described in the next section.
 
549
    </para>
 
550
   </sect2>
 
551
 
 
552
   <sect2>
 
553
    <title><acronym>SQL</acronym> Functions Returning Sets</title>
 
554
 
 
555
    <para>
 
556
     When an SQL function is declared as returning <literal>SETOF
 
557
     <replaceable>sometype</></literal>, the function's final
 
558
     <command>SELECT</> query is executed to completion, and each row it
 
559
     outputs is returned as an element of the result set.
 
560
    </para>
 
561
 
 
562
    <para>
 
563
     This feature is normally used when calling the function in the <literal>FROM</>
 
564
     clause.  In this case each row returned by the function becomes
 
565
     a row of the table seen by the query.  For example, assume that
 
566
     table <literal>foo</> has the same contents as above, and we say:
 
567
 
 
568
<programlisting>
 
569
CREATE FUNCTION getfoo(int) RETURNS SETOF foo AS $$
 
570
    SELECT * FROM foo WHERE fooid = $1;
 
571
$$ LANGUAGE SQL;
 
572
 
 
573
SELECT * FROM getfoo(1) AS t1;
 
574
</programlisting>
 
575
 
 
576
     Then we would get:
 
577
<screen>
 
578
 fooid | foosubid | fooname
 
579
-------+----------+---------
 
580
     1 |        1 | Joe
 
581
     1 |        2 | Ed
 
582
(2 rows)
 
583
</screen>
 
584
    </para>
 
585
 
 
586
    <para>
 
587
     Currently, functions returning sets may also be called in the select list
 
588
     of a query.  For each row that the query
 
589
     generates by itself, the function returning set is invoked, and an output
 
590
     row is generated for each element of the function's result set. Note,
 
591
     however, that this capability is deprecated and may be removed in future
 
592
     releases. The following is an example function returning a set from the
 
593
     select list:
 
594
 
 
595
<screen>
 
596
CREATE FUNCTION listchildren(text) RETURNS SETOF text AS $$
 
597
    SELECT name FROM nodes WHERE parent = $1
 
598
$$ LANGUAGE SQL;
 
599
 
 
600
SELECT * FROM nodes;
 
601
   name    | parent
 
602
-----------+--------
 
603
 Top       |
 
604
 Child1    | Top
 
605
 Child2    | Top
 
606
 Child3    | Top
 
607
 SubChild1 | Child1
 
608
 SubChild2 | Child1
 
609
(6 rows)
 
610
 
 
611
SELECT listchildren('Top');
 
612
 listchildren
 
613
--------------
 
614
 Child1
 
615
 Child2
 
616
 Child3
 
617
(3 rows)
 
618
 
 
619
SELECT name, listchildren(name) FROM nodes;
 
620
  name  | listchildren
 
621
--------+--------------
 
622
 Top    | Child1
 
623
 Top    | Child2
 
624
 Top    | Child3
 
625
 Child1 | SubChild1
 
626
 Child1 | SubChild2
 
627
(5 rows)
 
628
</screen>
 
629
 
 
630
     In the last <command>SELECT</command>,
 
631
     notice that no output row appears for <literal>Child2</>, <literal>Child3</>, etc.
 
632
     This happens because <function>listchildren</function> returns an empty set
 
633
     for those arguments, so no result rows are generated.
 
634
    </para>
 
635
   </sect2>
 
636
 
 
637
   <sect2>
 
638
    <title>Polymorphic <acronym>SQL</acronym> Functions</title>
 
639
 
 
640
    <para>
 
641
     <acronym>SQL</acronym> functions may be declared to accept and
 
642
     return the polymorphic types <type>anyelement</type> and
 
643
     <type>anyarray</type>.  See <xref
 
644
     linkend="extend-types-polymorphic"> for a more detailed
 
645
     explanation of polymorphic functions. Here is a polymorphic
 
646
     function <function>make_array</function> that builds up an array
 
647
     from two arbitrary data type elements:
 
648
<screen>
 
649
CREATE FUNCTION make_array(anyelement, anyelement) RETURNS anyarray AS $$
 
650
    SELECT ARRAY[$1, $2];
 
651
$$ LANGUAGE SQL;
 
652
 
 
653
SELECT make_array(1, 2) AS intarray, make_array('a'::text, 'b') AS textarray;
 
654
 intarray | textarray
 
655
----------+-----------
 
656
 {1,2}    | {a,b}
 
657
(1 row)
 
658
</screen>
 
659
    </para>
 
660
 
 
661
    <para>
 
662
     Notice the use of the typecast <literal>'a'::text</literal>
 
663
     to specify that the argument is of type <type>text</type>. This is
 
664
     required if the argument is just a string literal, since otherwise
 
665
     it would be treated as type
 
666
     <type>unknown</type>, and array of <type>unknown</type> is not a valid
 
667
     type.
 
668
     Without the typecast, you will get errors like this:
 
669
<screen>
 
670
<computeroutput>
 
671
ERROR:  could not determine "anyarray"/"anyelement" type because input has type "unknown"
 
672
</computeroutput>
 
673
</screen>
 
674
    </para>
 
675
 
 
676
    <para>
 
677
     It is permitted to have polymorphic arguments with a fixed
 
678
     return type, but the converse is not. For example:
 
679
<screen>
 
680
CREATE FUNCTION is_greater(anyelement, anyelement) RETURNS boolean AS $$
 
681
    SELECT $1 &gt; $2;
 
682
$$ LANGUAGE SQL;
 
683
 
 
684
SELECT is_greater(1, 2);
 
685
 is_greater
 
686
------------
 
687
 f
 
688
(1 row)
 
689
 
 
690
CREATE FUNCTION invalid_func() RETURNS anyelement AS $$
 
691
    SELECT 1;
 
692
$$ LANGUAGE SQL;
 
693
ERROR:  cannot determine result data type
 
694
DETAIL:  A function returning "anyarray" or "anyelement" must have at least one argument of either type.
 
695
</screen>
 
696
    </para>
 
697
   </sect2>
 
698
  </sect1>
 
699
 
 
700
  <sect1 id="xfunc-overload">
 
701
   <title>Function Overloading</title>
 
702
 
 
703
   <indexterm zone="xfunc-overload">
 
704
    <primary>overloading</primary>
 
705
    <secondary>functions</secondary>
 
706
   </indexterm>
 
707
 
 
708
   <para>
 
709
    More than one function may be defined with the same SQL name, so long
 
710
    as the arguments they take are different.  In other words,
 
711
    function names can be <firstterm>overloaded</firstterm>.  When a
 
712
    query is executed, the server will determine which function to
 
713
    call from the data types and the number of the provided arguments.
 
714
    Overloading can also be used to simulate functions with a variable
 
715
    number of arguments, up to a finite maximum number.
 
716
   </para>
 
717
 
 
718
   <para>
 
719
    When creating a family of overloaded functions, one should be
 
720
    careful not to create ambiguities.  For instance, given the
 
721
    functions
 
722
<programlisting>
 
723
CREATE FUNCTION test(int, real) RETURNS ...
 
724
CREATE FUNCTION test(smallint, double precision) RETURNS ...
 
725
</programlisting>
 
726
    it is not immediately clear which function would be called with
 
727
    some trivial input like <literal>test(1, 1.5)</literal>.  The
 
728
    currently implemented resolution rules are described in
 
729
    <xref linkend="typeconv">, but it is unwise to design a system that subtly
 
730
    relies on this behavior.
 
731
   </para>
 
732
 
 
733
   <para>
 
734
    A function that takes a single argument of a composite type should
 
735
    generally not have the same name as any attribute (field) of that type.
 
736
    Recall that <literal>attribute(table)</literal> is considered equivalent
 
737
    to <literal>table.attribute</literal>.  In the case that there is an
 
738
    ambiguity between a function on a composite type and an attribute of
 
739
    the composite type, the attribute will always be used.  It is possible
 
740
    to override that choice by schema-qualifying the function name
 
741
    (that is, <literal>schema.func(table)</literal>) but it's better to
 
742
    avoid the problem by not choosing conflicting names.
 
743
   </para>
 
744
 
 
745
   <para>
 
746
    When overloading C-language functions, there is an additional
 
747
    constraint: The C name of each function in the family of
 
748
    overloaded functions must be different from the C names of all
 
749
    other functions, either internal or dynamically loaded.  If this
 
750
    rule is violated, the behavior is not portable.  You might get a
 
751
    run-time linker error, or one of the functions will get called
 
752
    (usually the internal one).  The alternative form of the
 
753
    <literal>AS</> clause for the SQL <command>CREATE
 
754
    FUNCTION</command> command decouples the SQL function name from
 
755
    the function name in the C source code.  For instance,
 
756
<programlisting>
 
757
CREATE FUNCTION test(int) RETURNS int
 
758
    AS '<replaceable>filename</>', 'test_1arg'
 
759
    LANGUAGE C;
 
760
CREATE FUNCTION test(int, int) RETURNS int
 
761
    AS '<replaceable>filename</>', 'test_2arg'
 
762
    LANGUAGE C;
 
763
</programlisting>
 
764
    The names of the C functions here reflect one of many possible conventions.
 
765
   </para>
 
766
  </sect1>
 
767
 
 
768
  <sect1 id="xfunc-volatility">
 
769
   <title>Function Volatility Categories</title>
 
770
 
 
771
   <indexterm zone="xfunc-volatility">
 
772
    <primary>volatility</primary>
 
773
    <secondary>functions</secondary>
 
774
   </indexterm>
 
775
   <indexterm zone="xfunc-volatility">
 
776
    <primary>VOLATILE</primary>
 
777
   </indexterm>
 
778
   <indexterm zone="xfunc-volatility">
 
779
    <primary>STABLE</primary>
 
780
   </indexterm>
 
781
   <indexterm zone="xfunc-volatility">
 
782
    <primary>IMMUTABLE</primary>
 
783
   </indexterm>
 
784
 
 
785
   <para>
 
786
    Every function has a <firstterm>volatility</> classification, with
 
787
    the possibilities being <literal>VOLATILE</>, <literal>STABLE</>, or
 
788
    <literal>IMMUTABLE</>.  <literal>VOLATILE</> is the default if the
 
789
    <command>CREATE FUNCTION</command> command does not specify a category.
 
790
    The volatility category is a promise to the optimizer about the behavior
 
791
    of the function:
 
792
 
 
793
   <itemizedlist>
 
794
    <listitem>
 
795
     <para>
 
796
      A <literal>VOLATILE</> function can do anything, including modifying
 
797
      the database.  It can return different results on successive calls with
 
798
      the same arguments.  The optimizer makes no assumptions about the
 
799
      behavior of such functions.  A query using a volatile function will
 
800
      re-evaluate the function at every row where its value is needed.
 
801
     </para>
 
802
    </listitem>
 
803
    <listitem>
 
804
     <para>
 
805
      A <literal>STABLE</> function cannot modify the database and is
 
806
      guaranteed to return the same results given the same arguments
 
807
      for all calls within a single surrounding query.  This category
 
808
      allows the optimizer to optimize away multiple calls of the function
 
809
      within a single query.  In particular, it is safe to use an expression
 
810
      containing such a function in an index scan condition.  (Since an
 
811
      index scan will evaluate the comparison value only once, not once at
 
812
      each row, it is not valid to use a <literal>VOLATILE</> function in
 
813
      an index scan condition.)
 
814
     </para>
 
815
    </listitem>
 
816
    <listitem>
 
817
     <para>
 
818
      An <literal>IMMUTABLE</> function cannot modify the database and is
 
819
      guaranteed to return the same results given the same arguments forever.
 
820
      This category allows the optimizer to pre-evaluate the function when
 
821
      a query calls it with constant arguments.  For example, a query like
 
822
      <literal>SELECT ... WHERE x = 2 + 2</> can be simplified on sight to
 
823
      <literal>SELECT ... WHERE x = 4</>, because the function underlying
 
824
      the integer addition operator is marked <literal>IMMUTABLE</>.
 
825
     </para>
 
826
    </listitem>
 
827
   </itemizedlist>
 
828
   </para>
 
829
 
 
830
   <para>
 
831
    For best optimization results, you should label your functions with the
 
832
    strictest volatility category that is valid for them.
 
833
   </para>
 
834
 
 
835
   <para>
 
836
    Any function with side-effects <emphasis>must</> be labeled
 
837
    <literal>VOLATILE</>, so that calls to it cannot be optimized away.
 
838
    Even a function with no side-effects needs to be labeled
 
839
    <literal>VOLATILE</> if its value can change within a single query;
 
840
    some examples are <literal>random()</>, <literal>currval()</>,
 
841
    <literal>timeofday()</>.
 
842
   </para>
 
843
 
 
844
   <para>
 
845
    There is relatively little difference between <literal>STABLE</> and
 
846
    <literal>IMMUTABLE</> categories when considering simple interactive
 
847
    queries that are planned and immediately executed: it doesn't matter
 
848
    a lot whether a function is executed once during planning or once during
 
849
    query execution startup.  But there is a big difference if the plan is
 
850
    saved and reused later.  Labeling a function <literal>IMMUTABLE</> when
 
851
    it really isn't may allow it to be prematurely folded to a constant during
 
852
    planning, resulting in a stale value being re-used during subsequent uses
 
853
    of the plan.  This is a hazard when using prepared statements or when
 
854
    using function languages that cache plans (such as
 
855
    <application>PL/pgSQL</>).
 
856
   </para>
 
857
 
 
858
   <para>
 
859
    Because of the snapshotting behavior of MVCC (see <xref linkend="mvcc">)
 
860
    a function containing only <command>SELECT</> commands can safely be
 
861
    marked <literal>STABLE</>, even if it selects from tables that might be
 
862
    undergoing modifications by concurrent queries.
 
863
    <productname>PostgreSQL</productname> will execute a <literal>STABLE</>
 
864
    function using the snapshot established for the calling query, and so it
 
865
    will see a fixed view of the database throughout that query.
 
866
    Also note
 
867
    that the <function>current_timestamp</> family of functions qualify
 
868
    as stable, since their values do not change within a transaction.
 
869
   </para>
 
870
 
 
871
   <para>
 
872
    The same snapshotting behavior is used for <command>SELECT</> commands
 
873
    within <literal>IMMUTABLE</> functions.  It is generally unwise to select
 
874
    from database tables within an <literal>IMMUTABLE</> function at all,
 
875
    since the immutability will be broken if the table contents ever change.
 
876
    However, <productname>PostgreSQL</productname> does not enforce that you
 
877
    do not do that.
 
878
   </para>
 
879
 
 
880
   <para>
 
881
    A common error is to label a function <literal>IMMUTABLE</> when its
 
882
    results depend on a configuration parameter.  For example, a function
 
883
    that manipulates timestamps might well have results that depend on the
 
884
    <xref linkend="guc-timezone"> setting.  For safety, such functions should
 
885
    be labeled <literal>STABLE</> instead.
 
886
   </para>
 
887
 
 
888
   <note>
 
889
    <para>
 
890
     Before <productname>PostgreSQL</productname> release 8.0, the requirement
 
891
     that <literal>STABLE</> and <literal>IMMUTABLE</> functions cannot modify
 
892
     the database was not enforced by the system.  Release 8.0 enforces it
 
893
     by requiring SQL functions and procedural language functions of these
 
894
     categories to contain no SQL commands other than <command>SELECT</>.
 
895
     (This is not a completely bulletproof test, since such functions could
 
896
     still call <literal>VOLATILE</> functions that modify the database.
 
897
     If you do that, you will find that the <literal>STABLE</> or
 
898
     <literal>IMMUTABLE</> function does not notice the database changes
 
899
     applied by the called function.)
 
900
    </para>
 
901
   </note>
 
902
  </sect1>
 
903
 
 
904
  <sect1 id="xfunc-pl">
 
905
   <title>Procedural Language Functions</title>
 
906
 
 
907
   <para>
 
908
    <productname>PostgreSQL</productname> allows user-defined functions
 
909
    to be written in other languages besides SQL and C.  These other
 
910
    languages are generically called <firstterm>procedural
 
911
    languages</firstterm> (<acronym>PL</>s).
 
912
    Procedural languages aren't built into the
 
913
    <productname>PostgreSQL</productname> server; they are offered
 
914
    by loadable modules.
 
915
    See <xref linkend="xplang"> and following chapters for more
 
916
    information.
 
917
   </para>
 
918
  </sect1>
 
919
 
 
920
  <sect1 id="xfunc-internal">
 
921
   <title>Internal Functions</title>
 
922
 
 
923
   <indexterm zone="xfunc-internal"><primary>function</><secondary>internal</></>
 
924
 
 
925
   <para>
 
926
    Internal functions are functions written in C that have been statically
 
927
    linked into the <productname>PostgreSQL</productname> server.
 
928
    The <quote>body</quote> of the function definition
 
929
    specifies the C-language name of the function, which need not be the
 
930
    same as the name being declared for SQL use.
 
931
    (For reasons of backwards compatibility, an empty body
 
932
    is accepted as meaning that the C-language function name is the
 
933
    same as the SQL name.)
 
934
   </para>
 
935
 
 
936
   <para>
 
937
    Normally, all internal functions present in the
 
938
    server are declared during the initialization of the database cluster (<command>initdb</command>),
 
939
    but a user could use <command>CREATE FUNCTION</command>
 
940
    to create additional alias names for an internal function.
 
941
    Internal functions are declared in <command>CREATE FUNCTION</command>
 
942
    with language name <literal>internal</literal>.  For instance, to
 
943
    create an alias for the <function>sqrt</function> function:
 
944
<programlisting>
 
945
CREATE FUNCTION square_root(double precision) RETURNS double precision
 
946
    AS 'dsqrt'
 
947
    LANGUAGE internal
 
948
    STRICT;
 
949
</programlisting>
 
950
    (Most internal functions expect to be declared <quote>strict</quote>.)
 
951
   </para>
 
952
 
 
953
   <note>
 
954
    <para>
 
955
     Not all <quote>predefined</quote> functions are
 
956
     <quote>internal</quote> in the above sense.  Some predefined
 
957
     functions are written in SQL.
 
958
    </para>
 
959
   </note>
 
960
  </sect1>
 
961
 
 
962
  <sect1 id="xfunc-c">
 
963
   <title>C-Language Functions</title>
 
964
 
 
965
   <indexterm zone="xfunc-sql">
 
966
    <primary>function</primary>
 
967
    <secondary>user-defined</secondary>
 
968
    <tertiary>in C</tertiary>
 
969
   </indexterm>
 
970
 
 
971
   <para>
 
972
    User-defined functions can be written in C (or a language that can
 
973
    be made compatible with C, such as C++).  Such functions are
 
974
    compiled into dynamically loadable objects (also called shared
 
975
    libraries) and are loaded by the server on demand.  The dynamic
 
976
    loading feature is what distinguishes <quote>C language</> functions
 
977
    from <quote>internal</> functions &mdash; the actual coding conventions
 
978
    are essentially the same for both.  (Hence, the standard internal
 
979
    function library is a rich source of coding examples for user-defined
 
980
    C functions.)
 
981
   </para>
 
982
 
 
983
   <para>
 
984
    Two different calling conventions are currently used for C functions.
 
985
    The newer <quote>version 1</quote> calling convention is indicated by writing
 
986
    a <literal>PG_FUNCTION_INFO_V1()</literal> macro call for the function,
 
987
    as illustrated below.  Lack of such a macro indicates an old-style
 
988
    (<quote>version 0</quote>) function.  The language name specified in <command>CREATE FUNCTION</command>
 
989
    is <literal>C</literal> in either case.  Old-style functions are now deprecated
 
990
    because of portability problems and lack of functionality, but they
 
991
    are still supported for compatibility reasons.
 
992
   </para>
 
993
 
 
994
  <sect2 id="xfunc-c-dynload">
 
995
   <title>Dynamic Loading</title>
 
996
 
 
997
   <indexterm zone="xfunc-c-dynload">
 
998
    <primary>dynamic loading</primary>
 
999
   </indexterm>
 
1000
 
 
1001
   <para>
 
1002
    The first time a user-defined function in a particular
 
1003
    loadable object file is called in a session,
 
1004
    the dynamic loader loads that object file into memory so that the
 
1005
    function can be called.  The <command>CREATE FUNCTION</command>
 
1006
    for a user-defined C function must therefore specify two pieces of
 
1007
    information for the function: the name of the loadable
 
1008
    object file, and the C name (link symbol) of the specific function to call
 
1009
    within that object file.  If the C name is not explicitly specified then
 
1010
    it is assumed to be the same as the SQL function name.
 
1011
   </para>
 
1012
 
 
1013
   <para>
 
1014
    The following algorithm is used to locate the shared object file
 
1015
    based on the name given in the <command>CREATE FUNCTION</command>
 
1016
    command:
 
1017
 
 
1018
    <orderedlist>
 
1019
     <listitem>
 
1020
      <para>
 
1021
       If the name is an absolute path, the given file is loaded.
 
1022
      </para>
 
1023
     </listitem>
 
1024
 
 
1025
     <listitem>
 
1026
      <para>
 
1027
       If the name starts with the string <literal>$libdir</literal>,
 
1028
       that part is replaced by the <productname>PostgreSQL</> package
 
1029
        library directory
 
1030
       name, which is determined at build time.<indexterm><primary>$libdir</></>
 
1031
      </para>
 
1032
     </listitem>
 
1033
 
 
1034
     <listitem>
 
1035
      <para>
 
1036
       If the name does not contain a directory part, the file is
 
1037
       searched for in the path specified by the configuration variable
 
1038
       <xref linkend="guc-dynamic-library-path">.<indexterm><primary>dynamic_library_path</></>
 
1039
      </para>
 
1040
     </listitem>
 
1041
 
 
1042
     <listitem>
 
1043
      <para>
 
1044
       Otherwise (the file was not found in the path, or it contains a
 
1045
       non-absolute directory part), the dynamic loader will try to
 
1046
       take the name as given, which will most likely fail.  (It is
 
1047
       unreliable to depend on the current working directory.)
 
1048
      </para>
 
1049
     </listitem>
 
1050
    </orderedlist>
 
1051
 
 
1052
    If this sequence does not work, the platform-specific shared
 
1053
    library file name extension (often <filename>.so</filename>) is
 
1054
    appended to the given name and this sequence is tried again.  If
 
1055
    that fails as well, the load will fail.
 
1056
   </para>
 
1057
 
 
1058
   <para>
 
1059
    The user ID the <productname>PostgreSQL</productname> server runs
 
1060
    as must be able to traverse the path to the file you intend to
 
1061
    load.  Making the file or a higher-level directory not readable
 
1062
    and/or not executable by the <systemitem>postgres</systemitem>
 
1063
    user is a common mistake.
 
1064
   </para>
 
1065
 
 
1066
   <para>
 
1067
    In any case, the file name that is given in the
 
1068
    <command>CREATE FUNCTION</command> command is recorded literally
 
1069
    in the system catalogs, so if the file needs to be loaded again
 
1070
    the same procedure is applied.
 
1071
   </para>
 
1072
 
 
1073
   <note>
 
1074
    <para>
 
1075
     <productname>PostgreSQL</productname> will not compile a C function
 
1076
     automatically.  The object file must be compiled before it is referenced
 
1077
     in a <command>CREATE
 
1078
     FUNCTION</> command.  See <xref linkend="dfunc"> for additional
 
1079
     information.
 
1080
    </para>
 
1081
   </note>
 
1082
 
 
1083
   <para>
 
1084
    After it is used for the first time, a dynamically loaded object
 
1085
    file is retained in memory.  Future calls in the same session to
 
1086
    the function(s) in that file will only incur the small overhead of
 
1087
    a symbol table lookup.  If you need to force a reload of an object
 
1088
    file, for example after recompiling it, use the <command>LOAD</>
 
1089
    command or begin a fresh session.
 
1090
   </para>
 
1091
 
 
1092
   <para>
 
1093
    It is recommended to locate shared libraries either relative to
 
1094
    <literal>$libdir</literal> or through the dynamic library path.
 
1095
    This simplifies version upgrades if the new installation is at a
 
1096
    different location.  The actual directory that
 
1097
    <literal>$libdir</literal> stands for can be found out with the
 
1098
    command <literal>pg_config --pkglibdir</literal>.
 
1099
   </para>
 
1100
 
 
1101
   <para>
 
1102
    Before <productname>PostgreSQL</productname> release 7.2, only
 
1103
    exact absolute paths to object files could be specified in
 
1104
    <command>CREATE FUNCTION</>.  This approach is now deprecated
 
1105
    since it makes the function definition unnecessarily unportable.
 
1106
    It's best to specify just the shared library name with no path nor
 
1107
    extension, and let the search mechanism provide that information
 
1108
    instead.
 
1109
   </para>
 
1110
  </sect2>
 
1111
 
 
1112
   <sect2 id="xfunc-c-basetype">
 
1113
    <title>Base Types in C-Language Functions</title>
 
1114
 
 
1115
    <indexterm zone="xfunc-c-basetype">
 
1116
     <primary>data type</primary>
 
1117
     <secondary>internal organisation</secondary>
 
1118
    </indexterm>
 
1119
 
 
1120
    <para>
 
1121
     To know how to write C-language functions, you need to know how
 
1122
     <productname>PostgreSQL</productname> internally represents base
 
1123
     data types and how they can be passed to and from functions.
 
1124
     Internally, <productname>PostgreSQL</productname> regards a base
 
1125
     type as a <quote>blob of memory</quote>.  The user-defined
 
1126
     functions that you define over a type in turn define the way that
 
1127
     <productname>PostgreSQL</productname> can operate on it.  That
 
1128
     is, <productname>PostgreSQL</productname> will only store and
 
1129
     retrieve the data from disk and use your user-defined functions
 
1130
     to input, process, and output the data.
 
1131
    </para>
 
1132
 
 
1133
    <para>
 
1134
     Base types can have one of three internal formats:
 
1135
 
 
1136
     <itemizedlist>
 
1137
      <listitem>
 
1138
       <para>
 
1139
        pass by value, fixed-length
 
1140
       </para>
 
1141
      </listitem>
 
1142
      <listitem>
 
1143
       <para>
 
1144
        pass by reference, fixed-length
 
1145
       </para>
 
1146
      </listitem>
 
1147
      <listitem>
 
1148
       <para>
 
1149
        pass by reference, variable-length
 
1150
       </para>
 
1151
      </listitem>
 
1152
     </itemizedlist>
 
1153
    </para>
 
1154
 
 
1155
    <para>
 
1156
     By-value  types  can  only be 1, 2, or 4 bytes in length
 
1157
     (also 8 bytes, if <literal>sizeof(Datum)</literal> is 8 on your machine).
 
1158
     You should be careful 
 
1159
     to define your types such that  they  will  be  the  same  
 
1160
     size (in bytes) on all architectures.  For example, the 
 
1161
     <literal>long</literal> type is dangerous because  it  
 
1162
     is 4 bytes on some machines and 8 bytes on others, whereas 
 
1163
     <type>int</type>  type  is  4  bytes  on  most  
 
1164
     Unix machines.  A reasonable implementation of  
 
1165
     the  <type>int4</type>  type  on  Unix
 
1166
     machines might be:
 
1167
     
 
1168
<programlisting>
 
1169
/* 4-byte integer, passed by value */
 
1170
typedef int int4;
 
1171
</programlisting>
 
1172
    </para>
 
1173
 
 
1174
    <para>
 
1175
     On  the  other hand, fixed-length types of any size may
 
1176
     be passed by-reference.  For example, here is a  sample
 
1177
     implementation of a <productname>PostgreSQL</productname> type:
 
1178
     
 
1179
<programlisting>
 
1180
/* 16-byte structure, passed by reference */
 
1181
typedef struct
 
1182
{
 
1183
    double  x, y;
 
1184
} Point;
 
1185
</programlisting>
 
1186
 
 
1187
     Only  pointers  to  such types can be used when passing
 
1188
     them in and out of <productname>PostgreSQL</productname> functions.
 
1189
     To return a value of such a type, allocate the right amount of
 
1190
     memory with <literal>palloc</literal>, fill in the allocated memory,
 
1191
     and return a pointer to it.  (You can also return an input value
 
1192
     that has the same type as the return value directly by returning
 
1193
     the pointer to the input value.  <emphasis>Never</> modify the
 
1194
     contents of a pass-by-reference input value, however.)
 
1195
    </para>
 
1196
 
 
1197
    <para>
 
1198
     Finally, all variable-length types must also be  passed
 
1199
     by  reference.   All  variable-length  types must begin
 
1200
     with a length field of exactly 4 bytes, and all data to
 
1201
     be  stored within that type must be located in the memory 
 
1202
     immediately  following  that  length  field.   The
 
1203
     length field contains the total length of the structure,
 
1204
     that is,  it  includes  the  size  of  the  length  field
 
1205
     itself.
 
1206
    </para>
 
1207
 
 
1208
    <para>
 
1209
     As an example, we can define the type <type>text</type> as
 
1210
     follows:
 
1211
 
 
1212
<programlisting>
 
1213
typedef struct {
 
1214
    int4 length;
 
1215
    char data[1];
 
1216
} text;
 
1217
</programlisting>
 
1218
 
 
1219
     Obviously,  the  data  field declared here is not long enough to hold
 
1220
     all possible strings.  Since it's impossible to declare a variable-size
 
1221
     structure in <acronym>C</acronym>, we rely on the knowledge that the
 
1222
     <acronym>C</acronym> compiler won't range-check array subscripts.  We
 
1223
     just allocate the necessary amount of space and then access the array as
 
1224
     if it were declared the right length.  (This is a common trick, which
 
1225
     you can read about in many textbooks about C.)
 
1226
    </para>
 
1227
 
 
1228
    <para>
 
1229
     When manipulating 
 
1230
     variable-length types, we must  be  careful  to  allocate  
 
1231
     the  correct amount  of memory and set the length field correctly.
 
1232
     For example, if we wanted to  store  40  bytes  in  a <structname>text</>
 
1233
     structure, we might use a code fragment like this:
 
1234
 
 
1235
<programlisting>
 
1236
#include "postgres.h"
 
1237
...
 
1238
char buffer[40]; /* our source data */
 
1239
...
 
1240
text *destination = (text *) palloc(VARHDRSZ + 40);
 
1241
destination-&gt;length = VARHDRSZ + 40;
 
1242
memcpy(destination-&gt;data, buffer, 40);
 
1243
...
 
1244
</programlisting>
 
1245
 
 
1246
     <literal>VARHDRSZ</> is the same as <literal>sizeof(int4)</>, but
 
1247
     it's considered good style to use the macro <literal>VARHDRSZ</>
 
1248
     to refer to the size of the overhead for a variable-length type.
 
1249
    </para>
 
1250
 
 
1251
    <para>
 
1252
     <xref linkend="xfunc-c-type-table"> specifies which C type
 
1253
     corresponds to which SQL type when writing a C-language function
 
1254
     that uses a built-in type of <productname>PostgreSQL</>.
 
1255
     The <quote>Defined In</quote> column gives the header file that
 
1256
     needs to be included to get the type definition.  (The actual
 
1257
     definition may be in a different file that is included by the
 
1258
     listed file.  It is recommended that users stick to the defined
 
1259
     interface.)  Note that you should always include
 
1260
     <filename>postgres.h</filename> first in any source file, because
 
1261
     it declares a number of things that you will need anyway.
 
1262
    </para>
 
1263
 
 
1264
     <table tocentry="1" id="xfunc-c-type-table">
 
1265
      <title>Equivalent C Types for Built-In SQL Types</title>
 
1266
      <tgroup cols="3">
 
1267
       <thead>
 
1268
        <row>
 
1269
         <entry>
 
1270
          SQL Type
 
1271
         </entry>
 
1272
         <entry>
 
1273
          C Type
 
1274
         </entry>
 
1275
         <entry>
 
1276
          Defined In
 
1277
         </entry>
 
1278
        </row>
 
1279
       </thead>
 
1280
       <tbody>
 
1281
        <row>
 
1282
         <entry><type>abstime</type></entry>
 
1283
         <entry><type>AbsoluteTime</type></entry>
 
1284
         <entry><filename>utils/nabstime.h</filename></entry>
 
1285
        </row>
 
1286
        <row>
 
1287
         <entry><type>boolean</type></entry>
 
1288
         <entry><type>bool</type></entry>
 
1289
         <entry><filename>postgres.h</filename> (maybe compiler built-in)</entry>
 
1290
        </row>
 
1291
        <row>
 
1292
         <entry><type>box</type></entry>
 
1293
         <entry><type>BOX*</type></entry>
 
1294
         <entry><filename>utils/geo_decls.h</filename></entry>
 
1295
        </row>
 
1296
        <row>
 
1297
         <entry><type>bytea</type></entry>
 
1298
         <entry><type>bytea*</type></entry>
 
1299
         <entry><filename>postgres.h</filename></entry>
 
1300
        </row>
 
1301
        <row>
 
1302
         <entry><type>"char"</type></entry>
 
1303
         <entry><type>char</type></entry>
 
1304
         <entry>(compiler built-in)</entry>
 
1305
        </row>
 
1306
        <row>
 
1307
         <entry><type>character</type></entry>
 
1308
         <entry><type>BpChar*</type></entry>
 
1309
         <entry><filename>postgres.h</filename></entry>
 
1310
        </row>
 
1311
        <row>
 
1312
         <entry><type>cid</type></entry>
 
1313
         <entry><type>CommandId</type></entry>
 
1314
         <entry><filename>postgres.h</filename></entry>
 
1315
        </row>
 
1316
        <row>
 
1317
         <entry><type>date</type></entry>
 
1318
         <entry><type>DateADT</type></entry>
 
1319
         <entry><filename>utils/date.h</filename></entry>
 
1320
        </row>
 
1321
        <row>
 
1322
         <entry><type>smallint</type> (<type>int2</type>)</entry>
 
1323
         <entry><type>int2</type> or <type>int16</type></entry>
 
1324
         <entry><filename>postgres.h</filename></entry>
 
1325
        </row>
 
1326
        <row>
 
1327
         <entry><type>int2vector</type></entry>
 
1328
         <entry><type>int2vector*</type></entry>
 
1329
         <entry><filename>postgres.h</filename></entry>
 
1330
        </row>
 
1331
        <row>
 
1332
         <entry><type>integer</type> (<type>int4</type>)</entry>
 
1333
         <entry><type>int4</type> or <type>int32</type></entry>
 
1334
         <entry><filename>postgres.h</filename></entry>
 
1335
        </row>
 
1336
        <row>
 
1337
         <entry><type>real</type> (<type>float4</type>)</entry>
 
1338
         <entry><type>float4*</type></entry>
 
1339
        <entry><filename>postgres.h</filename></entry>
 
1340
        </row>
 
1341
        <row>
 
1342
         <entry><type>double precision</type> (<type>float8</type>)</entry>
 
1343
         <entry><type>float8*</type></entry>
 
1344
         <entry><filename>postgres.h</filename></entry>
 
1345
        </row>
 
1346
        <row>
 
1347
         <entry><type>interval</type></entry>
 
1348
         <entry><type>Interval*</type></entry>
 
1349
         <entry><filename>utils/timestamp.h</filename></entry>
 
1350
        </row>
 
1351
        <row>
 
1352
         <entry><type>lseg</type></entry>
 
1353
         <entry><type>LSEG*</type></entry>
 
1354
         <entry><filename>utils/geo_decls.h</filename></entry>
 
1355
        </row>
 
1356
        <row>
 
1357
         <entry><type>name</type></entry>
 
1358
         <entry><type>Name</type></entry>
 
1359
         <entry><filename>postgres.h</filename></entry>
 
1360
        </row>
 
1361
        <row>
 
1362
         <entry><type>oid</type></entry>
 
1363
         <entry><type>Oid</type></entry>
 
1364
         <entry><filename>postgres.h</filename></entry>
 
1365
        </row>
 
1366
        <row>
 
1367
         <entry><type>oidvector</type></entry>
 
1368
         <entry><type>oidvector*</type></entry>
 
1369
         <entry><filename>postgres.h</filename></entry>
 
1370
        </row>
 
1371
        <row>
 
1372
         <entry><type>path</type></entry>
 
1373
         <entry><type>PATH*</type></entry>
 
1374
         <entry><filename>utils/geo_decls.h</filename></entry>
 
1375
        </row>
 
1376
        <row>
 
1377
         <entry><type>point</type></entry>
 
1378
         <entry><type>POINT*</type></entry>
 
1379
         <entry><filename>utils/geo_decls.h</filename></entry>
 
1380
        </row>
 
1381
        <row>
 
1382
         <entry><type>regproc</type></entry>
 
1383
         <entry><type>regproc</type></entry>
 
1384
         <entry><filename>postgres.h</filename></entry>
 
1385
        </row>
 
1386
        <row>
 
1387
         <entry><type>reltime</type></entry>
 
1388
         <entry><type>RelativeTime</type></entry>
 
1389
         <entry><filename>utils/nabstime.h</filename></entry>
 
1390
        </row>
 
1391
        <row>
 
1392
         <entry><type>text</type></entry>
 
1393
         <entry><type>text*</type></entry>
 
1394
         <entry><filename>postgres.h</filename></entry>
 
1395
        </row>
 
1396
        <row>
 
1397
         <entry><type>tid</type></entry>
 
1398
         <entry><type>ItemPointer</type></entry>
 
1399
         <entry><filename>storage/itemptr.h</filename></entry>
 
1400
        </row>
 
1401
        <row>
 
1402
         <entry><type>time</type></entry>
 
1403
         <entry><type>TimeADT</type></entry>
 
1404
         <entry><filename>utils/date.h</filename></entry>
 
1405
        </row>
 
1406
        <row>
 
1407
         <entry><type>time with time zone</type></entry>
 
1408
         <entry><type>TimeTzADT</type></entry>
 
1409
         <entry><filename>utils/date.h</filename></entry>
 
1410
        </row>
 
1411
        <row>
 
1412
         <entry><type>timestamp</type></entry>
 
1413
         <entry><type>Timestamp*</type></entry>
 
1414
         <entry><filename>utils/timestamp.h</filename></entry>
 
1415
        </row>
 
1416
        <row>
 
1417
         <entry><type>tinterval</type></entry>
 
1418
         <entry><type>TimeInterval</type></entry>
 
1419
         <entry><filename>utils/nabstime.h</filename></entry>
 
1420
        </row>
 
1421
        <row>
 
1422
         <entry><type>varchar</type></entry>
 
1423
         <entry><type>VarChar*</type></entry>
 
1424
         <entry><filename>postgres.h</filename></entry>
 
1425
        </row>
 
1426
        <row>
 
1427
         <entry><type>xid</type></entry>
 
1428
         <entry><type>TransactionId</type></entry>
 
1429
         <entry><filename>postgres.h</filename></entry>
 
1430
        </row>
 
1431
       </tbody>
 
1432
      </tgroup>
 
1433
     </table>
 
1434
 
 
1435
    <para>
 
1436
     Now that we've gone over all of the possible structures
 
1437
     for base types, we can show some examples of real functions.
 
1438
    </para>
 
1439
   </sect2>
 
1440
 
 
1441
   <sect2>
 
1442
    <title>Calling Conventions Version 0 for C-Language Functions</title>
 
1443
 
 
1444
    <para>
 
1445
     We present the <quote>old style</quote> calling convention first &mdash; although
 
1446
     this approach is now deprecated, it's easier to get a handle on
 
1447
     initially.  In the version-0 method, the arguments and result
 
1448
     of the C function are just declared in normal C style, but being
 
1449
     careful to use the C representation of each SQL data type as shown
 
1450
     above.
 
1451
    </para>
 
1452
 
 
1453
    <para>
 
1454
     Here are some examples:
 
1455
 
 
1456
<programlisting>
 
1457
#include "postgres.h"
 
1458
#include &lt;string.h&gt;
 
1459
 
 
1460
/* by value */
 
1461
         
 
1462
int
 
1463
add_one(int arg)
 
1464
{
 
1465
    return arg + 1;
 
1466
}
 
1467
 
 
1468
/* by reference, fixed length */
 
1469
 
 
1470
float8 *
 
1471
add_one_float8(float8 *arg)
 
1472
{
 
1473
    float8    *result = (float8 *) palloc(sizeof(float8));
 
1474
 
 
1475
    *result = *arg + 1.0;
 
1476
       
 
1477
    return result;
 
1478
}
 
1479
 
 
1480
Point *
 
1481
makepoint(Point *pointx, Point *pointy)
 
1482
{
 
1483
    Point     *new_point = (Point *) palloc(sizeof(Point));
 
1484
 
 
1485
    new_point-&gt;x = pointx-&gt;x;
 
1486
    new_point-&gt;y = pointy-&gt;y;
 
1487
       
 
1488
    return new_point;
 
1489
}
 
1490
 
 
1491
/* by reference, variable length */
 
1492
 
 
1493
text *
 
1494
copytext(text *t)
 
1495
{
 
1496
    /*
 
1497
     * VARSIZE is the total size of the struct in bytes.
 
1498
     */
 
1499
    text *new_t = (text *) palloc(VARSIZE(t));
 
1500
    VARATT_SIZEP(new_t) = VARSIZE(t);
 
1501
    /*
 
1502
     * VARDATA is a pointer to the data region of the struct.
 
1503
     */
 
1504
    memcpy((void *) VARDATA(new_t), /* destination */
 
1505
           (void *) VARDATA(t),     /* source */
 
1506
           VARSIZE(t)-VARHDRSZ);    /* how many bytes */
 
1507
    return new_t;
 
1508
}
 
1509
 
 
1510
text *
 
1511
concat_text(text *arg1, text *arg2)
 
1512
{
 
1513
    int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
 
1514
    text *new_text = (text *) palloc(new_text_size);
 
1515
 
 
1516
    VARATT_SIZEP(new_text) = new_text_size;
 
1517
    memcpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
 
1518
    memcpy(VARDATA(new_text) + (VARSIZE(arg1)-VARHDRSZ),
 
1519
           VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
 
1520
    return new_text;
 
1521
}
 
1522
</programlisting>
 
1523
    </para>
 
1524
 
 
1525
    <para>
 
1526
     Supposing that the above code has been prepared in file
 
1527
     <filename>funcs.c</filename> and compiled into a shared object,
 
1528
     we could define the functions to <productname>PostgreSQL</productname>
 
1529
     with commands like this:
 
1530
     
 
1531
<programlisting>
 
1532
CREATE FUNCTION add_one(integer) RETURNS integer
 
1533
     AS '<replaceable>DIRECTORY</replaceable>/funcs', 'add_one'
 
1534
     LANGUAGE C STRICT;
 
1535
 
 
1536
-- note overloading of SQL function name "add_one"
 
1537
CREATE FUNCTION add_one(double precision) RETURNS double precision
 
1538
     AS '<replaceable>DIRECTORY</replaceable>/funcs', 'add_one_float8'
 
1539
     LANGUAGE C STRICT;
 
1540
 
 
1541
CREATE FUNCTION makepoint(point, point) RETURNS point
 
1542
     AS '<replaceable>DIRECTORY</replaceable>/funcs', 'makepoint'
 
1543
     LANGUAGE C STRICT;
 
1544
                         
 
1545
CREATE FUNCTION copytext(text) RETURNS text
 
1546
     AS '<replaceable>DIRECTORY</replaceable>/funcs', 'copytext'
 
1547
     LANGUAGE C STRICT;
 
1548
 
 
1549
CREATE FUNCTION concat_text(text, text) RETURNS text
 
1550
     AS '<replaceable>DIRECTORY</replaceable>/funcs', 'concat_text',
 
1551
     LANGUAGE C STRICT;
 
1552
</programlisting>
 
1553
    </para>
 
1554
 
 
1555
    <para>
 
1556
     Here, <replaceable>DIRECTORY</replaceable> stands for the
 
1557
     directory of the shared library file (for instance the
 
1558
     <productname>PostgreSQL</productname> tutorial directory, which
 
1559
     contains the code for the examples used in this section).
 
1560
     (Better style would be to use just <literal>'funcs'</> in the
 
1561
     <literal>AS</> clause, after having added
 
1562
     <replaceable>DIRECTORY</replaceable> to the search path.  In any
 
1563
     case, we may omit the system-specific extension for a shared
 
1564
     library, commonly <literal>.so</literal> or
 
1565
     <literal>.sl</literal>.)
 
1566
    </para>
 
1567
 
 
1568
    <para>
 
1569
     Notice that we have specified the functions as <quote>strict</quote>,
 
1570
     meaning that
 
1571
     the system should automatically assume a null result if any input
 
1572
     value is null.  By doing this, we avoid having to check for null inputs
 
1573
     in the function code.  Without this, we'd have to check for null values
 
1574
     explicitly, by checking for a null pointer for each
 
1575
     pass-by-reference argument.  (For pass-by-value arguments, we don't
 
1576
     even have a way to check!)
 
1577
    </para>
 
1578
 
 
1579
    <para>
 
1580
     Although this calling convention is simple to use,
 
1581
     it is not very portable; on some architectures there are problems
 
1582
     with passing data types that are smaller than <type>int</type> this way.  Also, there is
 
1583
     no simple way to return a null result, nor to cope with null arguments
 
1584
     in any way other than making the function strict.  The version-1
 
1585
     convention, presented next, overcomes these objections.
 
1586
    </para>
 
1587
   </sect2>
 
1588
 
 
1589
   <sect2>
 
1590
    <title>Calling Conventions Version 1 for C-Language Functions</title>
 
1591
 
 
1592
    <para>
 
1593
     The version-1 calling convention relies on macros to suppress most
 
1594
     of the complexity of passing arguments and results.  The C declaration
 
1595
     of a version-1 function is always
 
1596
<programlisting>
 
1597
Datum funcname(PG_FUNCTION_ARGS)
 
1598
</programlisting>
 
1599
     In addition, the macro call
 
1600
<programlisting>
 
1601
PG_FUNCTION_INFO_V1(funcname);
 
1602
</programlisting>
 
1603
     must appear in the same source file.  (Conventionally. it's
 
1604
     written just before the function itself.)  This macro call is not
 
1605
     needed for <literal>internal</>-language functions, since
 
1606
     <productname>PostgreSQL</> assumes that all internal functions
 
1607
     use the version-1 convention.  It is, however, required for
 
1608
     dynamically-loaded functions.
 
1609
    </para>
 
1610
 
 
1611
    <para>
 
1612
     In a version-1 function, each actual argument is fetched using a
 
1613
     <function>PG_GETARG_<replaceable>xxx</replaceable>()</function>
 
1614
     macro that corresponds to the argument's data type, and the
 
1615
     result is returned using a
 
1616
     <function>PG_RETURN_<replaceable>xxx</replaceable>()</function>
 
1617
     macro for the return type.
 
1618
     <function>PG_GETARG_<replaceable>xxx</replaceable>()</function>
 
1619
     takes as its argument the number of the function argument to
 
1620
     fetch, where the count starts at 0.
 
1621
     <function>PG_RETURN_<replaceable>xxx</replaceable>()</function>
 
1622
     takes as its argument the actual value to return.
 
1623
    </para>
 
1624
 
 
1625
    <para>
 
1626
     Here we show the same functions as above, coded in version-1 style:
 
1627
 
 
1628
<programlisting>
 
1629
#include "postgres.h"
 
1630
#include &lt;string.h&gt;
 
1631
#include "fmgr.h"
 
1632
 
 
1633
/* by value */
 
1634
 
 
1635
PG_FUNCTION_INFO_V1(add_one);
 
1636
         
 
1637
Datum
 
1638
add_one(PG_FUNCTION_ARGS)
 
1639
{
 
1640
    int32   arg = PG_GETARG_INT32(0);
 
1641
 
 
1642
    PG_RETURN_INT32(arg + 1);
 
1643
}
 
1644
 
 
1645
/* by reference, fixed length */
 
1646
 
 
1647
PG_FUNCTION_INFO_V1(add_one_float8);
 
1648
 
 
1649
Datum
 
1650
add_one_float8(PG_FUNCTION_ARGS)
 
1651
{
 
1652
    /* The macros for FLOAT8 hide its pass-by-reference nature. */
 
1653
    float8   arg = PG_GETARG_FLOAT8(0);
 
1654
 
 
1655
    PG_RETURN_FLOAT8(arg + 1.0);
 
1656
}
 
1657
 
 
1658
PG_FUNCTION_INFO_V1(makepoint);
 
1659
 
 
1660
Datum
 
1661
makepoint(PG_FUNCTION_ARGS)
 
1662
{
 
1663
    /* Here, the pass-by-reference nature of Point is not hidden. */
 
1664
    Point     *pointx = PG_GETARG_POINT_P(0);
 
1665
    Point     *pointy = PG_GETARG_POINT_P(1);
 
1666
    Point     *new_point = (Point *) palloc(sizeof(Point));
 
1667
 
 
1668
    new_point-&gt;x = pointx-&gt;x;
 
1669
    new_point-&gt;y = pointy-&gt;y;
 
1670
       
 
1671
    PG_RETURN_POINT_P(new_point);
 
1672
}
 
1673
 
 
1674
/* by reference, variable length */
 
1675
 
 
1676
PG_FUNCTION_INFO_V1(copytext);
 
1677
 
 
1678
Datum
 
1679
copytext(PG_FUNCTION_ARGS)
 
1680
{
 
1681
    text     *t = PG_GETARG_TEXT_P(0);
 
1682
    /*
 
1683
     * VARSIZE is the total size of the struct in bytes.
 
1684
     */
 
1685
    text     *new_t = (text *) palloc(VARSIZE(t));
 
1686
    VARATT_SIZEP(new_t) = VARSIZE(t);
 
1687
    /*
 
1688
     * VARDATA is a pointer to the data region of the struct.
 
1689
     */
 
1690
    memcpy((void *) VARDATA(new_t), /* destination */
 
1691
           (void *) VARDATA(t),     /* source */
 
1692
           VARSIZE(t)-VARHDRSZ);    /* how many bytes */
 
1693
    PG_RETURN_TEXT_P(new_t);
 
1694
}
 
1695
 
 
1696
PG_FUNCTION_INFO_V1(concat_text);
 
1697
 
 
1698
Datum
 
1699
concat_text(PG_FUNCTION_ARGS)
 
1700
{
 
1701
    text  *arg1 = PG_GETARG_TEXT_P(0);
 
1702
    text  *arg2 = PG_GETARG_TEXT_P(1);
 
1703
    int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
 
1704
    text *new_text = (text *) palloc(new_text_size);
 
1705
 
 
1706
    VARATT_SIZEP(new_text) = new_text_size;
 
1707
    memcpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
 
1708
    memcpy(VARDATA(new_text) + (VARSIZE(arg1)-VARHDRSZ),
 
1709
           VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
 
1710
    PG_RETURN_TEXT_P(new_text);
 
1711
}
 
1712
</programlisting>
 
1713
    </para>
 
1714
 
 
1715
    <para>
 
1716
     The <command>CREATE FUNCTION</command> commands are the same as
 
1717
     for the version-0 equivalents.
 
1718
    </para>
 
1719
 
 
1720
    <para>
 
1721
     At first glance, the version-1 coding conventions may appear to
 
1722
     be just pointless obscurantism.  They do, however, offer a number
 
1723
     of improvements, because the macros can hide unnecessary detail.
 
1724
     An example is that in coding <function>add_one_float8</>, we no longer need to
 
1725
     be aware that <type>float8</type> is a pass-by-reference type.  Another
 
1726
     example is that the <literal>GETARG</> macros for variable-length types allow
 
1727
     for more efficient fetching of <quote>toasted</quote> (compressed or
 
1728
     out-of-line) values.
 
1729
    </para>
 
1730
 
 
1731
    <para>
 
1732
     One big improvement in version-1 functions is better handling of null
 
1733
     inputs and results.  The macro <function>PG_ARGISNULL(<replaceable>n</>)</function>
 
1734
     allows a function to test whether each input is null.  (Of course, doing
 
1735
     this is only necessary in functions not declared <quote>strict</>.)
 
1736
     As with the
 
1737
     <function>PG_GETARG_<replaceable>xxx</replaceable>()</function> macros,
 
1738
     the input arguments are counted beginning at zero.  Note that one
 
1739
     should refrain from executing
 
1740
     <function>PG_GETARG_<replaceable>xxx</replaceable>()</function> until
 
1741
     one has verified that the argument isn't null.
 
1742
     To return a null result, execute <function>PG_RETURN_NULL()</function>;
 
1743
     this works in both strict and nonstrict functions.
 
1744
    </para>
 
1745
 
 
1746
    <para>
 
1747
     Other options provided in the new-style interface are two
 
1748
     variants of the
 
1749
     <function>PG_GETARG_<replaceable>xxx</replaceable>()</function>
 
1750
     macros. The first of these,
 
1751
     <function>PG_GETARG_<replaceable>xxx</replaceable>_COPY()</function>,
 
1752
     guarantees to return a copy of the specified argument that is
 
1753
     safe for writing into. (The normal macros will sometimes return a
 
1754
     pointer to a value that is physically stored in a table, which
 
1755
     must not be written to. Using the
 
1756
     <function>PG_GETARG_<replaceable>xxx</replaceable>_COPY()</function>
 
1757
     macros guarantees a writable result.)
 
1758
    The second variant consists of the
 
1759
    <function>PG_GETARG_<replaceable>xxx</replaceable>_SLICE()</function>
 
1760
    macros which take three arguments. The first is the number of the
 
1761
    function argument (as above). The second and third are the offset and
 
1762
    length of the segment to be returned. Offsets are counted from
 
1763
    zero, and a negative length requests that the remainder of the
 
1764
    value be returned. These macros provide more efficient access to
 
1765
    parts of large values in the case where they have storage type
 
1766
    <quote>external</quote>. (The storage type of a column can be specified using
 
1767
    <literal>ALTER TABLE <replaceable>tablename</replaceable> ALTER
 
1768
    COLUMN <replaceable>colname</replaceable> SET STORAGE
 
1769
    <replaceable>storagetype</replaceable></literal>. <replaceable>storagetype</replaceable> is one of
 
1770
    <literal>plain</>, <literal>external</>, <literal>extended</literal>,
 
1771
     or <literal>main</>.)
 
1772
    </para>
 
1773
 
 
1774
    <para>
 
1775
     Finally, the version-1 function call conventions make it possible
 
1776
     to return set results (<xref linkend="xfunc-c-return-set">) and
 
1777
     implement trigger functions (<xref linkend="triggers">) and
 
1778
     procedural-language call handlers (<xref
 
1779
     linkend="plhandler">).  Version-1 code is also more
 
1780
     portable than version-0, because it does not break restrictions
 
1781
     on function call protocol in the C standard.  For more details
 
1782
     see <filename>src/backend/utils/fmgr/README</filename> in the
 
1783
     source distribution.
 
1784
    </para>
 
1785
   </sect2>
 
1786
 
 
1787
   <sect2>
 
1788
    <title>Writing Code</title>
 
1789
 
 
1790
    <para>
 
1791
     Before we turn to the more advanced topics, we should discuss
 
1792
     some coding rules for <productname>PostgreSQL</productname>
 
1793
     C-language functions.  While it may be possible to load functions
 
1794
     written in languages other than C into
 
1795
     <productname>PostgreSQL</productname>, this is usually difficult
 
1796
     (when it is possible at all) because other languages, such as
 
1797
     C++, FORTRAN, or Pascal often do not follow the same calling
 
1798
     convention as C.  That is, other languages do not pass argument
 
1799
     and return values between functions in the same way.  For this
 
1800
     reason, we will assume that your C-language functions are
 
1801
     actually written in C.
 
1802
    </para>
 
1803
 
 
1804
    <para>
 
1805
     The basic rules for writing and building C functions are as follows:
 
1806
 
 
1807
     <itemizedlist>
 
1808
      <listitem>
 
1809
       <para>
 
1810
        Use <literal>pg_config
 
1811
        --includedir-server</literal><indexterm><primary>pg_config</><secondary>with user-defined C functions</></>
 
1812
        to find out where the <productname>PostgreSQL</> server header
 
1813
        files are installed on your system (or the system that your
 
1814
        users will be running on).  This option is new with
 
1815
        <productname>PostgreSQL</> 7.2.  For
 
1816
        <productname>PostgreSQL</> 7.1 you should use the option
 
1817
        <option>--includedir</option>.  (<command>pg_config</command>
 
1818
        will exit with a non-zero status if it encounters an unknown
 
1819
        option.)  For releases prior to 7.1 you will have to guess,
 
1820
        but since that was before the current calling conventions were
 
1821
        introduced, it is unlikely that you want to support those
 
1822
        releases.
 
1823
       </para>
 
1824
      </listitem>
 
1825
 
 
1826
      <listitem>
 
1827
       <para>
 
1828
        When allocating memory, use the
 
1829
        <productname>PostgreSQL</productname> functions
 
1830
        <function>palloc</function><indexterm><primary>palloc</></> and <function>pfree</function><indexterm><primary>pfree</></>
 
1831
        instead of the corresponding C library functions
 
1832
        <function>malloc</function> and <function>free</function>.
 
1833
        The memory allocated by <function>palloc</function> will be
 
1834
        freed automatically at the end of each transaction, preventing
 
1835
        memory leaks.
 
1836
       </para>
 
1837
      </listitem>
 
1838
 
 
1839
      <listitem>
 
1840
       <para>
 
1841
        Always zero the bytes of your structures using
 
1842
        <function>memset</function>.  Without this, it's difficult to
 
1843
        support hash indexes or hash joins, as you must pick out only
 
1844
        the significant bits of your data structure to compute a hash.
 
1845
        Even if you initialize all fields of your structure, there may be
 
1846
        alignment padding (holes in the structure) that may contain
 
1847
        garbage values.
 
1848
       </para>
 
1849
      </listitem>
 
1850
 
 
1851
      <listitem>
 
1852
       <para>
 
1853
        Most of the internal <productname>PostgreSQL</productname>
 
1854
        types are declared in <filename>postgres.h</filename>, while
 
1855
        the function manager interfaces
 
1856
        (<symbol>PG_FUNCTION_ARGS</symbol>, etc.)  are in
 
1857
        <filename>fmgr.h</filename>, so you will need to include at
 
1858
        least these two files.  For portability reasons it's best to
 
1859
        include <filename>postgres.h</filename> <emphasis>first</>,
 
1860
        before any other system or user header files.  Including
 
1861
        <filename>postgres.h</filename> will also include
 
1862
        <filename>elog.h</filename> and <filename>palloc.h</filename>
 
1863
        for you.
 
1864
       </para>
 
1865
      </listitem>
 
1866
 
 
1867
      <listitem>
 
1868
       <para>
 
1869
        Symbol names defined within object files must not conflict
 
1870
        with each other or with symbols defined in the
 
1871
        <productname>PostgreSQL</productname> server executable.  You
 
1872
        will have to rename your functions or variables if you get
 
1873
        error messages to this effect.
 
1874
       </para>
 
1875
      </listitem>
 
1876
 
 
1877
      <listitem>
 
1878
       <para>
 
1879
        Compiling and linking your code so that it can be dynamically
 
1880
        loaded into <productname>PostgreSQL</productname> always
 
1881
        requires special flags.  See <xref linkend="dfunc"> for a
 
1882
        detailed explanation of how to do it for your particular
 
1883
        operating system.
 
1884
       </para>
 
1885
      </listitem>
 
1886
     </itemizedlist>
 
1887
    </para>
 
1888
   </sect2>
 
1889
 
 
1890
&dfunc;
 
1891
 
 
1892
   <sect2 id="xfunc-c-pgxs">
 
1893
    <title>Extension Building Infrastructure</title>
 
1894
 
 
1895
   <indexterm zone="xfunc-c-pgxs">
 
1896
    <primary>pgxs</primary>
 
1897
   </indexterm>
 
1898
 
 
1899
   <para>
 
1900
    If you are thinking about distributing your
 
1901
    <productname>PostgreSQL</> extension modules, setting up a
 
1902
    portable build system for them can be fairly difficult.  Therefore
 
1903
    the <productname>PostgreSQL</> installation provides a build
 
1904
    infrastructure for extensions, called <acronym>PGXS</acronym>, so
 
1905
    that simple extension modules can be built simply against an
 
1906
    already installed server.  Note that this infrastructure is not
 
1907
    intended to be a universal build system framework that can be used
 
1908
    to build all software interfacing to <productname>PostgreSQL</>;
 
1909
    it simply automates common build rules for simple server extension
 
1910
    modules.  For more complicated packages, you need to write your
 
1911
    own build system.
 
1912
   </para>
 
1913
 
 
1914
   <para>
 
1915
    To use the infrastructure for your extension, you must write a
 
1916
    simple makefile.  In that makefile, you need to set some variables
 
1917
    and finally include the global <acronym>PGXS</acronym> makefile.
 
1918
    Here is an example that builds an extension module named
 
1919
    <literal>isbn_issn</literal> consisting of a shared library, an
 
1920
    SQL script, and a documentation text file:
 
1921
<programlisting>
 
1922
MODULES = isbn_issn
 
1923
DATA_built = isbn_issn.sql
 
1924
DOCS = README.isbn_issn
 
1925
 
 
1926
PGXS := $(shell pg_config --pgxs)
 
1927
include $(PGXS)
 
1928
</programlisting>
 
1929
    The last two lines should always be the same.  Earlier in the
 
1930
    file, you assign variables or add custom
 
1931
    <application>make</application> rules.
 
1932
   </para>
 
1933
 
 
1934
   <para>
 
1935
    The following variables can be set:
 
1936
 
 
1937
    <variablelist>
 
1938
     <varlistentry>
 
1939
      <term><varname>MODULES</varname></term>
 
1940
      <listitem>
 
1941
       <para>
 
1942
        list of shared objects to be built from source file with same
 
1943
        stem (do not include suffix in this list)
 
1944
       </para>
 
1945
      </listitem>
 
1946
     </varlistentry>
 
1947
 
 
1948
     <varlistentry>
 
1949
      <term><varname>DATA</varname></term>
 
1950
      <listitem>
 
1951
       <para>
 
1952
        random files to install into <literal><replaceable>prefix</replaceable>/share/contrib</literal>
 
1953
       </para>
 
1954
      </listitem>
 
1955
     </varlistentry>
 
1956
 
 
1957
     <varlistentry>
 
1958
      <term><varname>DATA_built</varname></term>
 
1959
      <listitem>
 
1960
       <para>
 
1961
        random files to install into
 
1962
        <literal><replaceable>prefix</replaceable>/share/contrib</literal>,
 
1963
        which need to be built first
 
1964
       </para>
 
1965
      </listitem>
 
1966
     </varlistentry>
 
1967
 
 
1968
     <varlistentry>
 
1969
      <term><varname>DOCS</varname></term>
 
1970
      <listitem>
 
1971
       <para>
 
1972
        random files to install under
 
1973
        <literal><replaceable>prefix</replaceable>/doc/contrib</literal>
 
1974
       </para>
 
1975
      </listitem>
 
1976
     </varlistentry>
 
1977
 
 
1978
     <varlistentry>
 
1979
      <term><varname>SCRIPTS</varname></term>
 
1980
      <listitem>
 
1981
       <para>
 
1982
        script files (not binaries) to install into
 
1983
        <literal><replaceable>prefix</replaceable>/bin</literal>
 
1984
       </para>
 
1985
      </listitem>
 
1986
     </varlistentry>
 
1987
 
 
1988
     <varlistentry>
 
1989
      <term><varname>SCRIPTS_built</varname></term>
 
1990
      <listitem>
 
1991
       <para>
 
1992
        script files (not binaries) to install into
 
1993
        <literal><replaceable>prefix</replaceable>/bin</literal>,
 
1994
        which need to be built first
 
1995
       </para>
 
1996
      </listitem>
 
1997
     </varlistentry>
 
1998
 
 
1999
     <varlistentry>
 
2000
      <term><varname>REGRESS</varname></term>
 
2001
      <listitem>
 
2002
       <para>
 
2003
        list of regression test cases (without suffix)
 
2004
       </para>
 
2005
      </listitem>
 
2006
     </varlistentry>
 
2007
    </variablelist>
 
2008
 
 
2009
    or at most one of these two:
 
2010
 
 
2011
    <variablelist>
 
2012
     <varlistentry>
 
2013
      <term><varname>PROGRAM</varname></term>
 
2014
      <listitem>
 
2015
       <para>
 
2016
        a binary program to build (list objects files in <varname>OBJS</varname>)
 
2017
       </para>
 
2018
      </listitem>
 
2019
     </varlistentry>
 
2020
 
 
2021
     <varlistentry>
 
2022
      <term><varname>MODULE_big</varname></term>
 
2023
      <listitem>
 
2024
       <para>
 
2025
        a shared object to build (list object files in <varname>OBJS</varname>)
 
2026
       </para>
 
2027
      </listitem>
 
2028
     </varlistentry>
 
2029
    </variablelist>
 
2030
 
 
2031
    The following can also be set:
 
2032
 
 
2033
    <variablelist>
 
2034
 
 
2035
     <varlistentry>
 
2036
      <term><varname>EXTRA_CLEAN</varname></term>
 
2037
      <listitem>
 
2038
       <para>
 
2039
        extra files to remove in <literal>make clean</literal>
 
2040
       </para>
 
2041
      </listitem>
 
2042
     </varlistentry>
 
2043
 
 
2044
     <varlistentry>
 
2045
      <term><varname>PG_CPPFLAGS</varname></term>
 
2046
      <listitem>
 
2047
       <para>
 
2048
        will be added to <varname>CPPFLAGS</varname>
 
2049
       </para>
 
2050
      </listitem>
 
2051
     </varlistentry>
 
2052
 
 
2053
     <varlistentry>
 
2054
      <term><varname>PG_LIBS</varname></term>
 
2055
      <listitem>
 
2056
       <para>
 
2057
        will be added to <varname>PROGRAM</varname> link line
 
2058
       </para>
 
2059
      </listitem>
 
2060
     </varlistentry>
 
2061
 
 
2062
     <varlistentry>
 
2063
      <term><varname>SHLIB_LINK</varname></term>
 
2064
      <listitem>
 
2065
       <para>
 
2066
        will be added to <varname>MODULE_big</varname> link line
 
2067
       </para>
 
2068
      </listitem>
 
2069
     </varlistentry>
 
2070
    </variablelist>
 
2071
   </para>
 
2072
 
 
2073
   <para>
 
2074
    Put this makefile as <literal>Makefile</literal> in the directory
 
2075
    which holds your extension. Then you can do
 
2076
    <literal>make</literal> to compile, and later <literal>make
 
2077
    install</literal> to install your module.  The extension is
 
2078
    compiled and installed for the
 
2079
    <productname>PostgreSQL</productname> installation that
 
2080
    corresponds to the first <command>pg_config</command> command
 
2081
    found in your path.
 
2082
   </para>
 
2083
  </sect2>
 
2084
 
 
2085
 
 
2086
   <sect2>
 
2087
    <title>Composite-Type Arguments in C-Language Functions</title>
 
2088
 
 
2089
    <para>
 
2090
     Composite types do not have a fixed layout like C structures.
 
2091
     Instances of a composite type may contain null fields.  In
 
2092
     addition, composite types that are part of an inheritance
 
2093
     hierarchy may have different fields than other members of the
 
2094
     same inheritance hierarchy.  Therefore,
 
2095
     <productname>PostgreSQL</productname> provides a function
 
2096
     interface for accessing fields of composite types from C.
 
2097
    </para>
 
2098
 
 
2099
    <para>
 
2100
     Suppose we want to write a function to answer the query
 
2101
 
 
2102
<programlisting>
 
2103
SELECT name, c_overpaid(emp, 1500) AS overpaid
 
2104
    FROM emp
 
2105
    WHERE name = 'Bill' OR name = 'Sam';
 
2106
</programlisting>
 
2107
 
 
2108
     Using call conventions version 0, we can define
 
2109
     <function>c_overpaid</> as:
 
2110
     
 
2111
<programlisting>
 
2112
#include "postgres.h"
 
2113
#include "executor/executor.h"  /* for GetAttributeByName() */
 
2114
 
 
2115
bool
 
2116
c_overpaid(HeapTupleHeader t, /* the current row of emp */
 
2117
           int32 limit)
 
2118
{
 
2119
    bool isnull;
 
2120
    int32 salary;
 
2121
 
 
2122
    salary = DatumGetInt32(GetAttributeByName(t, "salary", &amp;isnull));
 
2123
    if (isnull)
 
2124
        return false;
 
2125
    return salary &gt; limit;
 
2126
}
 
2127
</programlisting>
 
2128
 
 
2129
     In version-1 coding, the above would look like this:
 
2130
 
 
2131
<programlisting>
 
2132
#include "postgres.h"
 
2133
#include "executor/executor.h"  /* for GetAttributeByName() */
 
2134
 
 
2135
PG_FUNCTION_INFO_V1(c_overpaid);
 
2136
 
 
2137
Datum
 
2138
c_overpaid(PG_FUNCTION_ARGS)
 
2139
{
 
2140
    HeapTupleHeader  t = PG_GETARG_HEAPTUPLEHEADER(0);
 
2141
    int32            limit = PG_GETARG_INT32(1);
 
2142
    bool isnull;
 
2143
    Datum salary;
 
2144
 
 
2145
    salary = GetAttributeByName(t, "salary", &amp;isnull);
 
2146
    if (isnull)
 
2147
        PG_RETURN_BOOL(false);
 
2148
    /* Alternatively, we might prefer to do PG_RETURN_NULL() for null salary. */
 
2149
 
 
2150
    PG_RETURN_BOOL(DatumGetInt32(salary) &gt; limit);
 
2151
}
 
2152
</programlisting>
 
2153
    </para>
 
2154
 
 
2155
    <para>
 
2156
     <function>GetAttributeByName</function> is the 
 
2157
     <productname>PostgreSQL</productname> system function that
 
2158
     returns attributes out of the specified row.  It has
 
2159
     three arguments: the argument of type <type>HeapTupleHeader</type> passed
 
2160
     into 
 
2161
     the  function, the name of the desired attribute, and a
 
2162
     return parameter that tells whether  the  attribute
 
2163
     is  null.   <function>GetAttributeByName</function> returns a <type>Datum</type>
 
2164
     value that you can convert to the proper data type by using the
 
2165
     appropriate <function>DatumGet<replaceable>XXX</replaceable>()</function>
 
2166
     macro.  Note that the return value is meaningless if the null flag is
 
2167
     set; always check the null flag before trying to do anything with the
 
2168
     result.
 
2169
    </para>
 
2170
 
 
2171
    <para>
 
2172
     There is also <function>GetAttributeByNum</function>, which selects
 
2173
     the target attribute by column number instead of name.
 
2174
    </para>
 
2175
 
 
2176
    <para>
 
2177
     The following command declares the function
 
2178
     <function>c_overpaid</function> in SQL:
 
2179
 
 
2180
<programlisting>
 
2181
CREATE FUNCTION c_overpaid(emp, integer) RETURNS boolean
 
2182
    AS '<replaceable>DIRECTORY</replaceable>/funcs', 'c_overpaid'
 
2183
    LANGUAGE C STRICT;
 
2184
</programlisting>
 
2185
 
 
2186
     Notice we have used <literal>STRICT</> so that we did not have to
 
2187
     check whether the input arguments were NULL.
 
2188
    </para>
 
2189
   </sect2>
 
2190
 
 
2191
   <sect2>
 
2192
    <title>Returning Rows (Composite Types) from C-Language Functions</title>
 
2193
 
 
2194
    <para>
 
2195
     To return a row or composite-type value from a C-language
 
2196
     function, you can use a special API that provides macros and
 
2197
     functions to hide most of the complexity of building composite
 
2198
     data types.  To use this API, the source file must include:
 
2199
<programlisting>
 
2200
#include "funcapi.h"
 
2201
</programlisting>
 
2202
    </para>
 
2203
 
 
2204
    <para>
 
2205
     There are two ways you can build a composite data value (henceforth
 
2206
     a <quote>tuple</>): you can build it from an array of Datum values,
 
2207
     or from an array of C strings that can be passed to the input
 
2208
     conversion functions of the tuple's column data types.  In either
 
2209
     case, you first need to obtain or construct a <structname>TupleDesc</>
 
2210
     descriptor for the tuple structure.  When working with Datums, you
 
2211
     pass the <structname>TupleDesc</> to <function>BlessTupleDesc</>,
 
2212
     and then call <function>heap_formtuple</> for each row.  When working
 
2213
     with C strings, you pass the <structname>TupleDesc</> to
 
2214
     <function>TupleDescGetAttInMetadata</>, and then call
 
2215
     <function>BuildTupleFromCStrings</> for each row.  In the case of a
 
2216
     function returning a set of tuples, the setup steps can all be done
 
2217
     once during the first call of the function.
 
2218
    </para>
 
2219
 
 
2220
    <para>
 
2221
     Several helper functions are available for setting up the initial
 
2222
     <structname>TupleDesc</>.  If you want to use a named composite type,
 
2223
     you can fetch the information from the system catalogs.  Use
 
2224
<programlisting>
 
2225
TupleDesc RelationNameGetTupleDesc(const char *relname)
 
2226
</programlisting>
 
2227
     to get a <structname>TupleDesc</> for a named relation, or
 
2228
<programlisting>
 
2229
TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases)
 
2230
</programlisting>
 
2231
     to get a <structname>TupleDesc</> based on a type OID. This can
 
2232
     be used to get a <structname>TupleDesc</> for a base or
 
2233
     composite type.  When writing a function that returns
 
2234
     <structname>record</>, the expected <structname>TupleDesc</> 
 
2235
     must be passed in by the caller.
 
2236
    </para>
 
2237
 
 
2238
    <para>
 
2239
     Once you have a <structname>TupleDesc</>, call
 
2240
<programlisting>
 
2241
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
 
2242
</programlisting>
 
2243
     if you plan to work with Datums, or
 
2244
<programlisting>
 
2245
AttInMetadata *TupleDescGetAttInMetadata(TupleDesc tupdesc)
 
2246
</programlisting>
 
2247
     if you plan to work with C strings.  If you are writing a function
 
2248
     returning set, you can save the results of these functions in the
 
2249
     <structname>FuncCallContext</> structure &mdash; use the
 
2250
     <structfield>tuple_desc</> or <structfield>attinmeta</> field
 
2251
     respectively.
 
2252
    </para>
 
2253
 
 
2254
    <para>
 
2255
     When working with Datums, use
 
2256
<programlisting>
 
2257
HeapTuple heap_formtuple(TupleDesc tupdesc, Datum *values, char *nulls)
 
2258
</programlisting>
 
2259
     to build a <structname>HeapTuple</> given user data in Datum form.
 
2260
    </para>
 
2261
 
 
2262
    <para>
 
2263
     When working with C strings, use
 
2264
<programlisting>
 
2265
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
 
2266
</programlisting>
 
2267
     to build a <structname>HeapTuple</> given user data
 
2268
     in C string form.  <literal>values</literal> is an array of C strings,
 
2269
     one for each attribute of the return row. Each C string should be in
 
2270
     the form expected by the input function of the attribute data
 
2271
     type. In order to return a null value for one of the attributes,
 
2272
     the corresponding pointer in the <parameter>values</> array
 
2273
     should be set to <symbol>NULL</>.  This function will need to
 
2274
     be called again for each row you return.
 
2275
    </para>
 
2276
 
 
2277
    <para>
 
2278
     Once you have built a tuple to return from your function, it
 
2279
     must be converted into a <type>Datum</>. Use
 
2280
<programlisting>
 
2281
HeapTupleGetDatum(HeapTuple tuple)
 
2282
</programlisting>
 
2283
     to convert a <structname>HeapTuple</> into a valid Datum.  This
 
2284
     <type>Datum</> can be returned directly if you intend to return
 
2285
     just a single row, or it can be used as the current return value
 
2286
     in a set-returning function.
 
2287
    </para>
 
2288
 
 
2289
    <para>
 
2290
     An example appears in the next section.
 
2291
    </para>
 
2292
 
 
2293
   </sect2>
 
2294
 
 
2295
   <sect2 id="xfunc-c-return-set">
 
2296
    <title>Returning Sets from C-Language Functions</title>
 
2297
 
 
2298
    <para>
 
2299
     There is also a special API that provides support for returning
 
2300
     sets (multiple rows) from a C-language function.  A set-returning
 
2301
     function must follow the version-1 calling conventions.  Also,
 
2302
     source files must include <filename>funcapi.h</filename>, as
 
2303
     above.
 
2304
    </para>
 
2305
 
 
2306
    <para>
 
2307
     A set-returning function (<acronym>SRF</>) is called
 
2308
     once for each item it returns.  The <acronym>SRF</> must
 
2309
     therefore save enough state to remember what it was doing and
 
2310
     return the next item on each call.
 
2311
     The structure <structname>FuncCallContext</> is provided to help
 
2312
     control this process.  Within a function, <literal>fcinfo-&gt;flinfo-&gt;fn_extra</>
 
2313
     is used to hold a pointer to <structname>FuncCallContext</>
 
2314
     across calls.
 
2315
<programlisting>
 
2316
typedef struct
 
2317
{
 
2318
    /*
 
2319
     * Number of times we've been called before
 
2320
     * 
 
2321
     * call_cntr is initialized to 0 for you by SRF_FIRSTCALL_INIT(), and
 
2322
     * incremented for you every time SRF_RETURN_NEXT() is called.
 
2323
     */
 
2324
    uint32 call_cntr;
 
2325
 
 
2326
    /*
 
2327
     * OPTIONAL maximum number of calls
 
2328
     *
 
2329
     * max_calls is here for convenience only and setting it is optional.
 
2330
     * If not set, you must provide alternative means to know when the
 
2331
     * function is done.
 
2332
     */
 
2333
    uint32 max_calls;
 
2334
 
 
2335
    /*
 
2336
     * OPTIONAL pointer to result slot
 
2337
     * 
 
2338
     * This is obsolete and only present for backwards compatibility, viz,
 
2339
     * user-defined SRFs that use the deprecated TupleDescGetSlot().
 
2340
     */
 
2341
    TupleTableSlot *slot;
 
2342
 
 
2343
    /*
 
2344
     * OPTIONAL pointer to miscellaneous user-provided context information
 
2345
     * 
 
2346
     * user_fctx is for use as a pointer to your own data to retain
 
2347
     * arbitrary context information between calls of your function.
 
2348
     */
 
2349
    void *user_fctx;
 
2350
 
 
2351
    /*
 
2352
     * OPTIONAL pointer to struct containing attribute type input metadata
 
2353
     * 
 
2354
     * attinmeta is for use when returning tuples (i.e., composite data types)
 
2355
     * and is not used when returning base data types. It is only needed
 
2356
     * if you intend to use BuildTupleFromCStrings() to create the return
 
2357
     * tuple.
 
2358
     */
 
2359
    AttInMetadata *attinmeta;
 
2360
 
 
2361
    /*
 
2362
     * memory context used for structures that must live for multiple calls
 
2363
     *
 
2364
     * multi_call_memory_ctx is set by SRF_FIRSTCALL_INIT() for you, and used
 
2365
     * by SRF_RETURN_DONE() for cleanup. It is the most appropriate memory
 
2366
     * context for any memory that is to be reused across multiple calls
 
2367
     * of the SRF.
 
2368
     */
 
2369
    MemoryContext multi_call_memory_ctx;
 
2370
 
 
2371
    /*
 
2372
     * OPTIONAL pointer to struct containing tuple description
 
2373
     *
 
2374
     * tuple_desc is for use when returning tuples (i.e. composite data types)
 
2375
     * and is only needed if you are going to build the tuples with
 
2376
     * heap_formtuple() rather than with BuildTupleFromCStrings().  Note that
 
2377
     * the TupleDesc pointer stored here should usually have been run through
 
2378
     * BlessTupleDesc() first.
 
2379
     */
 
2380
    TupleDesc tuple_desc;
 
2381
 
 
2382
} FuncCallContext;
 
2383
</programlisting>
 
2384
    </para>
 
2385
 
 
2386
    <para>
 
2387
     An <acronym>SRF</> uses several functions and macros that
 
2388
     automatically manipulate the <structname>FuncCallContext</>
 
2389
     structure (and expect to find it via <literal>fn_extra</>).  Use
 
2390
<programlisting>
 
2391
SRF_IS_FIRSTCALL()
 
2392
</programlisting>
 
2393
     to determine if your function is being called for the first or a
 
2394
     subsequent time. On the first call (only) use
 
2395
<programlisting>
 
2396
SRF_FIRSTCALL_INIT()
 
2397
</programlisting>
 
2398
     to initialize the <structname>FuncCallContext</>. On every function call,
 
2399
     including the first, use
 
2400
<programlisting>
 
2401
SRF_PERCALL_SETUP()
 
2402
</programlisting>
 
2403
     to properly set up for using the <structname>FuncCallContext</>
 
2404
     and clearing any previously returned data left over from the
 
2405
     previous pass.
 
2406
    </para>
 
2407
 
 
2408
    <para>
 
2409
     If your function has data to return, use
 
2410
<programlisting>
 
2411
SRF_RETURN_NEXT(funcctx, result)
 
2412
</programlisting>
 
2413
     to return it to the caller.  (<literal>result</> must be of type
 
2414
     <type>Datum</>, either a single value or a tuple prepared as
 
2415
     described above.)  Finally, when your function is finished
 
2416
     returning data, use
 
2417
<programlisting>
 
2418
SRF_RETURN_DONE(funcctx)
 
2419
</programlisting>
 
2420
     to clean up and end the <acronym>SRF</>.
 
2421
    </para>
 
2422
 
 
2423
    <para>
 
2424
     The memory context that is current when the <acronym>SRF</> is called is
 
2425
     a transient context that will be cleared between calls.  This means
 
2426
     that you do not need to call <function>pfree</> on everything
 
2427
     you allocated using <function>palloc</>; it will go away anyway.  However, if you want to allocate
 
2428
     any data structures to live across calls, you need to put them somewhere
 
2429
     else.  The memory context referenced by
 
2430
     <structfield>multi_call_memory_ctx</> is a suitable location for any
 
2431
     data that needs to survive until the <acronym>SRF</> is finished running.  In most
 
2432
     cases, this means that you should switch into
 
2433
     <structfield>multi_call_memory_ctx</> while doing the first-call setup.
 
2434
    </para>
 
2435
 
 
2436
    <para>
 
2437
     A complete pseudo-code example looks like the following:
 
2438
<programlisting>
 
2439
Datum
 
2440
my_set_returning_function(PG_FUNCTION_ARGS)
 
2441
{
 
2442
    FuncCallContext  *funcctx;
 
2443
    Datum             result;
 
2444
    MemoryContext     oldcontext;
 
2445
    <replaceable>further declarations as needed</replaceable>
 
2446
 
 
2447
    if (SRF_IS_FIRSTCALL())
 
2448
    {
 
2449
        funcctx = SRF_FIRSTCALL_INIT();
 
2450
        oldcontext = MemoryContextSwitchTo(funcctx-&gt;multi_call_memory_ctx);
 
2451
        /* One-time setup code appears here: */
 
2452
        <replaceable>user code</replaceable>
 
2453
        <replaceable>if returning composite</replaceable>
 
2454
            <replaceable>build TupleDesc, and perhaps AttInMetadata</replaceable>
 
2455
        <replaceable>endif returning composite</replaceable>
 
2456
        <replaceable>user code</replaceable>
 
2457
        MemoryContextSwitchTo(oldcontext);
 
2458
    }
 
2459
 
 
2460
    /* Each-time setup code appears here: */
 
2461
    <replaceable>user code</replaceable>
 
2462
    funcctx = SRF_PERCALL_SETUP();
 
2463
    <replaceable>user code</replaceable>
 
2464
 
 
2465
    /* this is just one way we might test whether we are done: */
 
2466
    if (funcctx-&gt;call_cntr &lt; funcctx-&gt;max_calls)
 
2467
    {
 
2468
        /* Here we want to return another item: */
 
2469
        <replaceable>user code</replaceable>
 
2470
        <replaceable>obtain result Datum</replaceable>
 
2471
        SRF_RETURN_NEXT(funcctx, result);
 
2472
    }
 
2473
    else
 
2474
    {
 
2475
        /* Here we are done returning items and just need to clean up: */
 
2476
        <replaceable>user code</replaceable>
 
2477
        SRF_RETURN_DONE(funcctx);
 
2478
    }
 
2479
}
 
2480
</programlisting>
 
2481
    </para>
 
2482
 
 
2483
    <para>
 
2484
     A complete example of a simple <acronym>SRF</> returning a composite type looks like:
 
2485
<programlisting>
 
2486
PG_FUNCTION_INFO_V1(testpassbyval);
 
2487
 
 
2488
Datum
 
2489
testpassbyval(PG_FUNCTION_ARGS)
 
2490
{
 
2491
    FuncCallContext     *funcctx;
 
2492
    int                  call_cntr;
 
2493
    int                  max_calls;
 
2494
    TupleDesc            tupdesc;
 
2495
    AttInMetadata       *attinmeta;
 
2496
 
 
2497
     /* stuff done only on the first call of the function */
 
2498
     if (SRF_IS_FIRSTCALL())
 
2499
     {
 
2500
        MemoryContext   oldcontext;
 
2501
 
 
2502
        /* create a function context for cross-call persistence */
 
2503
        funcctx = SRF_FIRSTCALL_INIT();
 
2504
 
 
2505
        /* switch to memory context appropriate for multiple function calls */
 
2506
        oldcontext = MemoryContextSwitchTo(funcctx-&gt;multi_call_memory_ctx);
 
2507
 
 
2508
        /* total number of tuples to be returned */
 
2509
        funcctx-&gt;max_calls = PG_GETARG_UINT32(0);
 
2510
 
 
2511
        /* Build a tuple description for a __testpassbyval tuple */
 
2512
        tupdesc = RelationNameGetTupleDesc("__testpassbyval");
 
2513
 
 
2514
        /*
 
2515
         * generate attribute metadata needed later to produce tuples from raw
 
2516
         * C strings
 
2517
         */
 
2518
        attinmeta = TupleDescGetAttInMetadata(tupdesc);
 
2519
        funcctx-&gt;attinmeta = attinmeta;
 
2520
 
 
2521
        MemoryContextSwitchTo(oldcontext);
 
2522
    }
 
2523
 
 
2524
    /* stuff done on every call of the function */
 
2525
    funcctx = SRF_PERCALL_SETUP();
 
2526
 
 
2527
    call_cntr = funcctx-&gt;call_cntr;
 
2528
    max_calls = funcctx-&gt;max_calls;
 
2529
    attinmeta = funcctx-&gt;attinmeta;
 
2530
 
 
2531
    if (call_cntr &lt; max_calls)    /* do when there is more left to send */
 
2532
    {
 
2533
        char       **values;
 
2534
        HeapTuple    tuple;
 
2535
        Datum        result;
 
2536
 
 
2537
        /*
 
2538
         * Prepare a values array for building the returned tuple.
 
2539
         * This should be an array of C strings which will
 
2540
         * be processed later by the type input functions.
 
2541
         */
 
2542
        values = (char **) palloc(3 * sizeof(char *));
 
2543
        values[0] = (char *) palloc(16 * sizeof(char));
 
2544
        values[1] = (char *) palloc(16 * sizeof(char));
 
2545
        values[2] = (char *) palloc(16 * sizeof(char));
 
2546
 
 
2547
        snprintf(values[0], 16, "%d", 1 * PG_GETARG_INT32(1));
 
2548
        snprintf(values[1], 16, "%d", 2 * PG_GETARG_INT32(1));
 
2549
        snprintf(values[2], 16, "%d", 3 * PG_GETARG_INT32(1));
 
2550
 
 
2551
        /* build a tuple */
 
2552
        tuple = BuildTupleFromCStrings(attinmeta, values);
 
2553
 
 
2554
        /* make the tuple into a datum */
 
2555
        result = HeapTupleGetDatum(tuple);
 
2556
 
 
2557
        /* clean up (this is not really necessary) */
 
2558
        pfree(values[0]);
 
2559
        pfree(values[1]);
 
2560
        pfree(values[2]);
 
2561
        pfree(values);
 
2562
 
 
2563
        SRF_RETURN_NEXT(funcctx, result);
 
2564
    }
 
2565
    else    /* do when there is no more left */
 
2566
    {
 
2567
        SRF_RETURN_DONE(funcctx);
 
2568
    }
 
2569
}
 
2570
</programlisting>
 
2571
 
 
2572
     The SQL code to declare this function is:
 
2573
<programlisting>
 
2574
CREATE TYPE __testpassbyval AS (f1 integer, f2 integer, f3 integer);
 
2575
 
 
2576
CREATE OR REPLACE FUNCTION testpassbyval(integer, integer) RETURNS SETOF __testpassbyval
 
2577
    AS '<replaceable>filename</>', 'testpassbyval'
 
2578
    LANGUAGE C IMMUTABLE STRICT;
 
2579
</programlisting>
 
2580
    </para>
 
2581
 
 
2582
    <para>
 
2583
     The directory <filename>contrib/tablefunc</> in the source
 
2584
     distribution contains more examples of set-returning functions.
 
2585
    </para>
 
2586
   </sect2>
 
2587
 
 
2588
   <sect2>
 
2589
    <title>Polymorphic Arguments and Return Types</title>
 
2590
 
 
2591
    <para>
 
2592
     C-language functions may be declared to accept and
 
2593
     return the polymorphic types
 
2594
     <type>anyelement</type> and <type>anyarray</type>.
 
2595
     See <xref linkend="extend-types-polymorphic"> for a more detailed explanation
 
2596
     of polymorphic functions. When function arguments or return types
 
2597
     are defined as polymorphic types, the function author cannot know
 
2598
     in advance what data type it will be called with, or
 
2599
     need to return. There are two routines provided in <filename>fmgr.h</>
 
2600
     to allow a version-1 C function to discover the actual data types
 
2601
     of its arguments and the type it is expected to return. The routines are
 
2602
     called <literal>get_fn_expr_rettype(FmgrInfo *flinfo)</> and
 
2603
     <literal>get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)</>.
 
2604
     They return the result or argument type OID, or <symbol>InvalidOid</symbol> if the
 
2605
     information is not available.
 
2606
     The structure <literal>flinfo</> is normally accessed as
 
2607
     <literal>fcinfo-&gt;flinfo</>. The parameter <literal>argnum</>
 
2608
     is zero based.
 
2609
    </para>
 
2610
 
 
2611
    <para>
 
2612
     For example, suppose we want to write a function to accept a single
 
2613
     element of any type, and return a one-dimensional array of that type:
 
2614
 
 
2615
<programlisting>
 
2616
PG_FUNCTION_INFO_V1(make_array);
 
2617
Datum
 
2618
make_array(PG_FUNCTION_ARGS)
 
2619
{
 
2620
    ArrayType  *result;
 
2621
    Oid         element_type = get_fn_expr_argtype(fcinfo-&gt;flinfo, 0);
 
2622
    Datum       element;
 
2623
    int16       typlen;
 
2624
    bool        typbyval;
 
2625
    char        typalign;
 
2626
    int         ndims;
 
2627
    int         dims[MAXDIM];
 
2628
    int         lbs[MAXDIM];
 
2629
 
 
2630
    if (!OidIsValid(element_type))
 
2631
        elog(ERROR, "could not determine data type of input");
 
2632
 
 
2633
    /* get the provided element */
 
2634
    element = PG_GETARG_DATUM(0);
 
2635
 
 
2636
    /* we have one dimension */
 
2637
    ndims = 1;
 
2638
    /* and one element */
 
2639
    dims[0] = 1;
 
2640
    /* and lower bound is 1 */
 
2641
    lbs[0] = 1;
 
2642
 
 
2643
    /* get required info about the element type */
 
2644
    get_typlenbyvalalign(element_type, &amp;typlen, &amp;typbyval, &amp;typalign);
 
2645
 
 
2646
    /* now build the array */
 
2647
    result = construct_md_array(&amp;element, ndims, dims, lbs,
 
2648
                                element_type, typlen, typbyval, typalign);
 
2649
 
 
2650
    PG_RETURN_ARRAYTYPE_P(result);
 
2651
}
 
2652
</programlisting>
 
2653
    </para>
 
2654
 
 
2655
    <para>
 
2656
     The following command declares the function
 
2657
     <function>make_array</function> in SQL:
 
2658
 
 
2659
<programlisting>
 
2660
CREATE FUNCTION make_array(anyelement) RETURNS anyarray
 
2661
    AS '<replaceable>DIRECTORY</replaceable>/funcs', 'make_array'
 
2662
    LANGUAGE C STRICT;
 
2663
</programlisting>
 
2664
 
 
2665
     Note the use of <literal>STRICT</literal>; this is essential
 
2666
     since the code is not bothering to test for a null input.
 
2667
    </para>
 
2668
   </sect2>
 
2669
  </sect1>
 
2670
 
 
2671
<!-- Keep this comment at the end of the file
 
2672
Local variables:
 
2673
mode:sgml
 
2674
sgml-omittag:nil
 
2675
sgml-shorttag:t
 
2676
sgml-minimize-attributes:nil
 
2677
sgml-always-quote-attributes:t
 
2678
sgml-indent-step:1
 
2679
sgml-indent-data:t
 
2680
sgml-parent-document:nil
 
2681
sgml-default-dtd-file:"./reference.ced"
 
2682
sgml-exposed-tags:nil
 
2683
sgml-local-catalogs:("/usr/lib/sgml/catalog")
 
2684
sgml-local-ecat-files:nil
 
2685
End:
 
2686
-->