Re: Return Multiple Rows from Store Function

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Brahmam Eswar <brahmam1234(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Return Multiple Rows from Store Function
Date: 2018-07-27 09:47:11
Message-ID: CAFj8pRDD5-3h2g1rbYgOmX+MxY6B5EUoQR-1Fhmw33J3t89xFw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

2018-07-27 11:24 GMT+02:00 Brahmam Eswar <brahmam1234(at)gmail(dot)com>:

> Hi ,
>
> Returning multiple rows from store functions using "RETURNS TABLE" and
> RETURN QUERY. The results set may have more than 50k records. Does it give
> any performance issues related to memory? if yes how to avoid it
>
>
This command uses tuple store - the result is stored in memory to work_mem
size, and later is materialized (stored to temporary file).

Probably worst effect is invisibility of embedded query for planner. The
default estimation of SRF (set returning function) is 1000 rows. If you run
50K rows, the estimation will be really off, and the plan of query can be
strongly suboptimal.

The best practice is not using similar functions. Functions should not to
supply views. It is antipattern with more than one possible performance
issue.

regards

Pavel

> CREATE OR REPLACE FUNCTION funcq(COL1 character varying)
> )
> RETURNS TABLE
> ( a VARCHAR,
> b VARCHAR,
> c varchar)
> AS $$
>
> BEGIN
>
> RETURN QUERY SELECT a,b,c from table1 where C= COL1;
>
> END;
> $$
> LANGUAGE plpgsql;
>
>
> --
> Thanks & Regards,
> Brahmeswara Rao J.
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Łukasz Jarych 2018-07-27 10:18:55 Re: Read only to schema
Previous Message Brahmam Eswar 2018-07-27 09:24:25 Return Multiple Rows from Store Function