From waldomiro@shx.com.br Tue May 30 18:53:39 2006 X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 124779FB0D1 for ; Tue, 30 May 2006 15:53:39 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 97476-05 for ; Tue, 30 May 2006 15:53:34 -0300 (ADT) X-Greylist: delayed 00:27:47.580025 by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 628B09FB1B9 for ; Tue, 30 May 2006 15:53:30 -0300 (ADT) Received: from burta.hst.terra.com.br (burta.hst.terra.com.br [200.176.10.199]) by svr4.postgresql.org (Postfix) with ESMTP id 8E1795AFA09 for ; Tue, 30 May 2006 18:25:42 +0000 (GMT) Received: from caliope.hst.terra.com.br (caliope.hst.terra.com.br [200.176.10.8]) by burta.hst.terra.com.br (Postfix) with ESMTP id 24EE64B78094 for ; Tue, 30 May 2006 15:25:35 -0300 (BRT) X-Terra-Karma: -2% X-Terra-Hash: c8335fc1d0ecf755e79b71f0e33385bb Received-SPF: none (caliope.hst.terra.com.br: 200.176.10.8 is neither permitted nor denied by domain of shx.com.br) client-ip=200.176.10.8; envelope-from=waldomiro@shx.com.br; helo=[192.168.3.243]; Received: from [192.168.3.243] (unknown [200.207.161.27]) (authenticated user shx134) by caliope.hst.terra.com.br (Postfix) with ESMTP id BDA4D1BD4129 for ; Tue, 30 May 2006 15:25:34 -0300 (BRT) Message-ID: <447C8F1D.8090404@shx.com.br> Date: Tue, 30 May 2006 15:29:49 -0300 From: Waldomiro User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: pt-br, pt MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: INSERT OU UPDATE WITHOUT SELECT? Content-Type: multipart/alternative; boundary="------------090707040903070803030802" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.305 tagged_above=0 required=5 tests=BAYES_00, HTML_50_60, HTML_MESSAGE, HTML_TITLE_EMPTY, RCVD_IN_BL_SPAMCOP_NET, SUBJ_ALL_CAPS X-Spam-Level: X-Archive-Number: 200605/549 X-Sequence-Number: 19336 This is a multi-part message in MIME format. --------------090707040903070803030802 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi, Is there a command to Insert a record If It does not exists and a update if It exists? I do not want to do a select before a insert or update. I mean the postgres should test if a record exist before insert and if It exist then the postgres must do an update instead an insert. Thanks, WMiro. --------------090707040903070803030802 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi,

Is there a command to Insert a record If It does not exists and a update if It exists?

I do not want to do a select before a insert or update.

I mean the postgres should test if a record exist before insert and if It exist then the postgres must do an update instead an insert.

Thanks,

WMiro.


--------------090707040903070803030802-- From jonah.harris@gmail.com Tue May 30 21:05:25 2006 X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 9B1809FAE4E for ; Tue, 30 May 2006 18:05:25 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 17455-03 for ; Tue, 30 May 2006 18:05:20 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.186]) by postgresql.org (Postfix) with ESMTP id BBD3B9FA5E9 for ; Tue, 30 May 2006 18:05:19 -0300 (ADT) Received: by nf-out-0910.google.com with SMTP id y25so14354nfb for ; Tue, 30 May 2006 14:05:18 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=F8b0lzP3vGYMi8JaKMEZvvCxyOfvHmVUmjg5qkuySlnV+qu0Zf8kiV3y6XGcxrkpwFD8MMAD0DAyOXzEQLrdH5EYYZSjspGLBzPpSVEhqKq1QI1PpKRmCL3GnlHQAAWgov4TpOo/o8Ej7326ZM9khlynVx1Mn1SSunJr4ZxG8NY= Received: by 10.48.218.19 with SMTP id q19mr44804nfg; Tue, 30 May 2006 14:05:17 -0700 (PDT) Received: by 10.67.87.1 with HTTP; Tue, 30 May 2006 14:05:17 -0700 (PDT) Message-ID: <36e682920605301405u4d665fcftbe0a97b5753f5209@mail.gmail.com> Date: Tue, 30 May 2006 17:05:17 -0400 From: "Jonah H. Harris" To: Waldomiro Subject: Re: INSERT OU UPDATE WITHOUT SELECT? Cc: pgsql-performance@postgresql.org In-Reply-To: <447C8F1D.8090404@shx.com.br> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <447C8F1D.8090404@shx.com.br> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/550 X-Sequence-Number: 19337 On 5/30/06, Waldomiro wrote: > Is there a command to Insert a record If It does not exists and a update if > It exists? Sure, it's called MERGE. See http://en.wikipedia.org/wiki/Merge_%28SQL%29 > I mean the postgres should test if a record exist before insert and if It > exist then the postgres must do an update instead an insert. PostgreSQL does not support MERGE at the moment, sorry. -- Jonah H. Harris, Software Architect | phone: 732.331.1300 EnterpriseDB Corporation | fax: 732.331.1301 33 Wood Ave S, 2nd Floor | jharris@enterprisedb.com Iselin, New Jersey 08830 | http://www.enterprisedb.com/ From lists@peufeu.com Tue May 30 22:35:14 2006 X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 3CD779FA5E9 for ; Tue, 30 May 2006 19:35:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 25946-04 for ; Tue, 30 May 2006 19:35:10 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from peufeu.com (boutiquenumerique.com [82.67.9.10]) by postgresql.org (Postfix) with ESMTP id E053A9FA5E1 for ; Tue, 30 May 2006 19:35:09 -0300 (ADT) Received: (qmail 16389 invoked from network); 31 May 2006 00:35:16 +0200 Received: from boutiquenumerique.com (HELO apollo13) (82.67.9.10) by boutiquenumerique.com with SMTP; 31 May 2006 00:35:16 +0200 To: "Jonah H. Harris" , Waldomiro Cc: pgsql-performance@postgresql.org Subject: Re: INSERT OU UPDATE WITHOUT SELECT? References: <447C8F1D.8090404@shx.com.br> <36e682920605301405u4d665fcftbe0a97b5753f5209@mail.gmail.com> Message-ID: Date: Wed, 31 May 2006 00:35:15 +0200 From: PFC Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In-Reply-To: <36e682920605301405u4d665fcftbe0a97b5753f5209@mail.gmail.com> User-Agent: Opera M2/8.54 (Linux, build 1745) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/553 X-Sequence-Number: 19340 > PostgreSQL does not support MERGE at the moment, sorry. Issue an UPDATE, and watch the rowcount ; if the rowcount is 0, issue an INSERT. Be prepared to retry if another transaction has inserted the row meanwhile, though. MERGE would be really useful. From alvherre@commandprompt.com Tue May 30 22:38:25 2006 X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 298A89FA5E1 for ; Tue, 30 May 2006 19:38:25 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 25230-06 for ; Tue, 30 May 2006 19:38:20 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from alvh.no-ip.org (201-221-200-231.bk11-dsl.surnet.cl [201.221.200.231]) by postgresql.org (Postfix) with ESMTP id 145679FA5E9 for ; Tue, 30 May 2006 19:38:20 -0300 (ADT) Received: by alvh.no-ip.org (Postfix, from userid 1000) id 6D650C3A214; Tue, 30 May 2006 18:38:18 -0400 (CLT) Date: Tue, 30 May 2006 18:38:18 -0400 From: Alvaro Herrera To: PFC Cc: "Jonah H. Harris" , Waldomiro , pgsql-performance@postgresql.org Subject: Re: INSERT OU UPDATE WITHOUT SELECT? Message-ID: <20060530223818.GK31547@surnet.cl> Mail-Followup-To: PFC , "Jonah H. Harris" , Waldomiro , pgsql-performance@postgresql.org References: <447C8F1D.8090404@shx.com.br> <36e682920605301405u4d665fcftbe0a97b5753f5209@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.11+cvs20060403 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/555 X-Sequence-Number: 19342 PFC wrote: > >PostgreSQL does not support MERGE at the moment, sorry. > > Issue an UPDATE, and watch the rowcount ; if the rowcount is 0, > issue an INSERT. > Be prepared to retry if another transaction has inserted the row > meanwhile, though. Oh, you mean, like the example that's in the documentation? http://www.postgresql.org/docs/8.1/static/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING Example 36-1 > MERGE would be really useful. It has been discussed before -- MERGE is something different. -- Alvaro Herrera http://www.CommandPrompt.com/ PostgreSQL Replication, Consulting, Custom Development, 24x7 support From dave@tridecap.com Tue May 30 22:54:10 2006 X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 5D0689FA5E9 for ; Tue, 30 May 2006 19:54:10 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 29494-01 for ; Tue, 30 May 2006 19:54:04 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from globalrelay.com (mail1.globalrelay.com [216.18.71.77]) by postgresql.org (Postfix) with ESMTP id 569BC9FA5E1 for ; Tue, 30 May 2006 19:54:02 -0300 (ADT) X-Virus-Scanned: Scanned by GRC-AntiVirus Gateway X-GR-Acctd: YES Received: from [67.90.96.2] (HELO DaveEMachine) by globalrelay.com (CommuniGate Pro SMTP 4.2.3) with ESMTP id 91957859; Tue, 30 May 2006 15:54:00 -0700 From: "Dave Dutcher" To: "'PFC'" Cc: Subject: Re: INSERT OU UPDATE WITHOUT SELECT? Date: Tue, 30 May 2006 17:54:00 -0500 Message-ID: <039201c6843b$f11eeaf0$8300a8c0@tridecap.com> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 In-Reply-To: Importance: Normal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/556 X-Sequence-Number: 19343 What I do when I'm feeling lazy is execute a delete statement and then an insert. I only do it when I'm inserting/updating a very small number of rows, so I've never worried if its optimal for performance. Besides I've heard that an update in postgres is similar in performance to a delete/insert. > -----Original Message----- > From: pgsql-performance-owner@postgresql.org > [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of PFC > Sent: Tuesday, May 30, 2006 5:35 PM > To: Jonah H. Harris; Waldomiro > Cc: pgsql-performance@postgresql.org > Subject: Re: [PERFORM] INSERT OU UPDATE WITHOUT SELECT? > > > > PostgreSQL does not support MERGE at the moment, sorry. > > Issue an UPDATE, and watch the rowcount ; if the > rowcount is 0, issue an > INSERT. > Be prepared to retry if another transaction has > inserted the row > meanwhile, though. > > MERGE would be really useful. > > > ---------------------------(end of > broadcast)--------------------------- > TIP 5: don't forget to increase your free space map settings > From darcy@druid.net Tue May 30 23:05:23 2006 X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 5702B9FA5E9 for ; Tue, 30 May 2006 20:05:23 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 29391-03 for ; Tue, 30 May 2006 20:05:13 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from druid.net (druid.net [66.96.28.249]) by postgresql.org (Postfix) with ESMTP id 6EA0F9FA5E1 for ; Tue, 30 May 2006 20:05:13 -0300 (ADT) Received: from dilbert (H245.C18.B96.tor.eicat.ca [66.96.18.245]) by druid.net (Postfix) with SMTP id BAD41B483; Tue, 30 May 2006 19:05:12 -0400 (EDT) Date: Tue, 30 May 2006 19:05:08 -0400 From: "D'Arcy J.M. Cain" To: "Dave Dutcher" Cc: lists@peufeu.com, pgsql-performance@postgresql.org Subject: Re: INSERT OU UPDATE WITHOUT SELECT? Message-Id: <20060530190508.aa47bafe.darcy@druid.net> In-Reply-To: <039201c6843b$f11eeaf0$8300a8c0@tridecap.com> References: <039201c6843b$f11eeaf0$8300a8c0@tridecap.com> X-Mailer: Sylpheed version 2.2.4 (GTK+ 2.8.12; i386--netbsdelf) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/557 X-Sequence-Number: 19344 On Tue, 30 May 2006 17:54:00 -0500 "Dave Dutcher" wrote: > What I do when I'm feeling lazy is execute a delete statement and then > an insert. I only do it when I'm inserting/updating a very small number > of rows, so I've never worried if its optimal for performance. Besides > I've heard that an update in postgres is similar in performance to a > delete/insert. Well, they are basically the same operation in PostgreSQL. An update adds a row to the end and marks the old one dead. A delete/insert marks the row dead and adds one at the end. There may be some optimization if the engine does both in one operation. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From jnasby@pervasive.com Wed May 31 06:29:16 2006 X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id D9F2F9FA60C for ; Wed, 31 May 2006 03:29:16 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 90795-07 for ; Wed, 31 May 2006 03:29:11 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from noel.decibel.org (noel.decibel.org [67.100.216.10]) by postgresql.org (Postfix) with ESMTP id 5F4739F9312 for ; Wed, 31 May 2006 03:29:11 -0300 (ADT) Received: by noel.decibel.org (Postfix, from userid 1001) id E863E56427; Wed, 31 May 2006 01:29:09 -0500 (CDT) Received: (hashcash-sendmail, from uid 1001); Wed, 31 May 2006 01:29:08 -0500 Date: Wed, 31 May 2006 01:29:08 -0500 From: "Jim C. Nasby" To: "D'Arcy J.M. Cain" Cc: Dave Dutcher , lists@peufeu.com, pgsql-performance@postgresql.org Subject: Re: INSERT OU UPDATE WITHOUT SELECT? Message-ID: <20060531062908.GH53487@pervasive.com> References: <039201c6843b$f11eeaf0$8300a8c0@tridecap.com> <20060530190508.aa47bafe.darcy@druid.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060530190508.aa47bafe.darcy@druid.net> X-Operating-System: FreeBSD 6.0-RELEASE-p4 amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.11 X-Hashcash: 1:20:060531:darcy@druid.net::pj2/PI7/HcMYvMQM:000dj+ X-Hashcash: 1:20:060531:dave@tridecap.com::aw5YZ6UJxfHQFf3s:00000000000000000000 00000000000000000000000003cr X-Hashcash: 1:20:060531:lists@peufeu.com::gpbsp/64o8yoqcFx:06t/U X-Hashcash: 1:20:060531:pgsql-performance@postgresql.org::GHTwWKOE9R4VDBvm:00000 0000000000000000000000006QyV X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/563 X-Sequence-Number: 19350 On Tue, May 30, 2006 at 07:05:08PM -0400, D'Arcy J.M. Cain wrote: > On Tue, 30 May 2006 17:54:00 -0500 > "Dave Dutcher" wrote: > > What I do when I'm feeling lazy is execute a delete statement and then > > an insert. I only do it when I'm inserting/updating a very small number > > of rows, so I've never worried if its optimal for performance. Besides > > I've heard that an update in postgres is similar in performance to a > > delete/insert. > > Well, they are basically the same operation in PostgreSQL. An update > adds a row to the end and marks the old one dead. A delete/insert > marks the row dead and adds one at the end. There may be some > optimization if the engine does both in one operation. The new tuple will actually go on the same page during an update, if possible. If not, the FSM is consulted. Appending to the end of the table is a last resort. Update is more effecient than delete/insert. First, it's one less statement to parse and plan. Second, AFAIK insert always goes to the FSM; it has no way to know you're replacing the row(s) you just deleted. -- Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com Pervasive Software http://pervasive.com work: 512-231-6117 vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461