Re: [GENERAL] locking/insert into table and transactions - Mailing list pgsql-general

From Lincoln Yeoh
Subject Re: [GENERAL] locking/insert into table and transactions
Date
Msg-id 3.0.5.32.19991126170409.00841b20@pop.mecomb.po.my
Whole thread Raw
In response to Re: [GENERAL] drop/rename table and transactions  (Lincoln Yeoh <lylyeoh@mecomb.com>)
List pgsql-general
Hi,

I'd like to prevent duplicate ids from being inserted into a table. I can
let the database enforce it by using UNIQUE or PRIMARY KEY. But assuming I
prefer to catch such things with the application, what would be the best
way of doing it?

The only way I figured to do it was to use:
begin;
lock table accounts;
select count(*) from accounts where id=$number;
 if count=0, insert into accounts (id,etc) values ($number,$etc)
commit;

Is this a good idea? Or is it much better and faster to let the database
catch things?

Is it faster to use "select count(*) from accounts" or "select id from
accounts"?

Apparently count(*) has some speed optimizations in MySQL. So wondering if
there are similar things in Postgres.

Thanks,

Link.


pgsql-general by date:

Previous
From: Oleg Broytmann
Date:
Subject: Re: [GENERAL] Postgres on Ultrasparc
Next
From: Mike Mascari
Date:
Subject: Re: [HACKERS] Re: [GENERAL] drop/rename table and transactions