Re: Another issue in default-values patch: defaults expanded too soon

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Another issue in default-values patch: defaults expanded too soon
Date: 2008-12-16 22:43:32
Message-ID: 16814.1229467412@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

And while we're on the subject ... as the patch stands, it lets you
provide defaults for polymorphic parameters, as in

regression=# create function eq (f1 anyelement, f2 anyelement default 42) returns bool as 'select $1 = $2' language sql;
CREATE FUNCTION
regression=# select eq(now(),now());
eq
----
t
(1 row)

regression=# select eq(now());
ERROR: arguments declared "anyelement" are not all alike
DETAIL: timestamp with time zone versus integer
regression=# select eq(11,12);
eq
----
f
(1 row)

regression=# select eq(11);
eq
----
f
(1 row)

regression=# select eq(42);
eq
----
t
(1 row)

The reason this is pretty wacko is that changing the default can change the
set of calls the function can match:

regression=# create or replace function eq (f1 anyelement, f2 anyelement default now()) returns bool as 'select $1 = $2' language sql;
CREATE FUNCTION
regression=# select eq(now());
eq
----
t
(1 row)

regression=# select eq(42);
ERROR: arguments declared "anyelement" are not all alike
DETAIL: integer versus timestamp with time zone

In fact, it's worse than that: changing the default can change the
resolved output type of the function.

regression=# create function dupl (f1 anyelement default 42) returns anyelement
as 'select $1' language sql;
CREATE FUNCTION
regression=# select dupl();
dupl
------
42
(1 row)

regression=# create or replace function dupl (f1 anyelement default now()) returns anyelement
as 'select $1' language sql;
CREATE FUNCTION
regression=# select dupl();
dupl
-------------------------------
2008-12-16 17:39:41.418412-05
(1 row)

Isn't *that* special. Needless to say, this would utterly break any
view or rule referencing the function.

I'm inclined to think we should forbid defaults for polymorphic
parameters, and wondered if anyone can come up with an actually useful
use-case that'd require it. If we were going to allow it, we'd at least
have to restrict things enough so that the resolved output type couldn't
change.

(The reason I stumbled across this was that the current behavior is an
artifact of inserting the default expressions at parse time; in fact,
before resolving polymorphic types. It would get very much more painful
to support any sort of behavior along this line if we don't do that.)

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Josh Berkus 2008-12-16 23:04:56 Re: Another issue in default-values patch: defaults expanded too soon
Previous Message Emmanuel Cecchet 2008-12-16 22:16:40 [Fwd: Re: Transactions and temp tables]