Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U7Ch6-0002HO-Re for pgsql-sql@arkaria.postgresql.org; Sun, 17 Feb 2013 22:26:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U7Ch6-0001Qf-Bu for pgsql-sql@arkaria.postgresql.org; Sun, 17 Feb 2013 22:26:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U7Ch5-0001Qa-Jx for pgsql-sql@postgresql.org; Sun, 17 Feb 2013 22:26:47 +0000 Received: from isis.morrow.me.uk ([204.109.63.142]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U7Ch2-0000zI-Dq for pgsql-sql@postgresql.org; Sun, 17 Feb 2013 22:26:46 +0000 Received: from anubis.morrow.me.uk (host109-150-212-220.range109-150.btcentralplus.com [109.150.212.220]) (Authenticated sender: mauzo) by isis.morrow.me.uk (Postfix) with ESMTPSA id 0FA7F450C2; Sun, 17 Feb 2013 22:26:39 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.7.4 isis.morrow.me.uk 0FA7F450C2 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=morrow.me.uk; s=dkim201101; t=1361140002; bh=sLp585LQNvIr5e9QzX+sx5dXHIed0uioQAzzta+xppc=; h=Date:From:To:Subject:References:In-Reply-To; b=PLKzOs1Shhem4V8/zvD7l8Rx54SIK7vZ1399k0LtnCj+weGpXIkMvs0t3eVlUlauM WasXhN1tZNbeq2Cw0CQA9iKYAYyuq7TJzpnmTbeNY7t0QnCoGrH1EJpu4n8GS+c1Wx oJ1kPZjhTmK9W23JX79g7lwD9TlPxUkMpHFsZQvE= X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.97.6 at isis.morrow.me.uk Received: by anubis.morrow.me.uk (Postfix, from userid 5001) id D558694BA; Sun, 17 Feb 2013 22:26:35 +0000 (GMT) Date: Sun, 17 Feb 2013 22:26:35 +0000 From: Ben Morrow To: maps.on@gmx.net, pgsql-sql@postgresql.org Subject: Re: How to reject overlapping timespans? Message-ID: <20130217222632.GA36320@anubis.morrow.me.uk> References: <51210D23.9070403@gmx.net> <1735216876.37617.1361125250332.JavaMail.open-xchange@ox.ims-firmen.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <51212881.1040600@gmx.net> X-Newsgroups: pgsql.sql Organization: morrow.me.uk User-Agent: Mutt/1.5.21 (2010-09-15) X-Pg-Spam-Score: -2.6 (--) 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 Quoth maps.on@gmx.net (Andreas): > Am 17.02.2013 19:20, schrieb Andreas Kretschmer: > > Andreas hat am 17. Februar 2013 um 18:02 geschrieben: > >> 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 > > though I still have a 9.1.x as productive server so I'm afraid I have to > find another way. If you don't fancy implementing or backporting a GiST operator class for date ranges using OVERLAPS, you can fake one with the geometric types. You will need contrib/btree_gist to get GiST indexes on integers. create extension btree_gist; create function point(date) returns point immutable language sql as $$ select point(0, ($1 - date '2000-01-01')::double precision) $$; create function box(date, date) returns box immutable language sql as $$ select box(point($1), point($2)) $$; create table objects_data ( object_id integer references objects, startdate date, enddate date, exclude using gist (object_id with =, box(startdate, enddate) with &&) ); You have to use 'box' rather than 'lseg' because there are no indexes for lsegs. I don't know how efficient this will be, and of course the unique index will probably not be any use for anything else. Ben -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql