Re: unnesting of array of different size explodes memory - Mailing list pgsql-sql

From Tom Lane
Subject Re: unnesting of array of different size explodes memory
Date
Msg-id 4785.1302706352@sss.pgh.pa.us
Whole thread Raw
In response to unnesting of array of different size explodes memory  (Andreas Gaab <A.Gaab@scanlab.de>)
List pgsql-sql
Andreas Gaab <A.Gaab@scanlab.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.6e6rows. 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


pgsql-sql by date:

Previous
From: Andreas Gaab
Date:
Subject: unnesting of array of different size explodes memory
Next
From: Tom Lane
Date:
Subject: Re: pass in array to function for use by where clause? how optimize?