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 1m4oCI-0000Nr-Op for pgsql-general@arkaria.postgresql.org; Sat, 17 Jul 2021 17:33:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m4oCF-0003bP-UI for pgsql-general@arkaria.postgresql.org; Sat, 17 Jul 2021 17:33:51 +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 1m4oCE-0003Yo-4U for pgsql-general@lists.postgresql.org; Sat, 17 Jul 2021 17:33:50 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m4oC9-0007a5-Ha for pgsql-general@postgresql.org; Sat, 17 Jul 2021 17:33:48 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 0B2103200921; Sat, 17 Jul 2021 13:33:42 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sat, 17 Jul 2021 13:33:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=D yvtlE/xMGeQY3XDvBBjrIYxFT/T7tBVwcQRWxRociw=; b=LESeuJv/KNMz/9lcc 5HIEA/FAnR6tdM8xL2ZZ0G5Nr/NgGr5SUWrrrhuH1U5IVBAPINUuu+biGZc+Sn5i P25CsG7VJC8EdokvGNjpKV0kAubLjLoEREJEaQh1YkHHFhrp0uAe15cCK5nW/KB8 1c0KcnrhdizcXF3ZvwyUlroj43vn8pa1rM9Ob9einOfGm6EigNT2lN1WqGSh30iI Kun6r3KrYdiRn5KAON8OykEA7ymN/IuMPnwNzu9VAP/Mng9t7BMIHxluOlOXVtQa Xb+kaFgaE08Wi/Lvpf2KieMrENP+xeB8K/n4vzTUyJol8MzPhntfmppqf1gPSfJY HrMcw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=DyvtlE/xMGeQY3XDvBBjrIYxFT/T7tBVwcQRWxRoc iw=; b=MejxjP5E9iCUCRIgHtW0WvJtuX0P0gpRbWySSW7D1THXtzz1RgoXv/kWB pp7ncYBax4Ys8VQhTfHGiEgGOjp7THTc6ejiwWY2HRJITyLtXNMZIaLzwNhduXAd bswetqwGxbNW3icknYsZEvZjvWnZDfRqqhXZmGrpT5NU140ql6xTNXJcWjZQLC5P TrefEi7hq66YTjCsBzi63wVqTLoZ3rklcJG1tFaVYx7Z+kjSpdZN+PLCwKWKKbwQ YjwHXLwjEfOQu7072Vr1IdvHELFj/Dif6X3hkN021CPjk22lF86m0tjXcNmIE7or 5jVg3dgd8kUdUjmt7s3YcYObC7u4Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrvdehgdehjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeduudefjeetve ffhefgudehtdfhvdetkeeiueehfeevveduteekleeuheegudehhfenucffohhmrghinhep phhoshhtghhrvghsqhhlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtgho mh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 17 Jul 2021 13:33:42 -0400 (EDT) Subject: Re: Getting "ERROR: upper bound of FOR loop cannot be null" when trying to use a stored proc in a check constraint def. To: David Gauthier References: Cc: pgsql-general From: Adrian Klaver Message-ID: Date: Sat, 17 Jul 2021 10:33:41 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/17/21 10:13 AM, David Gauthier wrote: Please reply to list also. Ccing list. > That was it.  I added... > >   if (proj_csv is null) then >     return(1); >   end if; > > ... and it works fine. > > Back in earlier versions of PG (I'm talking v8), existing column values > weren't checked.  I remember you had to manually run a query using your > stored procedure to see if there would be any existing violations before > creating the constraint.  The way it is now is an improvement, much better. Glad it worked. Just be aware that using a function as a check constraint is skirting the rules for CHECK as explained here: https://www.postgresql.org/docs/current/sql-createtable.html There have been multiple posts to --general where that has come back to bite someone. Generally because the function reaches out to some other object in the database, which may or may not be there later. To me your particular function looks fairly benign, still you are standing close to the edge:) > > Thanks Adrian. > > On Fri, Jul 16, 2021 at 6:30 PM Adrian Klaver > wrote: > > On 7/16/21 3:26 PM, David Gauthier wrote: > > This stored procedure ... > > > 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 ? > > I'm going to say you have a NULL value in sibling_project_csv in the > table. > > > How to get this to work ? > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com