Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ix8HV-0002jc-05 for pgsql-general@arkaria.postgresql.org; Thu, 30 Jan 2020 11:46:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ix8HT-00049P-G1 for pgsql-general@arkaria.postgresql.org; Thu, 30 Jan 2020 11:46:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ix8HT-00049I-31 for pgsql-general@lists.postgresql.org; Thu, 30 Jan 2020 11:46:43 +0000 Received: from hermes.gatewaynet.com ([193.92.121.98]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ix8HP-0005u6-DO for pgsql-general@lists.postgresql.org; Thu, 30 Jan 2020 11:46:41 +0000 Received: from smadev.internal.net (pc131.internal.net [10.9.200.131]) by hermes.gatewaynet.com (8.15.2/8.15.2/Debian-8) with ESMTPS id 00UBkVDl020743 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 30 Jan 2020 13:46:36 +0200 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id 00UBkVWW094434 for ; Thu, 30 Jan 2020 13:46:31 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: Exclude constraint on ranges : commutative containment : allow only complete containment To: pgsql-general@lists.postgresql.org References: <3b555944-1563-6614-a5d5-e0cc001aecdb@matrix.gatewaynet.com> From: Achilleas Mantzios Message-ID: Date: Thu, 30 Jan 2020 13:46:31 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:60.0) Gecko/20100101 Thunderbird/60.3.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Virus-Scanned: clamav-milter 0.101.4 at hermes X-Virus-Status: Clean X-Spam-Status: No, score=-29.0 required=5.0 tests=ALL_TRUSTED,DYN_HAM_COORD, DYN_HAM_IT,DYN_HAM_NAUTICAL autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on hermes.gatewaynet.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 29/1/20 8:32 μ.μ., Adrian Klaver wrote: > On 1/29/20 8:12 AM, Achilleas Mantzios wrote: >> Hello Dear Postgresql ppl, >> I have a table with date ranges and need to express the following constraint : allow overlaps only if there is complete containment, e.g. >> allow values in rows like : >> [2020-01-01,2020-01-31) >> [2020-01-02,2020-01-10) >> [2020-01-10,2020-01-20) >> >> but disallow rows like >> >> [2020-01-02,2020-01-10) >> [2020-01-08,2020-01-11) > > I'm missing something. Can you provide a more complete example? Dear Adrian, I can give an example, lets say that we want to model the concept of budget, and we allow basic complete budgets covering a specific time period (daterange) which will have predictions and also matched actual transactions (Debits, Credits) , but also want "super" budgets of larger time periods which include a number of basic budgets (sub-budgets) and for which only predictions are allowed, not actual transactions. We could make the design strict and explicit by using referential constraints (basic budget pointing to a super budget) but If we chose to not make it strict , and conversely allow a more dynamic and liberal way that the system detects one form or the other , we could say : each sub-budget (of the lowest level of the hierarchy - although for the time being we have only two levels) cannot overlap with any other sub-budget each super-budget can only fully contain its sub-budgets , no partial overlap allowed. This could be solved easily if there was a commutative containment operator like : CREATE OR REPLACE FUNCTION range_containment(anyrange, anyrange)  RETURNS boolean  LANGUAGE sql  IMMUTABLE PARALLEL SAFE STRICT AS $function$ select $1 <@ $2 OR $1 @> $2; $function$; so range_containment returns true if the first operand is contained in the second or contains the second. create operator <@@> (PROCEDURE=range_containment, LEFTARG=anyrange, RIGHTARG=anyrange, COMMUTATOR = <@@> ); But unfortunately : alter table bdynacom.acc_budget ADD CONSTRAINT acc_budget_start_end EXCLUDE USING gist (daterange(period_start, period_end, '[]'::text) WITH <@@>); ERROR:  operator <@@>(anyrange,anyrange) is not a member of operator family "range_ops" DETAIL:  The exclusion operator must be related to the index operator class for the constraint. From a small research I did this might mean recompiling the source to make <@@> member of range_ops . > >> >> I think that writing a new commutative range operator e.g. |<@@> which would return true if the left operand is either contained by or contains the right operand and false otherwise would solve >> this, I am just wondering if there is a more elegant and economical way to express this. (besides writing a trigger which is always an option).| >> >> -- >> Achilleas Mantzios >> IT DEV Lead >> IT DEPT >> Dynacom Tankers Mgmt >> > > -- Achilleas Mantzios IT DEV Lead IT DEPT Dynacom Tankers Mgmt