Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U78sx-00020m-AT for pgsql-sql@arkaria.postgresql.org; Sun, 17 Feb 2013 18:22:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U78sw-00044V-RH for pgsql-sql@arkaria.postgresql.org; Sun, 17 Feb 2013 18:22:46 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U78sw-00044Q-5L for pgsql-sql@postgresql.org; Sun, 17 Feb 2013 18:22:46 +0000 Received: from mailout01.ims-firmen.de ([213.174.32.96]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U78ss-00050Y-S6 for pgsql-sql@postgresql.org; Sun, 17 Feb 2013 18:22:45 +0000 Received: from mailin04.ims-firmen.de ([192.168.1.144]) by mailout01.ims-firmen.de with esmtp (envelope-from ) id 1U78sq-00084S-gZ; Sun, 17 Feb 2013 19:22:40 +0100 Received: from [213.174.32.192] (helo=oxweb02.ims-firmen.de) by mailin04.ims-firmen.de with esmtpsa (TLSv1:RC4-MD5:128) (envelope-from ) id 1U78sq-0004Aq-AA; Sun, 17 Feb 2013 19:22:40 +0100 Date: Sun, 17 Feb 2013 19:20:50 +0100 (CET) From: Andreas Kretschmer Reply-To: Andreas Kretschmer To: pgsql-sql@postgresql.org, Andreas Message-ID: <1735216876.37617.1361125250332.JavaMail.open-xchange@ox.ims-firmen.de> In-Reply-To: <51210D23.9070403@gmx.net> References: <51210D23.9070403@gmx.net> Subject: Re: How to reject overlapping timespans? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Medium X-Mailer: Open-Xchange Mailer v6.20.7-Rev10 X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Andreas hat am 17. Februar 2013 um 18:02 geschrieben: > Hi, > > I need to store data that has a valid timespan with start and enddate. > > objects ( id, name, ... ) > object_data ( object_id referencs objects(id), startdate, enddate, ... ) > > nothing special, yet > > How can I have PG reject a data record where the new start- or enddate > lies between the start- or enddate of another record regarding the same > object_id? With 9.2 you can use DATERANGE and exclusion constraints test=# create table maps(id int, duration daterange, exclude using gist(id with =, duration with &&)); NOTICE: CREATE TABLE / EXCLUDE will create implicit index "maps_id_duration_excl" for table "maps" CREATE TABLE test=*# insert into maps values (1,'(2013-01-01,2013-01-10]'); INSERT 0 1 test=*# insert into maps values (1,'(2013-01-05,2013-01-15]'); ERROR: conflicting key value violates exclusion constraint "maps_id_duration_excl" DETAIL: Key (id, duration)=(1, [2013-01-06,2013-01-16)) conflicts with existing key (id, duration)=(1, [2013-01-02,2013-01-11)). test=*# Regards, Andreas -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql