pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
select (fn()).* executes function multiple times
4+ messages / 3 participants
[nested] [flat]

* select (fn()).* executes function multiple times
@ 2018-11-16 14:49 Andrey <parihaaraka@gmail.com>
  2018-11-16 14:57 ` Re: select (fn()).* executes function multiple times David G. Johnston <david.g.johnston@gmail.com>
  2018-11-16 14:57 ` Re: select (fn()).* executes function multiple times Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 4+ messages in thread

From: Andrey @ 2018-11-16 14:49 UTC (permalink / raw)
  To: pgsql-bugs

PostgreSQL 10.5, 11.1:

create table fn_ret_test_storage (f1 int default 1, f2 int default 2, f3
int default 3);

create or replace function fn_ret_test(out f1 int, out f2 int, out f3 int)
returns record
language plpgsql
as
$$
begin
insert into fn_ret_test_storage default values
returning * into f1, f2, f3;
end
$$;

do
$$
declare
_count int;
begin
select count(*) into _count from fn_ret_test_storage;
perform (fn_ret_test()).*;
select count(*) - _count into _count from fn_ret_test_storage;
raise notice '----------------- % rows inserted -----------------', _count;
end
$$;

-- output: NOTICE:  ----------------- 3 rows inserted -----------------

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: select (fn()).* executes function multiple times
  2018-11-16 14:49 select (fn()).* executes function multiple times Andrey <parihaaraka@gmail.com>
@ 2018-11-16 14:57 ` David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: David G. Johnston @ 2018-11-16 14:57 UTC (permalink / raw)
  To: parihaaraka@gmail.com; +Cc: pgsql-bugs

On Fri, Nov 16, 2018 at 7:49 AM Andrey <parihaaraka@gmail.com> wrote:
> perform (fn_ret_test()).*;

Yes it does [execute fn_ret_rest multiple times]; its known behavior
that while surprising is unlikely to get fixed.  Its simple to work
around using the LATERAL construct (i.e., placing said function call
in the FROM clause).

The root problem is the use of ".*" - the rewriter turns it into:
SELECT fn_ret_test.f1(), fn_ret_test().f2, fn_ret_test().f3; which
when written this way become evident why it is executed multiple
times.

David J.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: select (fn()).* executes function multiple times
  2018-11-16 14:49 select (fn()).* executes function multiple times Andrey <parihaaraka@gmail.com>
@ 2018-11-16 14:57 ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-11-16 15:08   ` Re: select (fn()).* executes function multiple times Andrey <parihaaraka@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Tom Lane @ 2018-11-16 14:57 UTC (permalink / raw)
  To: Andrey <parihaaraka@gmail.com>; +Cc: pgsql-bugs

That's not a bug, that's just how it works.

You can get the other way with something like

SELECT f.* FROM ..., LATERAL fn(...) f;

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: select (fn()).* executes function multiple times
  2018-11-16 14:49 select (fn()).* executes function multiple times Andrey <parihaaraka@gmail.com>
  2018-11-16 14:57 ` Re: select (fn()).* executes function multiple times Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-11-16 15:08   ` Andrey <parihaaraka@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Andrey @ 2018-11-16 15:08 UTC (permalink / raw)
  To: tgl@sss.pgh.pa.us; +Cc: pgsql-bugs

O_o
I got it. Sorry for disturbing you.
Thanks

regards, Andrey L

пт, 16 нояб. 2018 г. в 17:57, Tom Lane <tgl@sss.pgh.pa.us>:

> That's not a bug, that's just how it works.
>
> You can get the other way with something like
>
> SELECT f.* FROM ..., LATERAL fn(...) f;
>
>                         regards, tom lane
>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2018-11-16 15:08 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-11-16 14:49 select (fn()).* executes function multiple times Andrey <parihaaraka@gmail.com>
2018-11-16 14:57 ` David G. Johnston <david.g.johnston@gmail.com>
2018-11-16 14:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-11-16 15:08   ` Andrey <parihaaraka@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox