Is the "ACCESS EXCLUSIVE" lock for TRUNCATE really necessary? - Mailing list pgsql-general

From Florian G. Pflug
Subject Is the "ACCESS EXCLUSIVE" lock for TRUNCATE really necessary?
Date
Msg-id 440CD9A9.50802@phlo.org
Whole thread Raw
Responses Re: Is the "ACCESS EXCLUSIVE" lock for TRUNCATE really necessary?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hi

I know that TRUNCATE needs to acquire an ACCESS EXCLUSIVE lock,
because it will remove the datafile on commit, and needs to ensure
that noone will be using it anymore by then. For a lot of applications
(at least mine) this is imposes problems. I'd like to use TRUNCATE
in a few places, not only to "clean out" the table, but also to prevent
others from inserting while I'm deleting everything (Think a cache table -
if I clear the cache, I don't want concurrent transactions to be able
to insert - but readers are not a problem, they still see the database
in their "old state", so seeing the cache in the "old state" is correct).

Now, I was thinking if TRUNCATE couldn't just let relfilenode in
pg_class point to a new datafile, and leave the old one in place.
TRUNCATE would still need a lock that prevents any write-access to
the table, but it wouldn't need to lock-out readers too.
VACUUM could then remove datafiles when it purges a record from pg_class.

I'm asking mainly out of curiosity - I though about this for a while now,
and couldn't come up with a reason why this wouldn't be possible.

greetings, Florian Pflug

pgsql-general by date:

Previous
From: "Harry Hehl"
Date:
Subject: Sequencial scan instead of using index issue again
Next
From: "Florian G. Pflug"
Date:
Subject: Re: Is the "ACCESS EXCLUSIVE" lock for TRUNCATE really