Re: Function execution consuming lot of memory and eventually making server unresponsive

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: gnanam(at)zoniac(dot)com, pgsql-performance(at)postgresql(dot)org
Subject: Re: Function execution consuming lot of memory and eventually making server unresponsive
Date: 2011-02-24 15:58:53
Message-ID: 11544.1298563133@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Merlin Moncure <mmoncure(at)gmail(dot)com> 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

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Dave Johansen 2011-02-24 16:38:56 Re: Pushing IN (subquery) down through UNION ALL?
Previous Message Merlin Moncure 2011-02-24 15:14:13 Re: Function execution consuming lot of memory and eventually making server unresponsive