Re: Insert Or update

From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Insert Or update
Date: 2004-04-23 17:31:20
Message-ID: 87n05238k7.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Bruno Wolff III <bruno(at)wolff(dot)to> writes:

> This was discussed on the list over the last couple of days.
> There is no update or insert statement in postgres.
> You can do an update and check the number of rows affected and if it
> is 0 do the insert.

I prefer to do the insert and if it fails due to a unique key constraint then
do the update. If you don't have any deletes then this is safe from race
conditions whereas the update first method could fail if two people do updates
and then both try to insert.

In practice actually I rarely have to do this.

> However unless you lock the table while doing this, you have to be prepared
> to handle errors.

--
greg

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruno Wolff III 2004-04-23 17:41:15 Re: Insert Or update
Previous Message Igor Shevchenko 2004-04-23 17:17:10 Re: Insert Or update