TRUNCATE TABLE

From: Adriaan van Os <postgres(at)microbizz(dot)nl>
To: pgsql-performance(at)postgresql(dot)org
Subject: TRUNCATE TABLE
Date: 2007-07-11 20:10:49
Message-ID: 46953949.3030201@microbizz.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Recently, I have been doing extensive profiling of a version 8.1.4 Postgres DB with about 175
tables and 5 GB of data (the server running on Fedora Linux and the clients on Windows XP).
Surprisingly, one of the bottlenecks is TRUNCATE TABLE and that command is really slow as compared
to other operations. For example, we have operations like:

TRUNCATE TABLE my_temporary_table
COPY my_temporary_table ... FROM STDIN BINARY
do_something

where do_something is using the data in my_temporary_table to do something like a JOIN or a mass
UPDATE or whatever.

Now, it turns out that typically most time is lost in TRUNCATE TABLE, in fact it spoils the
performance of most operations on the DB !

I read in a mailing list archive that TRUNCATE TABLE is slow since it was made transaction-safe
somewhere in version 7, but for operations on a temporary table (with data coming from the outside
world) that is irrelevant, at least for my application, in casu, a middleware software package.

So, my questions are

1. Why is TRUNCATE TABLE so slow (even if transaction-safe)
2. Is there is way to dig up in the source code somewhere a quick-and-dirty TRUNCATE TABLE
alternative for operations on temporary tables that need not be transaction-safe (because the
middleware itself can easily restore anything that goes wrong there).

I noticed, by the way, that removing records in general is painfully slow, but I didn't do a
detailed analysis of that issue yet.

As an alternative to TRUNCATE TABLE I tried to CREATE and DROP a table, but that wasn't any faster.

Sincerely,

Adriaan van Os

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Andreas Kretschmer 2007-07-11 20:19:58 Re: bitmap-index-scan slower than normal index scan
Previous Message Tom Lane 2007-07-11 20:04:33 Re: bitmap-index-scan slower than normal index scan