Re: strange behavior with C function and DEFAULT function parameters

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tomas Vondra <tv(at)fuzzy(dot)cz>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: strange behavior with C function and DEFAULT function parameters
Date: 2013-10-21 00:54:44
Message-ID: 52647B54.40001@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On 10/20/2013 08:38 PM, Tomas Vondra wrote:
> Hi,
>
> I ran into some pretty strange behavior of C-language function and
> default parameter values, both on 9.2 and 9.4devel. Consider for example
> this trivial C function:
>
> Datum
> show_bug(PG_FUNCTION_ARGS) {
> elog(WARNING, "called ;-)");
> PG_RETURN_VOID();
> }
>
> which is accessed using this definition:
>
> CREATE FUNCTION show_bug(a TEXT DEFAULT NULL)
> RETURNS void
> AS 'bug.so'
> LANGUAGE C STRICT;
>
> and let's try various calls:
>
> db=# SELECT show_bug('a');
> WARNING: called ;-)
> show_bug
> ----------
>
> (1 row)
>
> Seems ok. Now let's use the default value:
>
> db=# SELECT show_bug();
> show_bug
> ----------
>
> (1 row)
>
> db=# SELECT show_bug(NULL);
> show_bug
> ----------
>
> (1 row)
>
> Well, seems quite strange to me - it seems as if the function is called,
> but apparently it's not. I can't find anything relevant in the docs.
>
> For comparison, a matching PL/pgSQL function:
>
> CREATE FUNCTION show_bug2(a TEXT DEFAULT NULL) RETURNS void AS $$
> BEGIN
> RAISE WARNING 'called ;-)';
> END;
> $$ LANGUAGE plpgsql;
>
> which behaves exactly as expected in all three cases:
>
> db=# SELECT show_bug('a');
> WARNING: called ;-)
> show_bug
> ----------
>
> (1 row)
>
> db=# SELECT show_bug();
> WARNING: called ;-)
> show_bug
> ----------
>
> (1 row)
> db=# SELECT show_bug(NULL);
> WARNING: called ;-)
> show_bug
> ----------
>
> (1 row)
>
> So, what I'm doing wrong? Seems like a bug to me ...
>

It's not a bug, it's expected. STRICT functions are not called with NULL
inputs - the result of the function is instead taken as NULL.

cheers

andrew

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Noah Misch 2013-10-21 01:10:13 Re: signed vs. unsigned in TYPEALIGN (was Re: space reserved for WAL record does not match what was written: panic on windows)
Previous Message Tomas Vondra 2013-10-21 00:49:08 Re: strange behavior with C function and DEFAULT function parameters