Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 8A81B9FA621 for ; Wed, 27 Sep 2006 18:42:29 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 62398-02 for ; Wed, 27 Sep 2006 21:42:25 +0000 (UTC) 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 677189FA1DE for ; Wed, 27 Sep 2006 18:42:24 -0300 (ADT) Received: by noel.decibel.org (Postfix, from userid 1001) id D49B456423; Wed, 27 Sep 2006 16:42:22 -0500 (CDT) Received: (hashcash-sendmail, from uid 1001); Wed, 27 Sep 2006 16:42:22 -0500 Date: Wed, 27 Sep 2006 16:42:22 -0500 From: "Jim C. Nasby" To: Matthew Schumacher Cc: pgsql-performance@postgresql.org Subject: Re: Problems with inconsistant query performance. Message-ID: <20060927214221.GA19827@nasby.net> References: <451AC4E2.90106@aptalaska.net> <20060927203155.GT19827@nasby.net> <451AEE15.6090103@aptalaska.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <451AEE15.6090103@aptalaska.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:060927:matt.s@aptalaska.net::3tJaZBlWmCT64zff:00000000000000000 00000000000000000000000054V2 X-Hashcash: 1:20:060927:pgsql-performance@postgresql.org::YviuBbp1CxIWGxHw:00000 00000000000000000000000046UY X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200609/374 X-Sequence-Number: 21060 On Wed, Sep 27, 2006 at 01:33:09PM -0800, Matthew Schumacher wrote: > Jim, > > Thanks for the help. I went and looked at that example and I don't see > how it's different than the "INSERT into radutmp_tab" I'm already doing. > Both raise an exception, the only difference is that I'm not doing > anything with it. Perhaps you are talking about the "IF (NOT FOUND)" I > put after the "UPDATE radutmp_tab". Should this be an EXCEPTION > instead? Also I don't know how this could cause a race condition. As > far as I understand each proc is run in it's own transaction, and the > code in the proc is run serially. Can you explain more why this could > case a race? It can cause a race if another process could be performing those same inserts or updates at the same time. I know the UPDATE case can certainly cause a race. 2 connections try to update, both hit NOT FOUND, both try to insert... only one will get to commit. I think that the UNIQUE_VIOLATION case should be safe, since a second inserter should block if there's another insert that's waiting to commit. DELETEs are something else to think about for both cases. If you're certain that only one process will be performing DML on those tables at any given time, then what you have is safe. But if that's the case, I'm thinking you should be able to group things into chunks, which should be more efficient. > Thanks, > schu > > > > Jim C. Nasby wrote: > > Periodically taking longer is probably a case of some other process in > > the database holding a lock you need, or otherwise bogging the system > > down, especially if you're always running acctmessage from the same > > connection (because the query plans shouldn't be changing then). I'd > > suggest looking at what else is happening at the same time. > > > > Also, it's more efficient to operate on chunks of data rather than one > > row at a time whenever possible. If you have to log each row > > individually, consider simply logging them into a table, and then > > periodically pulling data out of that table to do additional processing > > on it. > > > > BTW, your detection of duplicates/row existance has a race condition. > > Take a look at example 36-1 at > > http://www.postgresql.org/docs/8.1/interactive/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING > > for a better way to handle it. > > >> ========================================================================== > >> CREATE acctmessage( )RETURNS void AS $$ > >> BEGIN > >> INSERT into tmpaccounting_tab ( ... ) values ( ... ); > >> > >> IF _acctType = 'start' THEN > >> BEGIN > >> INSERT into radutmp_tab ( ... ) valuse ( ... ); > >> EXCEPTION WHEN UNIQUE_VIOLATION THEN > >> NULL; > >> END; > >> ELSIF _acctType = 'stop' THEN > >> UPDATE radutmp_tab SET ... WHERE sessionId = _sessionId AND userName = > >> _userName; > >> IF (NOT FOUND) THEN > >> INSERT into radutmp_tab ( ... ) values ( ... ); > >> END IF; > >> > >> END IF; > >> END; > >> $$ > >> LANGUAGE plpgsql; > >> ========================================================================== > -- Jim Nasby jim@nasby.net EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)