Re: Reducing relation locking overhead

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Kevin Brown <kevin(at)sysexperts(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Reducing relation locking overhead
Date: 2005-12-05 15:08:55
Message-ID: 20051205150855.GA14256@surnet.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I wonder if it would work to release the AccessShareLock before
acquiring the ExclusiveLock. Of course, this would let any ALTER TABLE
or DROP TABLE to do anything they wanted, but we could check that the
table is still the same after reacquiring the exclusive lock. REINDEX
would have to abort if anything unexpected happened to the table while
the REINDEX transaction was waiting after releasing the shared lock.

What's not at all clear to me is at what time is the lock "upgraded"?
Just after scanning the heap for the first time? In this case, how do
we detect all the tuples that need to be inserted after we acquire the
exclusive lock? Are they listed somewhere?

I imagine each table could have a global flag telling "there is an
online reindex running for this relation". If this flag is set, each
insert/delete to the index needs to save aside somewhere, the CTIDs of
tuples it is inserting/deleting. So the protocol for reindex could be:

acquire vacuum lock
acquire read lock
set REINDEX flag
build the bulk of the index
-- this takes a lot of time ...
-- meanwhile other transactions save CTIDs in a "spill area"
release read lock

acquire exclusive lock
recheck the table, abort if something weird happened
read the spill area, insert/delete from the index as appropiate
mark the index as complete
release exclusive lock
release vacuum lock

The "vacuum lock" is designed to leave any VACUUM out of the equation,
but let run any select/insert/update/delete run. Maybe this lock could
leave ALTER TABLE and other stuff out too. Not sure if we have
something like this in our lock table -- if not, can we create it?

Note that by this proposal, any DDL gets more expensive -- but if it's
the normal case (no REINDEX running), it's only a flag check.

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2005-12-05 15:44:45 Re: [PATCHES] snprintf() argument reordering not working
Previous Message Tom Lane 2005-12-05 14:47:43 Re: Reducing relation locking overhead