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