Forum OpenACS Development: Response to pl/pgsql porting style question

Collapse
Posted by Vinod Kurup on
I think when you call a function with a NULL param, pl/pgsql treats the param as type 'unknown' and matches it to any type in the function's parameter list. The problem comes when you have so many NULLs that more than 1 function would match. In that case, you can cast your NULL so that the proper function is called.

Example:

create function vk_test(integer,varchar) returns integer as ' begin raise notice ''integer first function''; return 0; end;' language 'plpgsql'; create function vk_test(varchar,integer) returns integer as ' begin raise notice ''varchar first function''; return 0; end;' language 'plpgsql'; # select vk_test(1, NULL); NOTICE: integer first function # select vk_test(NULL, 1); NOTICE: varchar first function # select vk_test('a'::varchar, NULL); NOTICE: varchar first function # select vk_test(NULL, 'a'::varchar); NOTICE: integer first function # select vk_test(NULL, NULL); ERROR: Function 'vk_test(unknown,unknown)' does not exist # select vk_test(NULL::integer, NULL::varchar); NOTICE: integer function
Using explicit casting of nulls allows you to choose the function with the signature that you want.