@@ -2384,6 +2384,56 @@ PG_FUNCTION_INFO_V1(funcname);
23842384 takes as its argument the actual value to return.
23852385 </para>
23862386
2387+ <para>
2388+ To call another version-1 function, you can use
2389+ <function>DirectFunctionCall<replaceable>n</replaceable>(func,
2390+ arg1, ..., argn)</function>. This is particularly useful when you want
2391+ to call functions defined in the standard internal library, by using an
2392+ interface similar to their SQL signature.
2393+ </para>
2394+
2395+ <para>
2396+ These convenience functions and similar ones can be found
2397+ in <filename>fmgr.h</filename>.
2398+ The <function>DirectFunctionCall<replaceable>n</replaceable></function>
2399+ family expect a C function name as their first argument. There are also
2400+ <function>OidFunctionCall<replaceable>n</replaceable></function> which
2401+ take the OID of the target function, and some other variants. All of
2402+ these expect the function's arguments to be supplied
2403+ as <type>Datum</type>s, and likewise they return <type>Datum</type>.
2404+ Note that neither arguments nor result are allowed to be NULL when
2405+ using these convenience functions.
2406+ </para>
2407+
2408+ <para>
2409+ For example, to call the <function>starts_with(text, text)</function>
2410+ function from C, you can search through the catalog and find out that
2411+ its C implementation is the
2412+ <function>Datum text_starts_with(PG_FUNCTION_ARGS)</function>
2413+ function. Typically you would
2414+ use <literal>DirectFunctionCall2(text_starts_with, ...)</literal> to
2415+ call such a function. However, <function>starts_with(text,
2416+ text)</function> requires collation information, so it will fail
2417+ with <quote>could not determine which collation to use for string
2418+ comparison</quote> if called that way. Instead you must
2419+ use <literal>DirectFunctionCall2Coll(text_starts_with, ...)</literal>
2420+ and provide the desired collation, which typically is just passed
2421+ through from <function>PG_GET_COLLATION()</function>, as shown in the
2422+ example below.
2423+ </para>
2424+
2425+ <para>
2426+ <filename>fmgr.h</filename> also supplies macros that facilitate
2427+ conversions between C types and <type>Datum</type>. For example to
2428+ turn <type>Datum</type> into <type>text*</type>, you can
2429+ use <function>DatumGetTextPP(X)</function>. While some types have macros
2430+ named like <function>TypeGetDatum(X)</function> for the reverse
2431+ conversion, <type>text*</type> does not; it's sufficient to use the
2432+ generic macro <function>PointerGetDatum(X)</function> for that.
2433+ If your extension defines additional types, it is usually convenient to
2434+ define similar macros for your types too.
2435+ </para>
2436+
23872437 <para>
23882438 Here are some examples using the version-1 calling convention:
23892439 </para>
@@ -2482,6 +2532,25 @@ concat_text(PG_FUNCTION_ARGS)
24822532 memcpy(VARDATA(new_text) + arg1_size, VARDATA_ANY(arg2), arg2_size);
24832533 PG_RETURN_TEXT_P(new_text);
24842534}
2535+
2536+ /* A wrapper around starts_with(text, text) */
2537+
2538+ PG_FUNCTION_INFO_V1(t_starts_with);
2539+
2540+ Datum
2541+ t_starts_with(PG_FUNCTION_ARGS)
2542+ {
2543+ text *t1 = PG_GETARG_TEXT_PP(0);
2544+ text *t2 = PG_GETARG_TEXT_PP(1);
2545+ Oid collid = PG_GET_COLLATION();
2546+ bool result;
2547+
2548+ result = DatumGetBool(DirectFunctionCall2Coll(text_starts_with,
2549+ collid,
2550+ PointerGetDatum(t1),
2551+ PointerGetDatum(t2)));
2552+ PG_RETURN_BOOL(result);
2553+ }
24852554]]>
24862555</programlisting>
24872556
@@ -2513,6 +2582,10 @@ CREATE FUNCTION copytext(text) RETURNS text
25132582CREATE FUNCTION concat_text(text, text) RETURNS text
25142583 AS '<replaceable>DIRECTORY</replaceable>/funcs', 'concat_text'
25152584 LANGUAGE C STRICT;
2585+
2586+ CREATE FUNCTION t_starts_with(text, text) RETURNS boolean
2587+ AS '<replaceable>DIRECTORY</replaceable>/funcs', 't_starts_with'
2588+ LANGUAGE C STRICT;
25162589</programlisting>
25172590
25182591 <para>
0 commit comments