pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Bhuvan A <bhuvansql@linuxfreemail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Effective usage without unique key -- suggestion
Date: Thu, 8 Aug 2002 10:20:03 +0100
Message-ID: <200208081020.03233.dev@archonet.com> (raw)
In-Reply-To: <Pine.LNX.4.44.0208071353500.2273-100000@Bhuvan.bksys.co.in>
References: <Pine.LNX.4.44.0208071353500.2273-100000@Bhuvan.bksys.co.in>

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 
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=> INSERT INTO foo values (1,4);
INSERT 7024674 1
richardh=> INSERT INTO foo values (2,4);
INSERT 7024675 1
richardh=> 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 equivalent 
to a delete + insert. You say this table has a high rate of change, so you 
might want to VACUUM it more often than daily.

- Richard Huxton



view thread (3+ messages)  latest in thread

Message-ID: <200208081020.03233.dev@archonet.com>
Permalink:  ../200208081020.03233.dev@archonet.com/
Also on:    postgresql.org/message-id/200208081020.03233.dev@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, bhuvansql@linuxfreemail.com
  Subject: Re: Effective usage without unique key -- suggestion
  In-Reply-To: <200208081020.03233.dev@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox