Doing a conditional insert/update

From: "Markus Holzer" <holli(dot)holzer(at)googlemail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Doing a conditional insert/update
Date: 2007-04-19 18:27:30
Message-ID: 978075550704191127g781a4a26i400f4fc80d491df6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello.

I'm currently developing my first web app with Postgres and I have a
question.

How do I perform a conditional insert/update?

To clarify: I need to insert data into a table when the primary key is not
already in the table, or an update if it is. I have currently solved this by
SELECTing for the primary key, then looking if there is a row, and if there
is I do an UPDATE otherwise I do an INSERT. But since this is a web app this
way of course leaves a big race condition.

Is there a way to get around that? Like the ON DUPLICATE KEY UPDATE
statement in MySQL?

Thanks for your time,

Holli

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Andrew Sullivan 2007-04-19 18:55:21 Re: Doing a conditional insert/update
Previous Message Stuart McGraw 2007-04-19 14:20:28 Re: slowness when subselect uses DISTINCT