Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id C8B169FB96F for ; Thu, 12 Jul 2007 16:20:18 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 46487-06 for ; Thu, 12 Jul 2007 16:20:06 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 677979FB810 for ; Thu, 12 Jul 2007 16:20:12 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id l6CJK2Vv009072; Thu, 12 Jul 2007 15:20:03 -0400 (EDT) To: Adriaan van Os cc: pgsql-performance@postgresql.org Subject: Re: TRUNCATE TABLE In-reply-to: <4695DA44.2060000@microbizz.nl> References: <46953949.3030201@microbizz.nl> <3561.1184190885@sss.pgh.pa.us> <4695DA44.2060000@microbizz.nl> Comments: In-reply-to Adriaan van Os message dated "Thu, 12 Jul 2007 09:37:40 +0200" Date: Thu, 12 Jul 2007 15:20:02 -0400 Message-ID: <9071.1184268002@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/145 X-Sequence-Number: 25829 Adriaan van Os writes: > Tom Lane wrote: >> When you don't quantify that statement at all, it's hard to make an >> intelligent comment on it, but TRUNCATE per se shouldn't be slow. > Below are some timings, in milliseconds. I can only conclude that you're using a seriously bad filesystem :-( I tried to replicate your results on a fairly old and slow HPUX box. I get a fairly repeatable time of around 40msec to truncate a table; this is presumably mostly filesystem time to create one file and delete another. I used CVS HEAD for this because the devel version of psql supports reporting \timing for \copy commands, but I'm quite sure that TRUNCATE isn't any faster than it was in 8.2: regression=# create table tab(f1 int); CREATE TABLE Time: 63.775 ms regression=# insert into tab select random()*10000 from generate_series(1,5000); INSERT 0 5000 Time: 456.011 ms regression=# \copy tab to 'tab.data' binary Time: 80.343 ms regression=# truncate table tab; TRUNCATE TABLE Time: 35.825 ms regression=# \copy tab from 'tab.data' binary Time: 391.928 ms regression=# select count(*) from tab; count ------- 5000 (1 row) Time: 21.457 ms regression=# truncate table tab; TRUNCATE TABLE Time: 47.867 ms regression=# \copy tab from 'tab.data' binary Time: 405.074 ms regression=# select count(*) from tab; count ------- 5000 (1 row) Time: 20.247 ms If I increase the test size to 200K rows, I get a proportional increase in the copy and select times, but truncate stays about the same: regression=# truncate table tab; TRUNCATE TABLE Time: 40.196 ms regression=# \copy tab from 'tab.data' binary Time: 15779.689 ms regression=# select count(*) from tab; count -------- 200000 (1 row) Time: 642.965 ms Your numbers are not making any sense to me. In particular there is no reason in the Postgres code for it to take longer to truncate a 200K-row table than a 5K-row table. (I would expect some increment at the point of having 1GB in the table, where we'd create a second table segment file, but you are nowhere near that.) The bottom line seems to be that you have a filesystem that takes a long time to delete a file, with the cost rising rapidly as the file gets bigger. Can you switch to a different filesystem? regards, tom lane