Обсуждение: plpgsql: defuault parameters and constant function parameters
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
"Roger Moloney" <ramoloney@hotmail.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
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/
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@momjian.us> http://momjian.us EnterpriseDB http://www.enterprisedb.com + If your life is a hard drive, Christ can be your backup. +