Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 5CDC9477261 for ; Fri, 27 Sep 2002 09:01:01 -0400 (EDT) Received: from anchor-post-30.mail.demon.net (anchor-post-30.mail.demon.net [194.217.242.88]) by postgresql.org (Postfix) with ESMTP id BD1E247731A for ; Fri, 27 Sep 2002 08:59:47 -0400 (EDT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-30.mail.demon.net with esmtp (Exim 3.35 #1) id 17uuik-000FQL-0U; Fri, 27 Sep 2002 13:59:50 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 78E1D163D4; Fri, 27 Sep 2002 13:55:46 +0100 (BST) Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id E7C04163BB; Fri, 27 Sep 2002 13:55:45 +0100 (BST) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: Kevin Houle , pgsql-sql@postgresql.org Subject: Re: Dublicates pairs in a table. Date: Fri, 27 Sep 2002 13:54:54 +0100 User-Agent: KMail/1.4.3 References: <000201c25d90$87230c10$6500000a@IT000> <3D910CEB.30704@houle.org> In-Reply-To: <3D910CEB.30704@houle.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200209271354.54187.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/377 X-Sequence-Number: 9654 On Wednesday 25 Sep 2002 2:10 am, Kevin Houle wrote: > I have the same issue with a table that currently holds well > over 600,000 rows. The case you left out is this: > > INSERT INTO test (c1,c2) VALUES('a','c'); > INSERT INTO test (c1,c2) VALUES('c','a'); > > I want that to fail, but I haven't been able to get it to fail > using unique indexes. I presume ordering is significant. Instead, > I am doing a SELECT prior to insert to insure the pair doesn't > already exist. If you've been able to get order-independent > pairs restricted to being unique using indexes, I'd like to know > about it. :-) Functional indexes sir - define a function that puts the columns into a sor= ted=20 order. richardh=3D> CREATE TABLE foo (a text, b text); CREATE richardh=3D> CREATE UNIQUE INDEX foo_both_uniq ON foo ( ord_fn(a,b) ); ERROR: DefineIndex: index function must be marked iscachable richardh=3D> \i ordfn.txt DROP CREATE richardh=3D> CREATE UNIQUE INDEX foo_both_uniq ON foo ( ord_fn(a,b) ); CREATE richardh=3D> insert into foo values ('aa','bb'); INSERT 332596 1 richardh=3D> insert into foo values ('aa','cc'); INSERT 332597 1 richardh=3D> insert into foo values ('bb','aa'); ERROR: Cannot insert a duplicate key into unique index foo_both_uniq richardh=3D> insert into foo values ('aa','bb'); ERROR: Cannot insert a duplicate key into unique index foo_both_uniq Function defined as: CREATE FUNCTION ord_fn (text,text) RETURNS text AS ' SELECT (CASE WHEN $1 < $2 THEN $1 || $2 ELSE $2 || $1 END) as t; ' LANGUAGE SQL WITH (iscachable); --=20 Richard Huxton