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

Поиск
Список
Период
Сортировка
От Chris Angelico
Тема Re: How to insert record only if primary key does not exist
Дата
Msg-id CAPTjJmqsOXkO5FKf9gj1CbPWL5AG_VQ1xpL96T3-OUMiaq70NA@mail.gmail.com
обсуждение исходный текст
Ответ на Re: How to insert record only if primary key does not exist  (Craig Ringer <ringerc@ringerc.id.au>)
Список pgsql-general
On Mon, Jul 2, 2012 at 12:19 AM, Craig Ringer <ringerc@ringerc.id.au> 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

В списке pgsql-general по дате отправления:

Предыдущее
От: Craig Ringer
Дата:
Сообщение: Re: How to insert record only if primary key does not exist
Следующее
От: Iqbal Aroussi
Дата:
Сообщение: PostgreSQL Slony-I Replication