Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m4hgR-0007n7-Rm for pgsql-general@arkaria.postgresql.org; Sat, 17 Jul 2021 10:36:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m4hgO-0001gL-Gn for pgsql-general@arkaria.postgresql.org; Sat, 17 Jul 2021 10:36:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m4hgO-0001gD-5W for pgsql-general@lists.postgresql.org; Sat, 17 Jul 2021 10:36:32 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m4hgH-0004Yu-H4 for pgsql-general@postgresql.org; Sat, 17 Jul 2021 10:36:30 +0000 Received: by mail-ej1-x633.google.com with SMTP id hd33so19240052ejc.9 for ; Sat, 17 Jul 2021 03:36:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=F8wdbPrSTGkloK8MAwa8CvAPUETj7kXxNHpoQPA2wM0=; b=qo7R4rojqP5k7/g0FqbeSiXR9zcYcFPrWC0GGPjnw0GfsPf/TuEvQY0vJZhRDISV5s DdRdLclejOh8XSUP5HCLgdByfg6eqGyLGxb2RgfVPbLIjqV34oBdG2d4vwU4OAq07n7o 6Etotw/AyEAEc6euQZT1uYURcNnT81T+2Qs5sgnLGd+YBd4tHWDXYZFP73AoHVIKrJYs gtbFeDJjT7RDSeuW4tjFlVC82ie1zxmcwyVHtEYoqg5NdEW4snvRjiKQUa5jVKas1paE zaxEPh1tbk/EHc4XWaN73LbpZEIrhPDUZ4NqhVXjU2PRAZb6ml4B/ly1cn9adLpSatqS BhaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=F8wdbPrSTGkloK8MAwa8CvAPUETj7kXxNHpoQPA2wM0=; b=IgQlk4tOlXbrscVb5PN2NJjUaWN3aWiH4IyE3LmoXjHNJhkGK/JkTnuRQPSuq9BJbB BfBhX9ORM66ZRvK7mT3+Fi8AKppTVRFVaf3yKyREgxAqRnb/t6w+T81K9fRwz//NxpM5 Qw4GIRJg5nPTO1NXlMH4CT/gwqMwp/v68Tx8Tul0G5UP53O4oo408Z9rg0qkcOVqUHDS W/04GwcTwqIB3olJthBhLrvkzBuqa5AJjIt8ShTozO2cf7j0iCa8NvD2uqK+018mvoR9 59y1No+F8i+4O6ZVAWS3eBxmpA4qaTlCMdl1DyhAWkHt8jwYwoYsRMZ5N+h9mCvszPZ7 OJQQ== X-Gm-Message-State: AOAM530pB0axs5UA6Yur1Qi5lC/mAyzJWln7UMLKh5AXKGLeGDLcejRi EL3o2oHzQtF0jA57gaOyrys= X-Google-Smtp-Source: ABdhPJxzyWS8StCTn3RT695C/mbtebIC63oTgHOMTdqeZj/DJOorK7dEwWs9k+x5ONuqh3VGuogJaw== X-Received: by 2002:a17:906:81da:: with SMTP id e26mr17432847ejx.352.1626518183369; Sat, 17 Jul 2021 03:36:23 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id jo19sm3773159ejb.59.2021.07.17.03.36.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Jul 2021 03:36:22 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: Getting "ERROR: upper bound of FOR loop cannot be null" when trying to use a stored proc in a check constraint def. From: Alban Hertroys In-Reply-To: Date: Sat, 17 Jul 2021 12:36:21 +0200 Cc: Postgres General Content-Transfer-Encoding: quoted-printable Message-Id: References: To: David Gauthier X-Mailer: Apple Mail (2.3445.104.17) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 17 Jul 2021, at 0:26, David Gauthier = wrote: (=E2=80=A6) > dvdb=3D# select validate_proj_csv('sabin,strix2,ipu1.0'); > validate_proj_csv=20 > ------------------- > 1 > (1 row) >=20 > dvdb=3D# select validate_proj_csv('sabin,strix2,ipu1.00'); > NOTICE: Project "ipu1.00" in project csv "sabin,strix2,ipu1.00" is = not a valid project. > validate_proj_csv=20 > ------------------- > 0 > (1 row) >=20 >=20 > But when I try to use it in a check constraint.... >=20 > dvdb=3D# alter table projects add constraint = validate_sibling_project_csv check = (validate_proj_csv(sibling_project_csv) =3D 0); > ERROR: upper bound of FOR loop cannot be null > CONTEXT: PL/pgSQL function validate_proj_csv(character varying) line = 14 at FOR with integer loop variable >=20 > What's going on ? > How to get this to work ? As people advised you previously, you would probably be better off = normalising your table. For example, add a table for the links: create table project_sibling ( project text not null references public.projects(project) on update cascade on delete cascade , sibling text not null references public.projects(project) on update cascade on delete cascade , primary key (project, sibling) ); -- Populate it from public.projects initially insert into project_sibling(project, sibling) select p.project, s.sibling from public.projects p cross join lateral regex_split_to_table(project_csv, ',') s(sibling) ; I had to make a few guesses there, as I don=E2=80=99t have your original = table structure, but that=E2=80=99s the gist of it. If that project_csv column gets populated by some external application, = you could keep the link-table updated with insert/update/delete = triggers. Alternatively, a writable view replacing public.projects may be a = possibility. Regards, Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.