Pavel Stehule <pavel.stehule@gmail.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