Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id AFB679FB412 for ; Thu, 19 Apr 2007 15:34:22 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 59600-01-7 for ; Thu, 19 Apr 2007 15:34:18 -0300 (ADT) X-Greylist: delayed 00:06:46.641202 by SQLgrey-1.7.4 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.250]) by postgresql.org (Postfix) with ESMTP id 4428C9FB4C6 for ; Thu, 19 Apr 2007 15:34:18 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c24so624412ana for ; Thu, 19 Apr 2007 11:34:17 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type; b=PKDKtwofdNCRP6l/NaStvu/zTSAuoJ+A/AlIbOAzaoJD/48YQZXoQhyKDdH70dfjlqN55kV8Qk+DPVM4d5bRuaj8EMF3WbpXDTfJrruJ9ErHYW90wsOnB+jCMdUKGb3ma4+KdOssLB2g/nH6xry34YHCwowDWSvRKaJGyDqkQSQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type; b=Z8bUxFAqxLhGOHgWqpagVjO6cWwuGzsNrBtKyiPSowuLlVY1DvZQffZHbcRG1VHDlpBVN9ThOMeVuxxEvx2DGtzqohSmNvXagYJd4+5mhQ2i0g2YjLD2Fa6JCzc70P5ar8GJxtBFUKSWi4w2v4g0xzu99n9b2hwNcqrcpjpd1Aw= Received: by 10.100.39.17 with SMTP id m17mr1120440anm.1177007250357; Thu, 19 Apr 2007 11:27:30 -0700 (PDT) Received: by 10.100.173.2 with HTTP; Thu, 19 Apr 2007 11:27:30 -0700 (PDT) Message-ID: <978075550704191127g781a4a26i400f4fc80d491df6@mail.gmail.com> Date: Thu, 19 Apr 2007 20:27:30 +0200 From: "Markus Holzer" To: pgsql-sql@postgresql.org Subject: Doing a conditional insert/update MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_106024_10101512.1177007250179" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/166 X-Sequence-Number: 28166 ------=_Part_106024_10101512.1177007250179 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 ------=_Part_106024_10101512.1177007250179 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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

------=_Part_106024_10101512.1177007250179--