Re: BUG #13798: Unexpected multiple exection of user defined function with out parameters

Поиск
Список
Период
Сортировка
От Kevin Grittner
Тема Re: BUG #13798: Unexpected multiple exection of user defined function with out parameters
Дата
Msg-id CACjxUsM0h1LHhkVLAWBgmWWj7x+6PRm9_qB2eutA1fdBmzvKEQ@mail.gmail.com
обсуждение исходный текст
Ответ на BUG #13798: Unexpected multiple exection of user defined function with out parameters  (mike.lang1010@gmail.com)
Ответы Re: BUG #13798: Unexpected multiple exection of user defined function with out parameters  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-bugs
On Sat, Dec 5, 2015 at 12:30 AM,  <mike.lang1010@gmail.com> wrote:

> I've found that when a user defined function has
> out parameters, it is invoked once per out parameter if invoked with the
> syntax:
>
> `SELECT (udf()).*`
>
> Is this the expected behavior?  It seems like it shouldn't.

You can see why this happens if you use EXPLAIN this way:

test=# explain (analyze, buffers, verbose) SELECT (reproduceBehavior()).*;
                                                                QUERY PLAN

------------------------------------------------------------------------------------------------------------------------------------------
 Result  (cost=0.00..1.01 rows=1 width=0) (actual time=1.931..1.932
rows=1 loops=1)
   Output: (reproducebehavior()).message1,
(reproducebehavior()).message2, (reproducebehavior()).message3,
(reproducebehavior()).message4
   Buffers: shared hit=17
 Planning time: 0.038 ms
 Execution time: 1.968 ms
(5 rows)

That shows that the * causes expansion to the following query:

SELECT
    (reproducebehavior()).message1,
    (reproducebehavior()).message2,
    (reproducebehavior()).message3,
    (reproducebehavior()).message4;

From that you can see why it is not surprising that the function is
executed once per OUT parameter, especially if it is VOLATILE.

You seem to be expecting it to behave like this:

SELECT * FROM (SELECT * FROM reproduceBehavior()) x;

Which is interpreted as:

SELECT x.message1, x.message2, x.message3, x.message4
  FROM (SELECT * FROM reproduceBehavior()) x;

To avoid surprises, avoid using *.

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

В списке pgsql-bugs по дате отправления:

Предыдущее
От: Andres Freund
Дата:
Сообщение: Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement.
Следующее
От: "David G. Johnston"
Дата:
Сообщение: Re: BUG #13798: Unexpected multiple exection of user defined function with out parameters