Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ek4bU-00037G-RK for pgsql-sql@arkaria.postgresql.org; Fri, 09 Feb 2018 09:04:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ek4bS-0003ne-HN for pgsql-sql@arkaria.postgresql.org; Fri, 09 Feb 2018 09:04:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ek4bS-0003nU-7w for pgsql-sql@lists.postgresql.org; Fri, 09 Feb 2018 09:04:18 +0000 Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=smadev.internal.net) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ek4bO-0007gs-5V for pgsql-sql@postgresql.org; Fri, 09 Feb 2018 09:04:16 +0000 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id w19947EB010919 for ; Fri, 9 Feb 2018 11:04:08 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) From: Achilleas Mantzios Subject: functions allowed in CHECK constraints To: pgsql-sql Message-ID: <1159df36-5e6b-a433-e9b7-5d197f820f56@matrix.gatewaynet.com> Date: Fri, 9 Feb 2018 11:04:07 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 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: Precedence: bulk Hello, I just tried specifying a function in a check constraint, (not being able to use a subquery or otherwise referencing rows in another (parent) table) and this worked. However reading the docs do not point to any such direction, on the contrary docs say that subselects are not allowed. e.g. this fails as expected : alter table crew_eval_rankset_rank ADD CONSTRAINT ranksetid_chk CHECK((SELECT NOT appliestoall FROM crew_eval_rankset cer WHERE cer.id=ranksetid)); ERROR:  cannot use subquery in check constraint while this works unexpectedly : create function crew_eval_ranksets_check_appliestoall (ranksetid int) RETURNS BOOLEAN LANGUAGE sql AS $$ SELECT NOT appliestoall FROM crew_eval_rankset cer WHERE cer.id=ranksetid $$; alter table crew_eval_rankset_rank ADD CONSTRAINT ranksetid_chk CHECK(crew_eval_ranksets_check_appliestoall(ranksetid)); insert into crew_eval_rankset(setname,appliestoall) VALUES('all ranks','t'); insert into crew_eval_rankset_rank (ranksetid , rankid) VALUES(3,70); ERROR:  new row for relation "crew_eval_rankset_rank" violates check constraint "ranksetid_chk" So, what's the point in forbidding the use of subselects if one can use functions? And OTOH if effectively doing so is bad for some reason, why let it happen with a function? Basically, I tried this after reading : https://stackoverflow.com/questions/21791675/foreign-key-constraint-with-some-column-values-residing-in-other-tables/22023533#22023533 , I wouldn't have thought doing it after reading the docs. -- Achilleas Mantzios IT DEV Lead IT DEPT Dynacom Tankers Mgmt