Re: strange infinite loop in plpgsql

From: rihad <rihad(at)mail(dot)ru>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: strange infinite loop in plpgsql
Date: 2007-11-10 18:19:20
Message-ID: 4735F628.6040006@mail.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tom Lane wrote:
> rihad <rihad(at)mail(dot)ru> writes:
>> LOOP
>> SELECT date+1 INTO day FROM days WHERE date=day OR EXTRACT(dow
>> FROM day) IN (0,6);
>> EXIT WHEN NOT FOUND;
>> timeout := timeout + 86400;
>> END LOOP;
>
> If the EXTRACT condition is true, then the SELECT will always succeed.

Isn't the new "day" re-evaluated on every loop iteration? I'm totally
confused.

> This code will get even more whacko once you have more than one row
> in "days", because it'll pick a random one of the rows in that case
> (in practice, the physically first one). I think you need something
> more like
>
> LOOP
> IF EXTRACT(dow FROM day) IN (0,6) THEN
> -- don't bother to consult table on weekends
> day := day + 1;
> ELSE
> SELECT date+1 INTO day FROM days WHERE date=day;
> EXIT WHEN NOT FOUND;
> END IF;
> timeout := timeout + 86400;
> END LOOP;
>
> BTW, you forgot to initialize "timeout".
>
Sorry, I hand-cooked this fast from the working code. I guess it
defaults to NULL instead of "random bits", which of course wouldn't save
me either, but the real (somewhat bigger) code eventually does RETURN
LEAST(timeout, expiration_timeout); skipping any nulls.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tony Caduto 2007-11-10 21:34:02 Verison 8.3 PL/pgSQL debugger Question
Previous Message Tom Lane 2007-11-10 17:50:14 Re: (Never?) Kill Postmaster?