Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id D35171337BA8 for ; Wed, 13 Apr 2011 11:53:23 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 97965-03 for ; Wed, 13 Apr 2011 14:52:33 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 78AA81337B93 for ; Wed, 13 Apr 2011 11:52:32 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p3DEqWoE004786; Wed, 13 Apr 2011 10:52:32 -0400 (EDT) To: Andreas Gaab cc: pgsql sql , =?iso-8859-1?Q?Hans_Joachim_M=FCnzer?= Subject: Re: unnesting of array of different size explodes memory In-reply-to: <48DA836F3865C54B8FBF424A3B775AF667239FFDEF@Exchange-Server> References: <48DA836F3865C54B8FBF424A3B775AF667239FFDEF@Exchange-Server> Comments: In-reply-to Andreas Gaab message dated "Wed, 13 Apr 2011 11:15:38 +0200" Date: Wed, 13 Apr 2011 10:52:32 -0400 Message-ID: <4785.1302706352@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/10 X-Sequence-Number: 35257 Andreas Gaab 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