Re: help using arrays in a function - Mailing list pgsql-general

From Tom Lane
Subject Re: help using arrays in a function
Date
Msg-id 11716.1077804462@sss.pgh.pa.us
Whole thread Raw
In response to help using arrays in a function  ("Jennifer Lee" <jlee@scri.sari.ac.uk>)
List pgsql-general
"Jennifer Lee" <jlee@scri.sari.ac.uk> writes:
>                         WHILE a[i] LOOP

> I'm now testing our database on 7.4 and get an error with this function
> and I can't figure out how to fix it.
> ERROR:  invalid input syntax for type boolean: "100"
> CONTEXT:  PL/pgSQL function "array_element_compare" line 9 at while

plpgsql now enforces that the test expression of IF, WHILE, etc must be
a boolean.  The above is not.

This is pretty poor coding practice anyway, since even before 7.4 it
would have given wrong answers for arrays containing zeroes or arrays
whose lower index bound is not 1.  I'd suggest using the array_lower and
array_upper functions to determine the valid range of subscripts.
Something like

    FOR i IN array_lower(a,1) .. array_upper(a,1) LOOP

should work.

            regards, tom lane

pgsql-general by date:

Previous
From: Randall Skelton
Date:
Subject: Re: Simplyfying many equals in a join
Next
From: Karam Chand
Date:
Subject: Index Information