Re: FOR integer loop bug?

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Ron <ronljohnsonjr(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: FOR integer loop bug?
Date: 2022-04-08 19:27:43
Message-ID: efd5f71b-93f4-3d49-7a6d-43a6cf509659@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 4/8/22 11:23, Ron wrote:
> On 4/8/22 13:07, Adrian Klaver wrote:

>> Hmm, I'm going to have to think on this.
>
> The only thinking is: "That's a bug waiting to happen!"
>

That was my first inclination.

From here:

https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-CONTROL-STRUCTURES-LOOPS

FOR name IN [ REVERSE ] expression .. expression [ BY expression ] LOOP

So:

CREATE OR REPLACE FUNCTION for_loop_test()
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
FOR i IN 1..(2/3.0) LOOP
RAISE NOTICE '%', i;
END LOOP;
END;

select for_loop_test();
NOTICE: 1
for_loop_test
---------------

Because:

select (2/3.0)::int;
int4
------
1

I find the result from this 1...10 surprising, however I have to believe
there is code counting on the equivalent of (2/3.0) working.

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Perry Smith 2022-04-09 14:42:57 Constraint ordering
Previous Message Ron 2022-04-08 18:23:06 Re: FOR integer loop bug?