Re: I often have to do "update if exist, else insert", is my database design wrong?

From: Steve Atkins <steve(at)blighty(dot)com>
To: pgsql-general General <pgsql-general(at)postgresql(dot)org>
Subject: Re: I often have to do "update if exist, else insert", is my database design wrong?
Date: 2008-07-25 18:59:57
Message-ID: FEB351BC-BCA6-4E5B-A650-F985216BD356@blighty.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Jul 25, 2008, at 11:46 AM, Francisco Reyes wrote:

>> Is this normal or are there something else I could do so I don't have
>> to check if it exists?
>
> I would say that it is normal.
>
>
>> For the developers: a combined insert/update command would be nice
>> :-)
>
> Mysql has such a beast along a some other non SQL compliant
> extensions.

MERGE isn't trivial, but the developers are well aware of the desire
for it.

>
>
> One possible approach to what you are trying to do would be:
> update existing table with new values
> delete from new table all records that already exist
> insert remaining new records
>
> Which can be done all in pure sql. No need to write a program.

Well... the race conditions may bite you there, if you have concurrent
access.

This - http://www.postgresql.org/docs/8.3/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE
- is the usual approach suggested for a concurrent-access safe
upsert/merge right now.

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Christophe 2008-07-25 19:01:21 Re: php + postgresql
Previous Message Francisco Reyes 2008-07-25 18:46:06 Re: I often have to do "update if exist, else insert", is my database design wrong?