Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 05B351337B4E for ; Thu, 24 Feb 2011 11:59:04 -0400 (AST) 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 73214-01 for ; Thu, 24 Feb 2011 15:58:55 +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 409F3133798E for ; Thu, 24 Feb 2011 11:58:55 -0400 (AST) 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 p1OFwrST011545; Thu, 24 Feb 2011 10:58:53 -0500 (EST) To: Merlin Moncure cc: gnanam@zoniac.com, pgsql-performance@postgresql.org Subject: Re: Function execution consuming lot of memory and eventually making server unresponsive In-reply-to: References: <00af01cbd408$93b63590$bb22a0b0$@com> Comments: In-reply-to Merlin Moncure message dated "Thu, 24 Feb 2011 09:14:13 -0600" Date: Thu, 24 Feb 2011 10:58:53 -0500 Message-ID: <11544.1298563133@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/470 X-Sequence-Number: 42608 Merlin Moncure writes: > Your entire function could probably be reduced to one SQL expression > with some thought. Or if not that, at least try to get rid of the use of varchar. All those forced varchar-to-date-and-back conversions are expensive. I'm also more than a tad worried by this: > v_loopingdate := TO_CHAR(DATE(p_startdate), ''mm-dd-yyyy''); > > WHILE (DATE(v_loopingdate) <= DATE(p_enddate)) LOOP There's nothing here guaranteeing that DATE() will think its input is in mm-dd-yyyy format. If DateStyle is set to something else, the logic would at least be wrong, and very possibly that explains your infinite loop. Learn to use PG's type system instead of fighting it. Your code will be shorter, clearer, faster, and less error-prone. regards, tom lane