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