Re: TRUNCATE TABLE

From: Adriaan van Os <postgres(at)microbizz(dot)nl>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE
Date: 2007-07-12 07:37:40
Message-ID: 4695DA44.2060000@microbizz.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Gregory Stark wrote:
> That's strange. Deleting should be the *quickest* operation in Postgres. Do
> you perchance have foreign key references referencing this table?

No.

> Do you have any triggers?

No.

Tom Lane wrote:
> Adriaan van Os <postgres(at)microbizz(dot)nl> writes:
>> Surprisingly, one of the bottlenecks is TRUNCATE TABLE and that
>> command is really slow as compared to other operations.
>
> 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.
> Are you sure you are not measuring a delay to obtain exclusive lock
> on the table before it can be truncated (ie, waiting for other
> transactions to finish with it)?

During the tests, there is only one connection to the database server. No other transactions are
running.

> 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.

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

The temporary table has one INT4 field and no indices.

Numrows TRUNCATE (ms) COPY (ms) SELECT (ms)
5122 80,6 16,1 51,2
3910 79,5 12,9 39,9
2745 90,4 10,7 32,4
1568 99,5 7,6 24,7
398 161,1 4,0 22,1
200 79,5 3,3 22,0
200 87,9 3,1 22,0
222368 4943,5 728,6 7659,5
222368 1685,7 512,2 2883,1

Note how fast the COPY is (which is nice). The SELECT statement uses the temporary table.

Regards,

Adriaan van Os

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Marcin Stępnicki 2007-07-12 08:33:45 one column from huge view
Previous Message Craig James 2007-07-12 07:34:45 pg_restore causes 100