Search Postgresql Archives

Re: How to insert record only if primary key does not exist

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jul 2, 2012 at 12:19 AM, Craig Ringer <ringerc@xxxxxxxxxxxxx> wrote:
> How to insert record to this table only if primary key does not exist ?
>
>
> You want an operation that's called an UPSERT or MERGE operation. PostgreSQL
> doesn't have any native support to do this for you. Doing it right is
> surprisingly tricky. This is the best article I've seen on the topic:
>
> http://www.depesz.com/2012/06/10/why-is-upsert-so-complicated/

Though that's aiming for a perfectly universal solution. There are
simpler solutions that work in restricted circumstances; the easiest
may be simply:

SAVEPOINT tryinsert
INSERT .... -- as normal
-- if error:
ROLLBACK TO SAVEPOINT tryinsert

Question: Is it better to simply do the insert as-is, or to have a
WHERE clause that will often, though not always, prevent duplicate
insertions?

ChrisA

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux