X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 30EDBD1B18A for ; Thu, 15 Jul 2004 14:46:47 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 80736-05 for ; Thu, 15 Jul 2004 17:46:29 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 891EFD1B241 for ; Thu, 15 Jul 2004 14:46:08 -0300 (ADT) Received: (qmail 19170 invoked by uid 500); 15 Jul 2004 17:53:18 -0000 Date: Thu, 15 Jul 2004 12:53:18 -0500 From: Bruno Wolff III To: Jeff Kowalczyk Cc: pgsql-sql@postgresql.org Subject: Re: 'UPDATE OR INSERT' command Message-ID: <20040715175318.GA18875@wolff.to> Mail-Followup-To: Jeff Kowalczyk , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200407/95 X-Sequence-Number: 18347 On Thu, Jul 15, 2004 at 13:20:57 -0500, Jeff Kowalczyk wrote: > Is there a postgresql SQL idiom to perform an UPDATE, which becomes an > INSERT if the primary key does not exist? > > I'm not sure I *should* use it in my application, I just want to know if > it can be done. Thanks. There isn't a single statement that does this. This has been discussed a number of times. The archives will have some different examples. You end up needing to either lock the table or check for failure since postgres doesn't have predicate locking. Which technique is best will depend on the details of your situation.