11<!--
2- $Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.30 2001/01/22 16:11:17 tgl Exp $
2+ $Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.31 2001/02/15 19:03:35 tgl Exp $
33-->
44
55 <chapter id="xfunc">
@@ -434,10 +434,9 @@ SELECT clean_EMP();
434434 functions that will be loaded into Postgres. The "Defined In"
435435 column gives the actual header file (in the
436436 <filename>.../src/backend/</filename>
437- directory) that the equivalent C type is defined. However, if you
438- include <filename>utils/builtins.h</filename>,
439- these files will automatically be
440- included.
437+ directory) that the equivalent C type is defined. Note that you should
438+ always include <filename>postgres.h</filename> first, and that in turn
439+ includes <filename>c.h</filename>.
441440
442441 <table tocentry="1">
443442 <title>Equivalent C Types
@@ -619,9 +618,8 @@ SELECT clean_EMP();
619618
620619 <para>
621620 By-value types can only be 1, 2 or 4 bytes in length
622- (even if your computer supports by-value types of other
623- sizes). <productname>Postgres</productname> itself
624- only passes integer types by value. You should be careful
621+ (also 8 bytes, if sizeof(Datum) is 8 on your machine).
622+ You should be careful
625623 to define your types such that they will be the same
626624 size (in bytes) on all architectures. For example, the
627625 <literal>long</literal> type is dangerous because it
@@ -657,7 +655,9 @@ typedef struct
657655 them in and out of <productname>Postgres</productname> functions.
658656 To return a value of such a type, allocate the right amount of
659657 memory with <literal>palloc()</literal>, fill in the allocated memory,
660- and return a pointer to it.
658+ and return a pointer to it. (Alternatively, you can return an input
659+ value of the same type by returning its pointer. <emphasis>Never</>
660+ modify the contents of a pass-by-reference input value, however.)
661661 </para>
662662
663663 <para>
@@ -721,8 +721,8 @@ memmove(destination->data, buffer, 40);
721721 Here are some examples:
722722
723723<programlisting>
724- #include <string.h>
725724#include "postgres.h"
725+ #include <string.h>
726726
727727/* By Value */
728728
@@ -780,10 +780,10 @@ concat_text(text *arg1, text *arg2)
780780 int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
781781 text *new_text = (text *) palloc(new_text_size);
782782
783- memset((void *) new_text, 0, new_text_size);
784783 VARATT_SIZEP(new_text) = new_text_size;
785- strncpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
786- strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
784+ memcpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
785+ memcpy(VARDATA(new_text) + (VARSIZE(arg1)-VARHDRSZ),
786+ VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
787787 return new_text;
788788}
789789</programlisting>
@@ -882,8 +882,8 @@ PG_FUNCTION_INFO_V1(funcname);
882882 Here we show the same functions as above, coded in version-1 style:
883883
884884<programlisting>
885- #include <string.h>
886885#include "postgres.h"
886+ #include <string.h>
887887#include "fmgr.h"
888888
889889/* By Value */
@@ -945,7 +945,7 @@ copytext(PG_FUNCTION_ARGS)
945945 */
946946 memcpy((void *) VARDATA(new_t), /* destination */
947947 (void *) VARDATA(t), /* source */
948- VARSIZE(t)-VARHDRSZ); /* how many bytes */
948+ VARSIZE(t)-VARHDRSZ); /* how many bytes */
949949 PG_RETURN_TEXT_P(new_t);
950950}
951951
@@ -959,10 +959,10 @@ concat_text(PG_FUNCTION_ARGS)
959959 int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
960960 text *new_text = (text *) palloc(new_text_size);
961961
962- memset((void *) new_text, 0, new_text_size);
963962 VARATT_SIZEP(new_text) = new_text_size;
964- strncpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
965- strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
963+ memcpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
964+ memcpy(VARDATA(new_text) + (VARSIZE(arg1)-VARHDRSZ),
965+ VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
966966 PG_RETURN_TEXT_P(new_text);
967967}
968968</programlisting>
@@ -991,10 +991,20 @@ concat_text(PG_FUNCTION_ARGS)
991991 </para>
992992
993993 <para>
994- The version-1 function call conventions also make it possible to
995- test for NULL inputs to a non-strict function, return a NULL
996- result (from either strict or non-strict functions), return
997- <quote>set</quote> results, and implement trigger functions and
994+ One big improvement in version-1 functions is better handling of NULL
995+ inputs and results. The macro <function>PG_ARGISNULL(n)</function>
996+ allows a function to test whether each input is NULL (of course, doing
997+ this is only necessary in functions not declared <quote>strict</>).
998+ As with the
999+ <function>PG_GETARG_<replaceable>xxx</replaceable>()</function> macros,
1000+ the input arguments are counted beginning at zero.
1001+ To return a NULL result, execute <function>PG_RETURN_NULL()</function>;
1002+ this works in both strict and non-strict functions.
1003+ </para>
1004+
1005+ <para>
1006+ The version-1 function call conventions make it possible to
1007+ return <quote>set</quote> results and implement trigger functions and
9981008 procedural-language call handlers. Version-1 code is also more
9991009 portable than version-0, because it does not break ANSI C restrictions
10001010 on function call protocol. For more details see
@@ -1167,11 +1177,14 @@ LANGUAGE 'c';
11671177 <listitem>
11681178 <para>
11691179 Most of the internal <productname>Postgres</productname> types
1170- are declared in <filename>postgres.h</filename>, the function
1180+ are declared in <filename>postgres.h</filename>,while the function
11711181manager interfaces (<symbol>PG_FUNCTION_ARGS</symbol>, etc.)
11721182are in <filename>fmgr.h</filename>, so you will need to
1173- include at least these two files. Including
1174- <filename>postgres.h</filename> will also include
1183+ include at least these two files. For portability reasons it's best
1184+ to include <filename>postgres.h</filename> <emphasis>first</>,
1185+ before any other system or user header files.
1186+ Including <filename>postgres.h</filename> will also include
1187+ <filename>c.h</filename>,
11751188<filename>elog.h</filename> and <filename>palloc.h</filename>
11761189for you.
11771190 </para>
@@ -1210,7 +1223,7 @@ LANGUAGE 'c';
12101223 <title>Function Overloading</title>
12111224
12121225 <para>
1213- More than one function may be defined with the same name,as long as
1226+ More than one function may be defined with the same name,so long as
12141227 the arguments they take are different. In other words, function names
12151228 can be <firstterm>overloaded</firstterm>.
12161229 A function may also have the same name as an attribute. In the case