[PATCH] Add some documentation on how to call internal functions

From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: [PATCH] Add some documentation on how to call internal functions
Date: 2024-09-12 09:41:42
Message-ID: 90853055-5BBD-493D-91E5-721677C7C59B@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

The documentation on extending using C functions,
leaves a blank on how to call other internal Postgres functions.
This can leave first-timers hanging.

I think it’d be helpful to spend some paragraphs on discussing the DirectFunctionCall API.

Here’s an attempt (also a PR[0]).

Here’s the relevant HTML snippet for convenience:
To call another version-1 function, you can use DirectFunctionCalln(func, arg1,...,argn). This is particularly useful when you want to call functions defined in the standard internal library, by using an interface similar to their SQL signature.

Different flavors of similar macros can be found in fmgr.h. The main point though is that they expect a C function name to call as their first argument (or its Oid in some cases), and actual arguments should be supplied as Datums. They always return Datum.

For example, to call the starts_with(text, text) from C, you can search through the catalog and find out that its C implementation is based on the Datum text_starts_with(PG_FUNCTION_ARGS) function.

In fmgr.h there are also available macros the facilitate conversions between C types and Datum. For example to turn text* into Datum, you can use DatumGetTextPP(X). If your extension defines additional types, it is usually convenient to define similar macros for these types too.

I’ve also added the below example function:

PG_FUNCTION_INFO_V1(t_starts_with);

Datum
t_starts_with(PG_FUNCTION_ARGS)
{
Datum t1 = PG_GETARG_DATUM(0);
Datum t2 = PG_GETARG_DATUM(1);
bool bool_res;

Datum datum_res = DirectFunctionCall2(text_starts_with, t1, t2);
bool_res = DatumGetBool(datum_res);

PG_RETURN_BOOL(bool_res);
}
PS1: I was not sure if src/tutorial is still relevant with this part of the documentation.
If so, it needs updating too.

[0] https://github.com/Florents-Tselai/postgres/pull/1/commits/1651b7bb68e0f9c2b61e1462367295d846d253ec

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrei Lepikhov 2024-09-12 09:51:19 Re: Incremental Sort Cost Estimation Instability
Previous Message Ashutosh Bapat 2024-09-12 09:35:41 Re: PG_TEST_EXTRA and meson