Re: Very long deletion time on a 200 GB database

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Greg Spiegelberg <gspiegelberg(at)gmail(dot)com>
Cc: Andy Colson <andy(at)squeakycode(dot)net>, sthomas(at)peak6(dot)com, "Reuven M(dot) Lerner" <reuven(at)lerner(dot)co(dot)il>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Marcin Mańk <marcin(dot)mank(at)gmail(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Very long deletion time on a 200 GB database
Date: 2012-02-23 18:30:32
Message-ID: 3023.1330021832@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Greg Spiegelberg <gspiegelberg(at)gmail(dot)com> writes:
> I used LOCK simply because if a VACUUM FULL x; slipped in between the
> SELECT and the DELETE the ctid's could conceivably change.

VACUUM FULL can't "slip in" there, because you'd have AccessShareLock
just from the SELECT. The real problem goes like this:

1. You SELECT some ctid and save it in the other table.
2. Somebody else updates or deletes that row.
3. Plain VACUUM comes along and frees the dead TID.
4. Somebody else (maybe not same somebody as #2) inserts a new
row at that TID position.
5. You DELETE that TID. Ooops.

So you might say "okay, the point of the lock is to block plain vacuum,
not vacuum full". I'm still a bit worried about whether the technique
is entirely safe, though, because of page pruning which can happen
anyway. What this really boils down to is: how sure are you that no
other userland activity is going to update or delete any of the targeted
rows between the SELECT INTO and the DELETE? If you're sure, then this
is safe without the extra lock. Otherwise, I wouldn't trust it.

It might be worth having the SELECT that creates the temp table be a
SELECT FOR UPDATE on the target table, so as to ensure you've locked
down the targeted rows against anybody else. This is not free though,
as it'll mean extra writes of all the modified tuples.

regards, tom lane

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Alessandro Gagliardi 2012-02-23 18:38:29 Re: set autovacuum=off
Previous Message Greg Spiegelberg 2012-02-23 18:13:39 Re: Very long deletion time on a 200 GB database