pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Markus Holzer <holli.holzer@googlemail.com>
To: pgsql-sql@postgresql.org
Subject: Doing a conditional insert/update
Date: Thu, 19 Apr 2007 20:27:30 +0200
Message-ID: <978075550704191127g781a4a26i400f4fc80d491df6@mail.gmail.com> (raw)

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

view thread (3+ messages)  latest in thread

Message-ID: <978075550704191127g781a4a26i400f4fc80d491df6@mail.gmail.com>
Permalink:  ../978075550704191127g781a4a26i400f4fc80d491df6@mail.gmail.com/
Also on:    postgresql.org/message-id/978075550704191127g781a4a26i400f4fc80d491df6@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: holli.holzer@googlemail.com
  Subject: Re: Doing a conditional insert/update
  In-Reply-To: <978075550704191127g781a4a26i400f4fc80d491df6@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox