X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 2390CD1B53A for ; Sat, 13 Sep 2003 20:16:27 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 74429-05 for ; Sat, 13 Sep 2003 17:15:56 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 1DED6D1B4F9 for ; Sat, 13 Sep 2003 17:15:54 -0300 (ADT) Received: (qmail 25318 invoked by uid 500); 13 Sep 2003 20:22:28 -0000 Date: Sat, 13 Sep 2003 15:22:28 -0500 From: Bruno Wolff III To: "G. Ralph Kuntz, MD" Cc: pgsql-sql@postgresql.org Subject: Re: Q: insert/update in same statement Message-ID: <20030913202228.GA25132@wolff.to> Mail-Followup-To: "G. Ralph Kuntz, MD" , pgsql-sql@postgresql.org References: <9881c752.0309120955.72b8460e@posting.google.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9881c752.0309120955.72b8460e@posting.google.com> User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/110 X-Sequence-Number: 15088 On Fri, Sep 12, 2003 at 10:55:17 -0700, "G. Ralph Kuntz, MD" 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.