X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id D54F9DB058 for ; Tue, 22 Nov 2005 13:50:45 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 90234-01 for ; Tue, 22 Nov 2005 17:50:44 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 027CEDB024 for ; Tue, 22 Nov 2005 13:50:42 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id jAMHogSF012773; Tue, 22 Nov 2005 12:50:42 -0500 (EST) To: fmiddleton@verizon.net cc: pgsql-sql@postgresql.org Subject: Re: can UNIQUEness of TEXT datatype really be guaranteed? In-reply-to: <2238206.1132680900217.JavaMail.root@vms068.mailsrvcs.net> References: <2238206.1132680900217.JavaMail.root@vms068.mailsrvcs.net> Comments: In-reply-to message dated "Tue, 22 Nov 2005 11:35:00 -0600" Date: Tue, 22 Nov 2005 12:50:42 -0500 Message-ID: <12772.1132681842@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.003 required=5 tests=[AWL=0.003] X-Spam-Score: 0.003 X-Spam-Level: X-Archive-Number: 200511/209 X-Sequence-Number: 23460 writes: > So I can't help but wonder, can Postgres really guarantee a TEXT field to be UNIQUE... or is declaring a TEXT field UNIQUE something an uninformed, novice user would do?... or is it something indicative of the strength and/or weeknesses that separate the functionality of the two DBMSs. In PG, it will work as long as no entry is too large to fit into a btree index entry (from memory, about 2700 bytes after compression, so the practical limit is probably 4KB or so). If you think you might have entries exceeding a few KB, you could use the trick of declaring a unique functional index on a checksum: create unique index myindex on mytable (md5(fieldname)); This will work as long as you don't get any md5 hash collisions, which is probably not a problem in practice. It will guarantee uniqueness in any case; the risk is that you might get false matches causing rejection of inputs that actually are distinct. A possibly simpler-to-understand way is to demand uniqueness in the first couple KB: create unique index myindex on mytable (substr(fieldname,1,2000)); regards, tom lane