X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1BAC85294D for ; Wed, 17 Aug 2005 23:30:27 -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 03288-01 for ; Thu, 18 Aug 2005 02:30:21 +0000 (GMT) Received: from linuxworld.com.au (unknown [203.34.46.50]) by svr1.postgresql.org (Postfix) with ESMTP id 33B4C5282E for ; Wed, 17 Aug 2005 23:30:19 -0300 (ADT) Received: from linuxworld.com.au (IDENT:swm@localhost.localdomain [127.0.0.1]) by linuxworld.com.au (8.13.2/8.13.2) with ESMTP id j7I2UORY022238; Thu, 18 Aug 2005 12:30:24 +1000 Received: from localhost (swm@localhost) by linuxworld.com.au (8.13.2/8.13.2/Submit) with ESMTP id j7I2UOqn022235; Thu, 18 Aug 2005 12:30:24 +1000 X-Authentication-Warning: linuxworld.com.au: swm owned process doing -bs Date: Thu, 18 Aug 2005 12:30:24 +1000 (EST) From: Gavin Sherry X-X-Sender: swm@linuxworld.com.au To: Alan Stange Cc: pgsql-performance@postgresql.org Subject: Re: limit number of concurrent callers to a stored proc? In-Reply-To: <4303E704.4030200@rentec.com> Message-ID: References: <4303E704.4030200@rentec.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.002 required=5 tests=[AWL=0.002] X-Spam-Level: X-Archive-Number: 200508/223 X-Sequence-Number: 13970 Hi Alan, On Wed, 17 Aug 2005, Alan Stange wrote: > Hello all, > > is there a simple way to limit the number of concurrent callers to a > stored proc? > > The problem we have is about 50 clients come and perform the same > operation at nearly the same time. Typically, this query takes a few > seconds to run, but in the case of this thundering herd the query time > drops to 70 seconds or much more. The query can return up to 15MB of data. > > The machine is a dual opteron, 8 GB memory, lots of fiber channel disk, > Linux 2.6, etc. > > So, I'm thinking that a semaphore than will block more than N clients > from being in the core of the function at one time would be a good thing. There is no PostgreSQL feature which will do this for you. It should be possible to implement this yourself, without too much pain. If you're using PL/PgSQL, write another function in C or one of the other more sophisticated PLs to implement the logic for you. At the beginning of the function, execute the function to increment the count; at the end, execute a function to decrement it. If you're writing the function in C or one of those more sophisticated PLs, it's even easier. As an aside, using semaphores might be a little painful. I'd just grab some shared memory and keep a counter in it. If the counter is greater than your desired number of concurrent executions, you sleep and try again soon. That being said, did you want to give us a look at your function and data and see if we can improve the performance at all? Thanks, Gavin