Unix Technical Forum

Proper Method for using LockAcquire

This is a discussion on Proper Method for using LockAcquire within the pgsql Hackers forums, part of the PostgreSQL category; --> Hi All, I've been working on a small module that I will be pluging into my local PostreSQL 8.x ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Hackers

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-12-2008, 03:25 AM
Chris Bowlby
 
Posts: n/a
Default Proper Method for using LockAcquire

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?



---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-12-2008, 03:25 AM
Tom Lane
 
Posts: n/a
Default 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

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-12-2008, 03:25 AM
Martijn van Oosterhout
 
Posts: n/a
Default 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/
> From each according to his ability. To each according to his ability to litigate.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFEtpaRIB7bNG8LQkwRAlE9AJ9dKYERoi9gUP+but1Gl3 ihD1c5agCfaSme
CxPfn+qd5g1M+/pVzaB3q2M=
=seR8
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:10 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com