Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id DFAD09FBB14 for ; Tue, 8 May 2007 07:08:07 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 59484-02 for ; Tue, 8 May 2007 07:08:04 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.175]) by postgresql.org (Postfix) with ESMTP id 4095A9FB889 for ; Tue, 8 May 2007 07:08:04 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id k3so56603ugf for ; Tue, 08 May 2007 03:08:02 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=LgBYGkRpzmSQtBkKtVCLJTAsSH0xAGoqpULvbWfqGsIeyNpyLtyflwqEpf+yo5/mfQDIIyrfBFKw0TA5evP/TE5tspoFcKcs0DOItARtuXHjz8McM79Pv5tU9FRWAqO8Qh1ITsj1ZumGXjCtA4XlPYRmQqJ1d+XKK2JRoduzqD4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=gRwL53CreePcZrIEYhMjjtvdjufMo5uIwFP5LiZbUbYIweI650Lk49W+xokAlAENss+qCcCtA0jnp/Ix7kuctis70TlwcOX+wqh99CGFbp42G7aSp/iAXr9p92N6e4tKrheoVZuHJXzrnSog7aW6QzJCxZea1Cjgf2i6UF71Ips= Received: by 10.67.115.14 with SMTP id s14mr377002ugm.1178618882673; Tue, 08 May 2007 03:08:02 -0700 (PDT) Received: from ?172.24.32.83? ( [62.232.55.118]) by mx.google.com with ESMTP id 54sm523741ugp.2007.05.08.03.07.59; Tue, 08 May 2007 03:07:59 -0700 (PDT) Message-ID: <46404BEA.5060009@enterprisedb.com> Date: Tue, 08 May 2007 11:07:38 +0100 From: Heikki Linnakangas User-Agent: Icedove 1.5.0.10 (X11/20070329) MIME-Version: 1.0 To: Pomarede Nicolas CC: pgsql-performance@postgresql.org Subject: Re: truncate a table instead of vaccum full when count(*) is 0 References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/96 X-Sequence-Number: 24486 Pomarede Nicolas wrote: > But for the data (dead rows), even running a vacuum analyze every day is > not enough, and doesn't truncate some empty pages at the end, so the > data size remains in the order of 200-300 MB, when only a few effective > rows are there. For a table like that you should run VACUUM much more often than once a day. Turn on autovacuum, or set up a cron script etc. to run it every 15 minutes or so. > Running a vacuum full is a solution for now, but it locks the table for > too long (10 minutes or so), which is not acceptable in that case, since > events should be processed in less that 10 seconds. > > So, I would like to truncate the table when the number of rows reaches 0 > (just after the table was processed, and just before some new rows are > added). > > Is there an easy way to do this under psql ? For example, lock the > table, do a count(*), if result is 0 row then truncate the table, unlock > the table (a kind of atomic 'truncate table if count(*) == 0'). > > Would this work and what would be the steps ? It should work, just like you describe it, with the caveat that TRUNCATE will remove any old row versions that might still be visible to an older transaction running in serializable mode. It sounds like it's not a problem in your scenario, but it's hard to say for sure without seeing the application. Running vacuum more often is probably a simpler and better solution, anyway. Which version of PostgreSQL is this? -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com