pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: David Gauthier <davegauthierpg@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Getting "ERROR: upper bound of FOR loop cannot be null" when trying to use a stored proc in a check constraint def.
Date: Sat, 17 Jul 2021 12:36:21 +0200
Message-ID: <C37E993A-BA24-48F6-942D-AF439D394474@gmail.com> (raw)
In-Reply-To: <CAMBRECB+c1nA-AGx8JMX-4RfVxyKLczJt6gkiPK0=AKu26h-uQ@mail.gmail.com>
References: <CAMBRECB+c1nA-AGx8JMX-4RfVxyKLczJt6gkiPK0=AKu26h-uQ@mail.gmail.com>


> On 17 Jul 2021, at 0:26, David Gauthier <davegauthierpg@gmail.com> wrote:

(…)

> dvdb=# select validate_proj_csv('sabin,strix2,ipu1.0');
>  validate_proj_csv 
> -------------------
>                  1
> (1 row)
> 
> dvdb=# 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 
> -------------------
>                  0
> (1 row)
> 
> 
> But when I try to use it in a check constraint....
> 
> dvdb=# alter table projects add constraint validate_sibling_project_csv check (validate_proj_csv(sibling_project_csv) = 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
> 
> 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’t have your original table structure, but that’s 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.






view thread (5+ messages)  latest in thread

Message-ID: <C37E993A-BA24-48F6-942D-AF439D394474@gmail.com>
Permalink:  ../C37E993A-BA24-48F6-942D-AF439D394474@gmail.com/
Also on:    postgresql.org/message-id/C37E993A-BA24-48F6-942D-AF439D394474@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, davegauthierpg@gmail.com
  Subject: Re: Getting "ERROR: upper bound of FOR loop cannot be null" when trying to use a stored proc in a check constraint def.
  In-Reply-To: <C37E993A-BA24-48F6-942D-AF439D394474@gmail.com>
  References: <CAMBRECB+c1nA-AGx8JMX-4RfVxyKLczJt6gkiPK0=AKu26h-uQ@mail.gmail.com> <C37E993A-BA24-48F6-942D-AF439D394474@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox