pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: G. Ralph Kuntz, MD <grk@usa.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: Q: insert/update in same statement
Date: Sat, 13 Sep 2003 15:22:28 -0500
Message-ID: <20030913202228.GA25132@wolff.to> (raw)
In-Reply-To: <9881c752.0309120955.72b8460e@posting.google.com>
References: <9881c752.0309120955.72b8460e@posting.google.com>

On Fri, Sep 12, 2003 at 10:55:17 -0700,
  "G. Ralph Kuntz, MD" <grk@usa.net> wrote:
> Is it possible to make a single SQL statement that will insert a row
> if it does not already exist, otherwise it will just update the
> fields?

No.

> Actually, just a statement that will not give an error about duplicate
> keys would be enough for my application.

This you can do by using a select statement that checks if the primary
key is a duplicate on the insert statement. An example was posted on
one of the lists about a week ago.

Note that there are conncurrency issues with doing this. Either you want
to exclusively lock the table or use serializable isolation and be
prepared to retry after failures.



view thread (2+ messages)

Message-ID: <20030913202228.GA25132@wolff.to>
Permalink:  ../20030913202228.GA25132@wolff.to/
Also on:    postgresql.org/message-id/20030913202228.GA25132@wolff.to

 · 

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: bruno@wolff.to, grk@usa.net
  Subject: Re: Q: insert/update in same statement
  In-Reply-To: <20030913202228.GA25132@wolff.to>

* 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