Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 00BF5475909 for ; Thu, 8 Aug 2002 05:30:06 -0400 (EDT) Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by postgresql.org (Postfix) with ESMTP id 74196475903 for ; Thu, 8 Aug 2002 05:30:05 -0400 (EDT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 3.36 #2) id 17cjcI-0003vs-0U; Thu, 08 Aug 2002 10:30:03 +0100 Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id 2E85D12A29; Thu, 8 Aug 2002 10:20:06 +0100 (BST) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: Bhuvan A , pgsql-sql@postgresql.org Subject: Re: Effective usage without unique key -- suggestion Date: Thu, 8 Aug 2002 10:20:03 +0100 User-Agent: KMail/1.4.1 References: In-Reply-To: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200208081020.03233.dev@archonet.com> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/143 X-Sequence-Number: 8989 On Wednesday 07 Aug 2002 9:27 am, Bhuvan A wrote: > Hi, > > I am using postgresql 7.2.1 on redhat linux 7.3 > > I have a table in very high production database which is circulating > several thousands of records per day ie.. count does not exceed several > thousands. Here for some technical reason i dont have unique key on this > table, but it should contain unique records. I know that without using > unique index it can be achieved in any of the following methods. Why don't you create a unique index on the fields concerned? You can always= =20 create a functional index if uniqueness is a complicated thing for you. CREATE TABLE foo (a int, b int); CREATE FUNCTION foo_add(int, int) RETURNS int AS ' SELECT $1 + $2; ' LANGUAGE 'sql' WITH (iscachable); CREATE UNIQUE INDEX foo_uniq_idx ON foo ( foo_add(a,b) ); richardh=3D> INSERT INTO foo values (1,4); INSERT 7024674 1 richardh=3D> INSERT INTO foo values (2,4); INSERT 7024675 1 richardh=3D> INSERT INTO foo values (2,3); ERROR: Cannot insert a duplicate key into unique index foo_uniq_idx > Method 1 > * check for the record. > * if exists update, else insert > > Method 2 > * delete the record (trigger on before insert) > * insert the record > > So Can you please suggest the best among the above 2 methods? > > BTW, internals of the above 2 methods would be different. For example in > method 2, frequent deletion of records calls for vacuuming the database. > FYI, i does VACUUM ANALYZE every day. So Can you please suggest the best > among the above 2 methods which well suits me and to use postgres more > effectively? Don't think it matters which way you do it with MVCC - updating is equivale= nt=20 to a delete + insert. You say this table has a high rate of change, so you= =20 might want to VACUUM it more often than daily. - Richard Huxton