From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org, Heiko Klein <Heiko(dot)Klein(at)gmx(dot)net> |
Subject: | Re: Selecting from a function(x,y) returning a row-type(sum, prod) |
Date: | 2007-04-30 14:56:49 |
Message-ID: | 27207.1177945009@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
"A. Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com> writes:
> am Mon, dem 30.04.2007, um 15:45:18 +0200 mailte Heiko Klein folgendes:
>> How can I do the following:
>>
>> select * from myvals, sum_n_product(myvals.x, myvals.y);
> select x, y, sum_n_product(x,y) from myvals;
This is only part of the answer, however, because what you get is
regression=# select *, sum_n_product(x,y) from myvals;
x | y | sum_n_product
---+---+---------------
1 | 2 | (3,2)
(1 row)
which is not the display he wanted. If you know a little bit about how
PG deals with *-expansion you might think to try
regression=# select *, (sum_n_product(x,y)).* from myvals;
x | y | sum | prod
---+---+-----+------
1 | 2 | 3 | 2
(1 row)
which is the correct output --- but it turns out that what it's doing is
effectively
select *, (sum_n_product(x,y)).sum, (sum_n_product(x,y)).prod from myvals;
ie the function is called twice per row. If that's a problem, what you
have to do is resort to a two-level query:
regression=# select x,y,(f).* from
regression-# (select *, sum_n_product(x,y) as f from myvals offset 0) ss;
x | y | sum | prod
---+---+-----+------
1 | 2 | 3 | 2
(1 row)
The "offset 0" is an optimization fence to keep the planner from
flattening this form into the form where the function is called twice.
(As of 8.2, you can dispense with that if the function is marked volatile.)
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | John D. Burger | 2007-04-30 15:42:01 | Re: Processing a work queue |
Previous Message | psql psql | 2007-04-30 14:53:44 | Re: Stemming not working with tsearch2() function |