Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 564C5B5DBF8 for ; Mon, 22 Aug 2011 12:20:58 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 47353-06 for ; Mon, 22 Aug 2011 15:20:50 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 8766CB5DBF7 for ; Mon, 22 Aug 2011 12:20:50 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QvWIs-0006m5-Qe for pgsql-sql@postgresql.org; Mon, 22 Aug 2011 17:20:42 +0200 Received: from 212.126.222.122 ([212.126.222.122]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 22 Aug 2011 17:20:42 +0200 Received: from hari.fuchs by 212.126.222.122 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 22 Aug 2011 17:20:42 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: exclusion constraint for ranges of IP Date: Mon, 22 Aug 2011 17:20:30 +0200 Lines: 46 Message-ID: <86hb595v7l.fsf@mgm.protecting.net> References: <86ty9a5rw6.fsf@mgm.protecting.net> <1343D11C-6F58-4653-8EA8-837C01E6184F@unicell.co.il> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 212.126.222.122 X-Archive: encrypt User-Agent: Gnus/5.110009 (No Gnus v0.9) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:8ePTJNqSOMYSrSiCyL4izIYQBww= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.671 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_NUMERIC_HELO=1.164, RP_MATCHES_RCVD=-0.946, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201108/86 X-Sequence-Number: 35754 In article <1343D11C-6F58-4653-8EA8-837C01E6184F@unicell.co.il>, Herouth Maoz writes: > On 22/08/2011, at 01:19, Harald Fuchs wrote: >> In article , >> Herouth Maoz writes: >> >>> Hi, >>> I'm designing a new database. One of the table contains allowed IP ranges for a customer (Fields: customer_id, from_ip, to_ip) which is intended to check - if an incoming connection's originating IP number falls within the range, it is identified as a particular customer. >> >>> Naturally, I'd like to have constraints on the table that prevent entering of ip ranges that overlap. Is there a way to do that with exclusion constraints? Or do I have to define a new type for this? >> >> This "new type" already exists: ip4r, which can be found in pgfoundry. >> With it you can do >> >> CREATE TABLE mytbl ( >> iprange ip4r NOT NULL, >> ..., >> CONSTRAINT range_check CHECK ((NOT overlap(iprange))) >> ); > Thank you. > I assume you can't use a CHECK constraint for between-rows constraints. Wouldn't this be > CONSTRAINT EXCLUDE ( iprange WITH && ) > ? You're right. In the old PostgreSQL version I had to use I defined a helper function CREATE FUNCTION overlap(ip4r) RETURNS boolean LANGUAGE sql AS $_$ SELECT count(*) > 0 FROM mytbl WHERE iprange != $1 AND iprange && $1 $_$; for the CHECK CONSTRAINT, but in more recent PostgreSQL versions you can just say EXCLUDE (iprange WITH &&) (without CONSTRAINT).