postgresql - Function returns table based on other function's returned data -
i have function defined as:
create or replace function func_1() returns table ( column_1 text, column_2 text, -- large number of other returned columns -- ... ) $$ begin -- done here end; $$ language plpgsql;
is possible declare func_2
, use same returned table structure func_1
(data returned func_1
used internally func_2
)?
of course, copy , paste declaration, if changes in table structure returned func_1
have manually keep func_2
in sync. 1 way refactor func_1
return composite type , make func_2
return same type - possible keep using returns table
, make function depend on "seamlessly"?
as @joshua pointed out above, can set dependency between these functions exploiting fact every table , view comes free composite type:
create function f() returns table (x int) language sql 'values (1)'; create view v select * f(); create function g() returns setof v language sql 'values (2)';
g()
depends on return type of f()
. though if hoping change return type of f()
, have propagate through, you're out of luck:
create or replace function f() returns table (x int, y int) language sql 'values (3,3)';
error: cannot change return type of existing function
hint: use drop function f() first.
and then, of course:
drop function f();
error: cannot drop function f() because other objects depend on it
detail: view v depends on function f()
function g() depends on type v
hint: use drop ... cascade drop dependent objects too.
if want redefine function return without dropping completely, think you're going need create composite type.
Comments
Post a Comment