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

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Add some documentation on how to call internal functions
Date: 2024-10-19 05:05:10
Message-ID: CAFj8pRAX831EiuJbxoE=K0acNNc5HJs6yqJkc1DHaeToPzaTbg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

pá 18. 10. 2024 v 22:23 odesílatel Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> napsal:

> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
> > I'll mark this patch as ready for committer
>
> I spent a little time looking at this. I agree that it's better to
> show conversion of the example function's arguments to and from text*
> rather than leaving them as Datum. I also do think that we need to
> incorporate the example into src/tutorial, if only because that
> provides a chance to test it. And indeed, testing exposed that the
> example doesn't work:
>
> $ cd src/tutorial
> $ make
> $ psql postgres
> psql (18devel)
> Type "help" for help.
>
> postgres=# \i funcs.sql
>
> psql:funcs.sql:152: ERROR: could not determine which collation to use for
> string comparison
> HINT: Use the COLLATE clause to set the collation explicitly.
>
> The problem here is that we failed to pass through the result of
> PG_GET_COLLATION() to text_starts_with. We could do that, certainly,
> for a couple more lines of code. But it feels like this is getting
> into details that obscure the main point. I wonder if it'd be better
> to choose a different example that calls a non-collation-dependent
> target function.
>

This can be a trap for some beginners too. So example of
DirectFunctionCall2Coll can be nice

<-->result = DatumGetBool(DirectFunctionCall2Coll(text_starts_with,
<--><--><--><--><--><--><--><--><--><--><--><--> DEFAULT_COLLATION_OID,
<--><--><--><--><--><--><--><--><--><--><--><--> PointerGetDatum(t1),
<--><--><--><--><--><--><--><--><--><--><--><--> PointerGetDatum(t2)));

With comment so text based functions can require collation - and simple
solution can be using DEFAULT_COLLATION_OID, and we can
introduce second example of just DirectFunctionCall

Datum
bytea_left(PG_FUNCTION_ARGS)
{
<-->bytea<-> *t = PG_GETARG_BYTEA_PP(0);
<-->int32<-><-->l = PG_GETARG_INT32(1);
<-->bytea<-> *result;

<-->result = DatumGetByteaPP(DirectFunctionCall3(bytea_substr,
<--><--><--><--><--><--><--><--><--><--><--><--> PointerGetDatum(t),
<--><--><--><--><--><--><--><--><--><--><--><--> Int32GetDatum(1),
<--><--><--><--><--><--><--><--><--><--><--><--> Int32GetDatum(l)));
<-->PG_RETURN_BYTEA_P(result);
}

>
> Anyway, proposed v3 attached. I folded the two patches into one,
> and editorialized on the text a little.
>

> regards, tom lane
>
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Joel Jacobson 2024-10-19 06:52:52 Re: [BUG FIX] Fix validation of COPY options FORCE_NOT_NULL/FORCE_NULL
Previous Message Benoit Lobréau 2024-10-19 04:46:44 Re: Logging parallel worker draught