Re: Time to drop old-style (V0) functions?

From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Time to drop old-style (V0) functions?
Date: 2017-03-07 14:50:37
Views: Raw Message | Whole Thread | Download mbox
Lists: pgsql-hackers

I think we have consensus to go ahead with this, and the patches are
mostly mechanical, so I only have a few comments on style and one
possible bug below:


static int restore(char *s, float val, int n);

* Input/Output functions


doesn't seem like the right whitespace change

@@ -359,13 +360,14 @@ gseg_picksplit(GistEntryVector *entryvec,
* Emit segments to the left output page, and compute its bounding box.
- datum_l = (SEG *) palloc(sizeof(SEG));
- memcpy(datum_l, sort_items[0].data, sizeof(SEG));
+ datum_l = PointerGetDatum(palloc(sizeof(SEG)));
+ memcpy(DatumGetPointer(datum_l), sort_items[0].data, sizeof(SEG));

There would be a little bit less back-and-forth here if you kept
datum_l and datum_r of type SEG *.

case RTOverlapStrategyNumber:
- retval = (bool) seg_overlap(key, query);
+ retval =
+ !DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));

Accidentally flipped the logic?

-seg_contains(SEG *a, SEG *b)
- return ((a->lower <= b->lower) && (a->upper >= b->upper));
+ PG_RETURN_BOOL((a->lower <= b->lower) && (a->upper >= b->upper));

-seg_contained(SEG *a, SEG *b)
- return (seg_contains(b, a));
+ DirectFunctionCall2(seg_contains,

Maybe in seg_contained also assign the arguments to a and b, so it's
easier to see the relationship between contains and contained.

-seg_same(SEG *a, SEG *b)
- return seg_cmp(a, b) == 0;
+ Datum cmp =
+ DirectFunctionCall2(seg_cmp, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1));
+ PG_RETURN_BOOL(DatumGetInt32(cmp) == 0);

I would write this as

int32 cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1));

Having a variable of type Datum is a bit meaningless.

Oh, I see you did it that way later in seg_lt() etc., so same here.


diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml
index 255bfddad7..cd41b89136 100644
--- a/doc/src/sgml/xfunc.sgml
+++ b/doc/src/sgml/xfunc.sgml
@@ -675,7 +675,7 @@ CREATE FUNCTION mleast(VARIADIC arr numeric[]) RETURNS numeric AS $$

SELECT mleast(10, -1, 5, 4.4);
- mleast
+ mleast
(1 row)

These changes are neither right nor wrong, but we have had previous
discussions about this and settled on leaving the whitespace as psql
outputs it. In any case it seems unrelated here.

- Two different calling conventions are currently used for C functions.
- The newer <quote>version 1</quote> calling convention is indicated by writing
- a <literal>PG_FUNCTION_INFO_V1()</literal> macro call for the function,
- as illustrated below. Lack of such a macro indicates an old-style
- (<quote>version 0</quote>) function. The language name specified in <command>CREATE FUNCTION</command>
- is <literal>C</literal> in either case. Old-style functions are now deprecated
- because of portability problems and lack of functionality, but they
- are still supported for compatibility reasons.
+ Currently only one calling convention is used for C functions
+ (<quote>version 1</quote>). Support for that calling convention is
+ indicated by writing a <literal>PG_FUNCTION_INFO_V1()</literal> macro
+ call for the function, as illustrated below.

extra blank line

@@ -1655,8 +1652,8 @@ CREATE FUNCTION square_root(double precision) RETURNS double precision
If the name starts with the string <literal>$libdir</literal>,
that part is replaced by the <productname>PostgreSQL</> package
- library directory
- name, which is determined at build time.<indexterm><primary>$libdir</></>
+ library directory name, which is determined at build time.
+ <indexterm><primary>$libdir</></>


@@ -455,9 +394,12 @@ fetch_finfo_record(void *filehandle, char *funcname)
if (infofunc == NULL)
- /* Not found --- assume version 0 */
- pfree(infofuncname);
- return &default_inforec;
+ ereport(ERROR,
+ errmsg("could not find function information for function \"%s\"",
+ funcname),
+ errhint("SQL-callable functions need an accompanying PG_FUNCTION_INFO_V1(funcname).")));
+ return NULL; /* silence compiler */

/* Found, so call it */

Perhaps plug in the actual C function name into the error message, like

errhint("SQL-callable functions need an accompanying PG_FUNCTION_INFO_V1(%s).", funcname)

@@ -270,14 +269,16 @@ widget_in(char *str)
result->center.y = atof(coord[1]);
result->radius = atof(coord[2]);

- return result;
+ PG_RETURN_DATUM(PointerGetDatum(result));


Attached is a patch for src/backend/utils/fmgr/README that edits out the
transitional comments and just keeps the parts still relevant.

Tests pass for me. So this is mostly ready to go AFAICS.

Peter Eisentraut
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachment Content-Type Size
fmgr-README.patch text/x-patch 12.8 KB

In response to


Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2017-03-07 15:24:54 Re: Need a builtin way to run all tests faster manner
Previous Message Tomas Vondra 2017-03-07 14:46:57 Re: PATCH: two slab-like memory allocators