Re: [GENERAL] locking/insert into table and transactions
Lincoln Yeoh <lylyeoh@mecomb.com>
From: Lincoln Yeoh <lylyeoh@mecomb.com>
To: pgsql-general@postgreSQL.org
Date: 1999-11-26T09:04:09Z
Lists: pgsql-hackers
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.