Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 2726979D57 for ; Thu, 12 Jul 2012 10:40:30 -0300 (ADT) Received: from nm8-vm0.bullet.mail.ac4.yahoo.com ([98.139.52.230]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1SpJd0-0006TA-V7 for pgsql-sql@postgresql.org; Thu, 12 Jul 2012 13:40:29 +0000 Received: from [98.139.52.191] by nm8.bullet.mail.ac4.yahoo.com with NNFMP; 12 Jul 2012 13:40:09 -0000 Received: from [98.139.52.181] by tm4.bullet.mail.ac4.yahoo.com with NNFMP; 12 Jul 2012 13:40:09 -0000 Received: from [127.0.0.1] by omp1064.mail.ac4.yahoo.com with NNFMP; 12 Jul 2012 13:40:09 -0000 X-Yahoo-Newman-Id: 913457.87780.bm@omp1064.mail.ac4.yahoo.com Received: (qmail 72994 invoked from network); 12 Jul 2012 13:40:09 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=V7z/k+iFv83GUel5oas5bIyBhQu3AEp1TH/Q5OMHQ6rykCUUtg070iJ956SK51F8UFsgpXEPT4w5lepHMyOOYSPAJnSXyOvdgK3jxYCJhaoUoe3MhkMxDtz0PWPZEo89ky9/QzrxUrXE+RzAFOiSxwLkSsKjENiuyZ814oM8N1Y= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1342100409; bh=zOPrr3fquLlSsjbqraYacH6vygJiOSWf2FSQTFCGNP8=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=0I9oi2RqeAkLBfWjug28JJNEM5rYSphWsp8eGkN8+mfp6LUQzRLQIMTb6c3gupigA0XtLbZ3D1CuCnQaD6PzQ+L+plAbyJ2ylEpUAuBoxurragxT85NWgVCLvx7zpHgp+jmDPeFz1VYVwoOMGRZh0QzWI88m3lRQglXyj5nSZ4E= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: .G9WMrQVM1lM5r705sC84iMHXwGMgilCHNPI1mq_fPmSsyI bNMmQ7wlR956Rt0TGuKJCsXEXrknC1GC_r.4RXXu3sp7MXfTLArlF4YBXEKa IA7cyjyt5eJFyQil_ZnaKNmfPQpvE670pA7DG733w2mxdvqePfFmol954Nl9 cL02aa3Tt6S0lP6qL0EDm9i.u.Cf0lfmOYLiLTgWppLxC1VZCLiK_LGsFZUM M0_RkRtK7z960GphMCqXnNroS7KComooJZYBLFOoMugc5ct1.9F7go0toj7X NyrAkFFdDwhzTMMzgYVZe_zPPXK8B.uPwUXWslKb74cggcOULWz1LvvguHg1 4uUPhHE_NEgbKXXMctk.HqYK6Ja5_07dY41vwHce00TEUWZ2SkkrBRrAjsB5 pMeonh_sbZbXXnFWGmd8VNwhN.shp_WYMeMsWQfUFZOLxrscFzmtZ5A13Mdl WR5dVvb84DKvkUmZ0A.uu.SyyrRFRB_Fwbz6VubYJyim1Tvlr X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.101] (polobo@24.93.23.188 with xymcookie) by smtp112-mob.biz.mail.ac4.yahoo.com with SMTP; 12 Jul 2012 06:40:09 -0700 PDT References: <4FFD3050.3010509@gmx.net> <20120711081607.GA17798@tux> <20120711082458.GA19013@tux> <20120712051445.GA5421@tux> <4FFE8E70.3060109@gmx.net> In-Reply-To: <4FFE8E70.3060109@gmx.net> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Message-Id: <54FCC33D-BEA3-4104-B241-D13E6C8EC717@yahoo.com> Cc: "pgsql-sql@postgresql.org" , Kretschmer Andreas , "M.Mamin@intershop.de" X-Mailer: iPad Mail (9B206) From: David Johnston Subject: Re: Prevent double entries ... no simple unique index Date: Thu, 12 Jul 2012 09:40:12 -0400 To: Andreas X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201207/12 X-Sequence-Number: 36749 On Jul 12, 2012, at 4:44, Andreas wrote: > Am 12.07.2012 07:14, schrieb Andreas Kretschmer: >> Marc Mamin wrote: >>=20 >>> A partial index would do the same, but requires less space: >>>=20 >>> create unique index on log(state) WHERE state IN (0,1); >>=20 >=20 >=20 > OK, nice :) >=20 > What if I have those states in a 3rd table? > So I can see a state-history of when a state got set by whom. >=20 >=20 > objects ( id serial PK, ... ) > events ( id serial PK, object_id integer FK on objects.id, ... ) >=20 > event_states ( id serial PK, event_id integer FK on events.id, state int= eger ) >=20 > There still should only be one event per object that has state 0 or 1. > Though here I don't have the object-id within the event_states-table. >=20 > Is it still possible to have a unique index that needs to span over a join= of events and event_states? >=20 No, all index columns must come from the same table. You would need to use a= trigger-based system to enforce your constraint. You can either have the triggers simply perform validation or you can create= a materialized view and create the partial index on that. You could also c= onsider creating an updatable view and avoid directly interacting with the t= hree individual tables. You could also just turn event states into a history table and leave the cur= rent state on the event table. David J.=