Re: unnesting of array of different size explodes memory

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andreas Gaab <A(dot)Gaab(at)scanlab(dot)de>
Cc: pgsql sql <pgsql-sql(at)postgresql(dot)org>, Hans Joachim Münzer <H(dot)Muenzer(at)scanlab(dot)de>
Subject: Re: unnesting of array of different size explodes memory
Date: 2011-04-13 14:52:32
Message-ID: 4785.1302706352@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Andreas Gaab <A(dot)Gaab(at)scanlab(dot)de> writes:
> As I now understand, the following query leads to 12 results, not just 4 (or 3...):

> SELECT unnest(ARRAY[1,2,3]), unnest(ARRAY[4,5,6,7]);

> Why could postgres use as much memory till the kernel complained when unnesting 1200 and 1300 elements resulting in 1.6e6 rows. Are there settings to prevent this such as "work_mem"?

Multiple SRFs in a targetlist are a good thing to avoid. The behavior
is ... um ... peculiar, and the fact that we can't reclaim memory
partway through is really the least of the problems with it.

Try doing it like this instead:

SELECT * from unnest(ARRAY[1,2,3]) a, unnest(ARRAY[4,5,6,7]) b;

This has saner behavior and is less likely to leak memory. Not to
mention less likely to be deprecated or de-implemented altogether
in the far future.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2011-04-13 15:09:23 Re: pass in array to function for use by where clause? how optimize?
Previous Message Andreas Gaab 2011-04-13 09:15:38 unnesting of array of different size explodes memory