Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UUDqS-0005PB-AJ for pgsql-sql@arkaria.postgresql.org; Mon, 22 Apr 2013 10:19:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UUDqR-00065N-II for pgsql-sql@arkaria.postgresql.org; Mon, 22 Apr 2013 10:19:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UUDqQ-00065G-Iw for pgsql-sql@postgresql.org; Mon, 22 Apr 2013 10:19:34 +0000 Received: from plane.gmane.org ([80.91.229.3]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UUDqG-0007T2-Bn for pgsql-sql@postgresql.org; Mon, 22 Apr 2013 10:19:34 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1UUDqE-0005x5-Sx for pgsql-sql@postgresql.org; Mon, 22 Apr 2013 12:19:22 +0200 Received: from dslb-084-060-109-059.pools.arcor-ip.net ([84.60.109.59]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 22 Apr 2013 12:19:22 +0200 Received: from WolfgangMeiners01 by dslb-084-060-109-059.pools.arcor-ip.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 22 Apr 2013 12:19:22 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Wolfgang Meiners Subject: check for overlapping time intervals Date: Mon, 22 Apr 2013 12:19:17 +0200 Lines: 49 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: dslb-084-060-109-059.pools.arcor-ip.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 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 Hi, I am on postgresql 9.1 and use at table like CREATE TABLE timetable( tid INTEGER PRIMARY KEY, gid INTEGER REFERENCES groups(gid), day DATE, s TIME NOT NULL, --- start e TIME NOT NULL, --- end CHECK (e > s)); Now, i need a constraint to prevent overlapping timeintervals in this table. For this, i use a trigger: CREATE OR REPLACE FUNCTION validate_timetable() RETURNS trigger AS $$ BEGIN IF TG_OP = 'INSERT' THEN IF EXISTS( SELECT * FROM timetable WHERE gid = NEW.gid AND day = NEW.day AND s < NEW.e AND e > NEW.s) THEN RAISE EXCEPTION 'overlapping intervals'; END IF; ELSIF TG_OP = 'UPDATE' THEN IF EXISTS( SELECT * FROM timetable WHERE gid = NEW.gid AND day = NEW.day AND tid <> OLD. tid AND s < NEW.e AND e > NEW.s) THEN RAISE EXCEPTION 'overlapping intervals'; END IF; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER validate_timetable BEFORE INSERT OR UPDATE ON timetable FOR EACH ROW EXECUTE PROCEDURE validate_timetable(); Is there a simpler way to check for overlapping timeintervals? I ask this question, because i have more similar tables with similar layout and would have to write similar functions again and again. Thank you for any hints Wolfgang -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql