Re: Proper Method for using LockAcquire - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Proper Method for using LockAcquire
Date
Msg-id 18117.1152816587@sss.pgh.pa.us
Whole thread Raw
In response to Proper Method for using LockAcquire  (Chris Bowlby <excalibur@accesswave.ca>)
List pgsql-hackers
Chris Bowlby <excalibur@accesswave.ca> writes:
>  I've been working on a small module that I will be pluging into my 
> local PostreSQL 8.x database and am in need of doing some table locking. 
> At this time, I've used various other examples to no avail and was 
> wondering what the proper method for aquiring a table lock within the 
> module would be?

You should not be touching locks at any level lower than lmgr.c's
exports; eg LockRelation() not LockAcquire().  The LockAcquire API
is not as stable.

Usually people take a relation lock in combination with opening the rel
in the first place, ie, specify the desired lock to heap_open or
relation_open or one of their variants.  If you apply LockRelation() to
an already-opened rel then you need to be worrying about possible
deadlocks due to lock upgrading.

Also, 90% of the time you probably don't want to release the lock
explicitly at all; leave it to be held until transaction end.
Early release violates the 2PL principle, so you need to analyze
things pretty carefully to determine if it's safe.
        regards, tom lane


pgsql-hackers by date:

Previous
From: "Marc G. Fournier"
Date:
Subject: Re: Three weeks left until feature freeze
Next
From: Martijn van Oosterhout
Date:
Subject: Re: Proper Method for using LockAcquire