Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 71B3D633788 for ; Tue, 3 Mar 2009 21:37:47 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 60715-02 for ; Tue, 3 Mar 2009 21:37:43 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from yx-out-2324.google.com (yx-out-2324.google.com [74.125.44.29]) by mail.postgresql.org (Postfix) with ESMTP id DC1DC632330 for ; Tue, 3 Mar 2009 21:37:43 -0400 (AST) Received: by yx-out-2324.google.com with SMTP id 3so1776170yxj.73 for ; Tue, 03 Mar 2009 17:37:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=nhI1lTDblheUyQv0SbVY1WPKEdVAJ4TcvthfEra5hc4=; b=A4xDw8DG1qh3XgJ1ChPY62zXSTBjjPuVe136TTeIICqvo7Kp1YSUTp9FYbRDwpiOlq f161IEOECNFP5W2N8grdX74yWd1DPxweiPk4qVK1G0LTRkllgZG7iV4RXdbtTmm9NQmx IJ3n8ubXEmEeGrUxWD9hGibTlTm9WDkV8PkNE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=FTGfWb/1wt6sN8+uA8X0i2tH1EMGlj6f4e+tC1hp8BwM3+OmCw+XFHryMuRIgG8ukz /7URl2KPDfKtiw1xipCaS2pq0lDtaFAZdjmdiLEWP62ozB+rYeYe52TttzxKjfib8sVB B7G+Q5hrQ7sZi/+y1+yanC5TKEL4Z8WV6eGrM= MIME-Version: 1.0 Received: by 10.151.101.20 with SMTP id d20mr13553071ybm.185.1236130662611; Tue, 03 Mar 2009 17:37:42 -0800 (PST) In-Reply-To: <30792576.96971236126536868.JavaMail.root@mail.4linux.com.br> References: <29144661.96951236126415852.JavaMail.root@mail.4linux.com.br> <30792576.96971236126536868.JavaMail.root@mail.4linux.com.br> Date: Tue, 3 Mar 2009 18:37:42 -0700 Message-ID: Subject: Re: work_mem in high transaction rate database From: Scott Marlowe To: Flavio Henrique Araque Gurgel Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200903/39 X-Sequence-Number: 32961 On Tue, Mar 3, 2009 at 5:28 PM, Flavio Henrique Araque Gurgel wrote: > Hello all > > In a dedicated server with 16 cores and 16GB of RAM running PostgreSQL 8.2.5 > we have a database with basically two kinds of transactions: > - short transactions with a couple of updates and inserts that runs all the > day; > - batch data loads with hundreds of inserts that runs several times a day; > - one delete for thousands of lines after each batch; > - selects are made when users need reports, low concurrency here. > > Today the max_connections is ~2500 where the application is a cluster of > JBoss servers with a pool a bit smaller then this total. > work_mem = 1GB > maintenance_work_mem = 1GB > shared_buffers = 4GB Oh my lord, that is a foot gun waiting to go off. Assuming 2k connections, and somehow a fair number of them went active with big sorts, you'd be able to exhaust all physical memory with about 8 to 16 connections. Lower work_mem now. To something like 1 to 4 Meg. Do not pass go. If some oddball query really needs a lot of work_mem, and benchmarks show something larger work_mem helps, consider raising the work_mem setting for that one query to something under 1G (way under 1G) That makes it noticeably faster. Don't allocate more than a test shows you helps. > autovacuum takes a lot of time running in the largest tables (3 large tables > in 50) causing some connections to have to wait for it to finish to start > transactioning again. Vacuum does not block transactions. unless you're dropping tables or something. > I see a few processes (connections) using 10 ~ 20% of total system memory > and the others using no more then 1%. This is commonly misread. It has to do with the vagaries of shared memory allocation and accounting. The numbers quite likely don't mean what you think they mean. Post the first 20 or so lines from top to show us. > What I want to ask is: is it better to keep the work_mem as high as it is > today or is it a safe bet triyng to reduce this number, for example, to 1 or > 2MB so I can keep the distribution of memory more balanced among all > connections? You're work_mem is dangerously high. Your current reading of top may not actually support lowering it directly. Since you've got 4G shared_buffers allocated, any process that's touched all or most of shared_buffer memory will show as using 4G of ram. That's why you should post output of top, or google on linux virtual memory and top and what the numbers mean. Let's say that 1% of your queries can benefit from > 100Meg work_mem, and 5% with 60M, and 10% with 40M, and 20% with 20M, and 30% with 16M, and 50% 8M and 4M is enough for all the else to do well. If, somehow, 100 queries fired off that could use > 100Meg, they might, with your current settings use all your memory and start using swap til swap ran out and they started getting out of memory errors and failing. This would affect all the other queries on the machine as well. OTOH, if you had work_mem limited to 16M, and 100 of those same queries fired off, they'd individually run a little slower, but they wouldn't be able to run the machine out of memory. If your work_mem and max_connections multiplied is > than some fraction of memory you're doing it wrong, and setting your machine up for mysterious, heavy load failures, the worst kind.