Re: strange infinite loop in plpgsql - Mailing list pgsql-general

From rihad
Subject Re: strange infinite loop in plpgsql
Date
Msg-id 47367C90.1040009@mail.ru
Whole thread Raw
In response to Re: strange infinite loop in plpgsql  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
>> rihad <rihad@mail.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.

It's morning again over at our place, so... of course it's not! I
stupidly did a SELECT date+1 instead of SELECT day+1, resulting in an
infinite loop. A hard to spot bug, especially when your eyes are half
closed. Good thing is that the solution came to me while I was still in
bed a minute ago :-)


But your idea later on of separating EXTRACT outside the disk-touching
code might be a nice cpu-memory trade-off that I hadn't thought of (or
wasn't aware). It turns out any SQL "thing" can be in an IF or WHILE
etc. ? I'll go read the docs more attentively.

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: (Never?) Kill Postmaster?
Next
From: "Scott Marlowe"
Date:
Subject: Re: any way for ORDER BY x to imply NULLS FIRST in 8.3?