Re: locks in CREATE TRIGGER, ADD FK - Mailing list pgsql-hackers

From Neil Conway
Subject Re: locks in CREATE TRIGGER, ADD FK
Date
Msg-id 4240DAB9.1020601@samurai.com
Whole thread Raw
In response to Re: locks in CREATE TRIGGER, ADD FK  (Christopher Kings-Lynne <chriskl@familyhealth.com.au>)
Responses Re: locks in CREATE TRIGGER, ADD FK
Re: locks in CREATE TRIGGER, ADD FK
List pgsql-hackers
Christopher Kings-Lynne wrote:
> If you want to be my friend forever, then fix CLUSTER so that it uses 
> sharerowexclusive as well :D

Hmm, this might be possible as well. During a CLUSTER, we currently

- lock the heap relation with AccessExclusiveLock
- lock the index we're clustering on with AccessExclusiveLock
- create a temporary heap relation
- fill with data from the old heap relation, via an index scan
- swap the relfilenodes of the old and temporary heap relations
- rebuild indexes

We certainly can't allow concurrent modifications to either the table or 
the clustered index while this is happening. Allowing index scans 
*should* be safe -- an index scan could result in modifications to the 
index (e.g. updating "tuple is killed" bits), but those shouldn't be 
essential. We might also want to disallow SELECT FOR UPDATE, since we 
would end up invoking heap_mark4update() on the old heap relation. Not 
sure offhand how serious that would be.

So I think it should be possible to lock both the heap relation and the 
index with ExclusiveLock, which would allow SELECTs on them. This would 
apply to both the single relation and multiple relation variants of 
CLUSTER (since we do each individual clustering in its own transaction).

... except that when we rebuild the relation's indexes, we acquire an 
AccessExclusiveLock on the index. This would introduce the risk of 
deadlock. It seems necessary to acquire an AccessExclusiveLock when 
rebuilding shared indexes, since we do the index build in-place, but I 
think we can get by with an ExclusiveLock in the non-shared case, for 
similar reasons as above: we build the new index and then swap relfilenodes.

-Neil


pgsql-hackers by date:

Previous
From: Christopher Kings-Lynne
Date:
Subject: Re: locks in CREATE TRIGGER, ADD FK
Next
From: Bruce Momjian
Date:
Subject: Re: locks in CREATE TRIGGER, ADD FK