1
1
<!--
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 $
3
3
-->
4
4
5
5
<chapter id="xfunc">
@@ -434,10 +434,9 @@ SELECT clean_EMP();
434
434
functions that will be loaded into Postgres. The "Defined In"
435
435
column gives the actual header file (in the
436
436
<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>.
441
440
442
441
<table tocentry="1">
443
442
<title>Equivalent C Types
@@ -619,9 +618,8 @@ SELECT clean_EMP();
619
618
620
619
<para>
621
620
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
625
623
to define your types such that they will be the same
626
624
size (in bytes) on all architectures. For example, the
627
625
<literal>long</literal> type is dangerous because it
@@ -657,7 +655,9 @@ typedef struct
657
655
them in and out of <productname>Postgres</productname> functions.
658
656
To return a value of such a type, allocate the right amount of
659
657
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.)
661
661
</para>
662
662
663
663
<para>
@@ -721,8 +721,8 @@ memmove(destination->data, buffer, 40);
721
721
Here are some examples:
722
722
723
723
<programlisting>
724
- #include <string.h>
725
724
#include "postgres.h"
725
+ #include <string.h>
726
726
727
727
/* By Value */
728
728
@@ -780,10 +780,10 @@ concat_text(text *arg1, text *arg2)
780
780
int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
781
781
text *new_text = (text *) palloc(new_text_size);
782
782
783
- memset((void *) new_text, 0, new_text_size);
784
783
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);
787
787
return new_text;
788
788
}
789
789
</programlisting>
@@ -882,8 +882,8 @@ PG_FUNCTION_INFO_V1(funcname);
882
882
Here we show the same functions as above, coded in version-1 style:
883
883
884
884
<programlisting>
885
- #include <string.h>
886
885
#include "postgres.h"
886
+ #include <string.h>
887
887
#include "fmgr.h"
888
888
889
889
/* By Value */
@@ -945,7 +945,7 @@ copytext(PG_FUNCTION_ARGS)
945
945
*/
946
946
memcpy((void *) VARDATA(new_t), /* destination */
947
947
(void *) VARDATA(t), /* source */
948
- VARSIZE(t)-VARHDRSZ); /* how many bytes */
948
+ VARSIZE(t)-VARHDRSZ); /* how many bytes */
949
949
PG_RETURN_TEXT_P(new_t);
950
950
}
951
951
@@ -959,10 +959,10 @@ concat_text(PG_FUNCTION_ARGS)
959
959
int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
960
960
text *new_text = (text *) palloc(new_text_size);
961
961
962
- memset((void *) new_text, 0, new_text_size);
963
962
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);
966
966
PG_RETURN_TEXT_P(new_text);
967
967
}
968
968
</programlisting>
@@ -991,10 +991,20 @@ concat_text(PG_FUNCTION_ARGS)
991
991
</para>
992
992
993
993
<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
998
1008
procedural-language call handlers. Version-1 code is also more
999
1009
portable than version-0, because it does not break ANSI C restrictions
1000
1010
on function call protocol. For more details see
@@ -1167,11 +1177,14 @@ LANGUAGE 'c';
1167
1177
<listitem>
1168
1178
<para>
1169
1179
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
1171
1181
manager interfaces (<symbol>PG_FUNCTION_ARGS</symbol>, etc.)
1172
1182
are 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>,
1175
1188
<filename>elog.h</filename> and <filename>palloc.h</filename>
1176
1189
for you.
1177
1190
</para>
@@ -1210,7 +1223,7 @@ LANGUAGE 'c';
1210
1223
<title>Function Overloading</title>
1211
1224
1212
1225
<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
1214
1227
the arguments they take are different. In other words, function names
1215
1228
can be <firstterm>overloaded</firstterm>.
1216
1229
A function may also have the same name as an attribute. In the case