plpgsql: defuault parameters and constant function parameters

Lists: pgsql-interfaces
From: "Roger Moloney" <ramoloney(at)hotmail(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: plpgsql: defuault parameters and constant function parameters
Date: 2007-08-28 12:31:57
Message-ID: BAY115-F27A40D39CE453718AA0380AFD30@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Hi,

Having just done the first draft of a large migration from informix to
postgres, I wanted to point out that the migration was hugely complicated by
postgres inability to define default parameters and to modify function
parameters which were not output.

Consider a simple function like (in informix):

// informix procedure
create procedure p_AddItem (pItemName char(50),
pItemDescription char(100),
pX integer
DEFAULT 1,
pY char
DEFAULT 'x',
pZ smallint)
returning INTEGER;
define vItemRef integer;
begin
-- Do input error checks
/* checks on pX, pY, pZ */

-- Add the item and return its reference
insert item (item_name, item_description) values (pItemName,
pItemDescription);
let vItemRef = DBINFO('sqca.sqerrd1');
return vItemRef;
end

and it's postgres equivalent:

// postgres function
create or replace function p_AddItem (pItemName char(50),
pItemDescription
char(100),
pX int /*
DEFAULT not possible */,
pY char /* DEFAULT
not possible */,
pZ smallint)
returns INTEGER as
$$
declare
vItemRef integer;
vX integer DEFAULT coalesce(pX,1);
vY char DEFAULT coalesce(pY,'x');
vZ smallint DEFAULT coalesce(pZ,'');

begin
-- Do input error checks
/* checks on vX, vY, vZ */

-- Add the item and return its reference
insert into item (item_name, item_description) values (pItemName,
pItemDescription);
vItemRef := currval('item_item_ref_seq');
return vItemRef;
end
$$
language 'plpgsql';

As you can see there are a lot of changes to make. Not too nice when you are
dealing with hundreds of procedures. This would be greatly easier if:

1) postgres could do DEFAULT parameters.
I know this probably would not happen as it would make functions
harder to recognise and
may not suit postgres polymorphic functions.

2) postgres would allow function parameters to be non-constant without
declaring them as OUT.
I dont want to make them output parameters as they are not output
parameters. I am returning different output parameters. However it would be
great if I could modify (and not pass back) the value of a input parameter.
Perhaps this is *not* allowed for performance reasons ? But it was a huge
factor in the migration and is still causing bother to me as almost every
single parameter needs to be refactored (i.e. put in a coalesce statement
for it and then refactor the name throughout the function body) as almost
every paraemter is DEFAULTED in informix or modified in the informix
function body.

I can't stress how difficult this made the migration. As mentioned there
were hundreds of procedures and some have 40 parameters in them.

Any chance of allowing input parameters to be modified within the function
body ?

_________________________________________________________________
Download the latest version of Windows Live Messenger NOW!
http://get.live.com/en-ie/messenger/overview


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Roger Moloney" <ramoloney(at)hotmail(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: plpgsql: defuault parameters and constant function parameters
Date: 2007-08-30 00:19:23
Message-ID: 13701.1188433163@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

"Roger Moloney" <ramoloney(at)hotmail(dot)com> writes:
> Having just done the first draft of a large migration from informix to
> postgres, I wanted to point out that the migration was hugely complicated by
> postgres inability to define default parameters

You can get that effect using function name overloading. For instance

create function f(x int, y int) ...

create function f(x int) ... as 'select f(x, 1)' ...

It's easier if the defaultable parameters are at the end, but I rather
wonder how your informix system is disambiguating the calls either
with a signature like that. If you leave off just one of the two
defaultable parameters, how does it know which?

> I dont want to make them output parameters as they are not output
> parameters. I am returning different output parameters. However it would be
> great if I could modify (and not pass back) the value of a input parameter.

Just use a differently named local variable, perhaps? Actually, because
of the scoping rules it seems to me to work fine even if they have the
same name:

regression=# create function ff(f1 int) returns int as $$
declare f1 int := coalesce(f1, 1);
begin
return f1;
end$$ language plpgsql;
CREATE FUNCTION
regression=# select ff(3);
ff
----
3
(1 row)

regression=# select ff(null);
ff
----
1
(1 row)

> Any chance of allowing input parameters to be modified within the function
> body ?

That check is there to prevent mistakes, and I think it's a good one.

regards, tom lane


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-interfaces(at)postgresql(dot)org
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Roger Moloney" <ramoloney(at)hotmail(dot)com>
Subject: Re: plpgsql: defuault parameters and constant function parameters
Date: 2007-09-18 21:25:12
Message-ID: 200709182325.12677.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Tom Lane wrote:
> It's easier if the defaultable parameters are at the end, but I
> rather wonder how your informix system is disambiguating the calls
> either with a signature like that.  If you leave off just one of the
> two defaultable parameters, how does it know which?

Several other SQL systems allow naming the parameters in calls, such as

CALL foo(a = 1, b = 2)

That's the only way to stay sane with function having 20 or more
parameters anyway.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-interfaces(at)postgresql(dot)org, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Roger Moloney <ramoloney(at)hotmail(dot)com>
Subject: Re: plpgsql: defuault parameters and constant function parameters
Date: 2007-09-18 22:11:47
Message-ID: 200709182211.l8IMBl527174@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-interfaces

Peter Eisentraut wrote:
> Tom Lane wrote:
> > It's easier if the defaultable parameters are at the end, but I
> > rather wonder how your informix system is disambiguating the calls
> > either with a signature like that. ?If you leave off just one of the
> > two defaultable parameters, how does it know which?
>
> Several other SQL systems allow naming the parameters in calls, such as
>
> CALL foo(a = 1, b = 2)
>
> That's the only way to stay sane with function having 20 or more
> parameters anyway.

TODO has:

o Allow function parameters to be passed by name,
get_employee_salary(12345 AS emp_id, 2001 AS tax_year)

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +