Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread David G. Johnston
On Thu, Jun 18, 2015 at 12:00 PM, Sven Geggus wrote: > David G. Johnston wrote: > > > Assuming you are on 9.3+ what you want to use is LATERAL > > OK, how is such a query supposed to look like? > > assuming "select myfunc(col1,col2) from mytable" works as the inner select? > ​Syntax, descriptio

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Sven Geggus
David G. Johnston wrote: > Assuming you are on 9.3+ what you want to use is LATERAL OK, how is such a query supposed to look like? assuming "select myfunc(col1,col2) from mytable" works as the inner select? Sven -- Software patents are the software project equivalent of land mines: Each desi

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Alvaro Herrera
Sven Geggus wrote: > Using your suggestion the desired two columns are generated, but I consider > this a little bit ugly: > > mydb=> WITH exec_func AS ( select myfunc(col1,col2) from mytable ) > SELECT (exec_func.myfunc).* FROM exec_func; > HINWEIS: called with parms foo,bar: text1 value1 > HIN

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread David G. Johnston
On Thu, Jun 18, 2015 at 10:31 AM, Sven Geggus wrote: > David G. Johnston wrote: > > WITH exec_func AS ( SELECT myfunc(col1,col2) FROM mytable ) > > SELECT (exec_func.myfunc).* FROM exec_func; > > > > This relies on the fact that currently a CTE introduces an optimization > > barrier. > > Hm, let

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Sven Geggus
David G. Johnston wrote: > WITH exec_func AS ( SELECT myfunc(col1,col2) FROM mytable ) > SELECT (exec_func.myfunc).* FROM exec_func; > > This relies on the fact that currently a CTE introduces an optimization > barrier. Hm, let me summarize. My function seems to work as expected and is only call

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread David G. Johnston
On Thu, Jun 18, 2015 at 9:52 AM, Sven Geggus wrote: > Raymond O'Donnell wrote: > > >> mydb=> select myfunc('foo','bar'); > > > > You need to do: > > > >select * from myfunc('foo','bar'); > > This has been a misguided example. Reality should more likely look like > this: > > select myfunc(col

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Sven Geggus
Raymond O'Donnell wrote: >> mydb=> select myfunc('foo','bar'); > > You need to do: > >select * from myfunc('foo','bar'); This has been a misguided example. Reality should more likely look like this: select myfunc(col1,col2) from mytable; And it would of course be undesired if myfunc woul

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread David G. Johnston
On Thu, Jun 18, 2015 at 9:32 AM, Sven Geggus wrote: > David G. Johnston wrote: > > > Look at the "returns table (col1 type, col2 type)" form. > > If I got this right "returns table" is not what I want as I need to select > from my function as a virtual table in this case. > ​Yes, I mis-read you

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Sven Geggus
Pavel Stehule wrote: > CREATE OR REPLACE FUNCTION public.fx(i integer, OUT a text, OUT b text) > RETURNS SETOF record > LANGUAGE plpgsql > AS $function$ > BEGIN > RETURN QUERY SELECT 'foo'||id, 'bar'||id FROM generate_series(1,i) g(id); > RETURN; > END; > $function$ I'm afraid I will almos

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Sven Geggus
David G. Johnston wrote: > Look at the "returns table (col1 type, col2 type)" form. If I got this right "returns table" is not what I want as I need to select from my function as a virtual table in this case. Regards Sven -- "Thinking of using NT for your critical apps?

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread David G. Johnston
On Thursday, June 18, 2015, Chris Travers wrote: > > > Select (myfunc('foo','bar')).*; > > > This should be avoided. Use lateral instead,or a cte a/o offset 0. My_func is evaluated twice (once per column) if called this way > > Or > Select * from myfunc('foo','bar'); > This is ok David J.

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Tom Lane
"Raymond O'Donnell" writes: > On 18/06/2015 13:36, Sven Geggus wrote: >> I would like to be able to do something like this: >> >> select myfunc('foo','bar'); >> or >> select myfunc(foo, bar) from foobartable; >> or even >> select myfunc(foo, bar), 'baz' as baz from foobartable; > You need to do:

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Merlin Moncure
On Thu, Jun 18, 2015 at 7:50 AM, Chris Travers wrote: > > On Thu, Jun 18, 2015, 14:38 Sven Geggus wrote: > > Hello, > > I supose this is simple, but I did not find a solution in the documentation. > > Because you already are returning 2 columns. > > I would like to be able to do something like th

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Raymond O'Donnell
On 18/06/2015 13:36, Sven Geggus wrote: > Hello, > > I supose this is simple, but I did not find a solution in the documentation. > > I would like to be able to do something like this: > > select myfunc('foo','bar'); > or > select myfunc(foo, bar) from foobartable; > or even > select myfunc(foo,

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Chris Travers
On Thu, Jun 18, 2015, 14:38 Sven Geggus wrote: Hello, I supose this is simple, but I did not find a solution in the documentation. Because you already are returning 2 columns. I would like to be able to do something like this: select myfunc('foo','bar'); or select myfunc(foo, bar) from foob

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread Pavel Stehule
Hi CREATE OR REPLACE FUNCTION public.fx(i integer, OUT a text, OUT b text) RETURNS SETOF record LANGUAGE plpgsql AS $function$ BEGIN RETURN QUERY SELECT 'foo'||id, 'bar'||id FROM generate_series(1,i) g(id); RETURN; END; $function$ postgres=# SELECT a,b FROM fx(4); ┌──┬──┐ │ a │

Re: [GENERAL] PL/pgSQL: How to return two columns and multiple rows

2015-06-18 Thread David G. Johnston
On Thursday, June 18, 2015, Sven Geggus wrote: > Hello, > > I supose this is simple, but I did not find a solution in the > documentation. > > I would like to be able to do something like this: > > select myfunc('foo','bar'); > or > select myfunc(foo, bar) from foobartable; > or even > select myf