Proper Method for using LockAcquire

Started by Chris Bowlbyover 19 years ago3 messages
#1Chris Bowlby
excalibur@accesswave.ca

Hi All,

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?

For example I am using an SPI based module:

static void mytest(void) {
LOCKMETHODID localLockTableId = INVALID_LOCKMETHOD;
LOCKTAG localtag;

memset(&localtag, 0, sizeof(localtag));
localtag.relId = XactLockTableId;
localtag.dbId = 1;
localtag.objId.xid = InvalidTransactionId;

if (!LockAcquire(LocalLockTableId, &localtag,
GetCurrentTransactionId(), Sharelock, false)) {
elog(ERROR, "mytest: did not acquire table lock");
}

....

if(!LockRelease(LocalLockTableId, &localtag,
GetCurrentTransactionId(), Sharelock)) {
elog(ERROR, "mytest: could not release lock");
}
}

I know there is something I am missing and would appreciate any help. I
believe I need to initialize the LocalLockTableId, but I have not been
able to find any examples of that being done. Could someone look this
over and point me in the right direction?

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Chris Bowlby (#1)
Re: Proper Method for using LockAcquire

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

#3Martijn van Oosterhout
kleptog@svana.org
In reply to: Chris Bowlby (#1)
Re: Proper Method for using LockAcquire

On Thu, Jul 13, 2006 at 03:01:31PM -0300, Chris Bowlby wrote:

Hi All,

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?

Firstly, why? Most operations in PostgreSQL acquire the appropriate
locks for you, so you don't need to do it yourself. Explicit locking
opens you up to deadlocks.

Secondly, what's wrong with LockRelation(rel, lockmode)? I grabbed that
from relation_open in access/heap/heapam.c.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

From each according to his ability. To each according to his ability to litigate.