2

As stated in http://www.postgresql.org/docs/8.2/interactive/explicit-locking.html there will be no gaurantee Limit can be applied before the locking row. Although wrapper over pg_try_advisory_lock should work but not working.

The fallowing code is locking only one row.If same code is executed from other instance instead of locking unlocked row it is trying to lock the first id which was already locked and returning false.

ref: http://www.flyingtealeaf.com/2010/02/17/postgresql-concurrency
lock the rows until next select postgres and this

select pg_try_advisory_lock(id), * 
from 
( 
  select  id,val
from test
left JOIN pg_locks pgl
       ON pgl.classid = tableoid('test')
      AND pgl.objid = test.id
      AND pgl.pid <> pg_backend_pid()
      
    WHERE pgl.objid IS NULL
order by id limit 1
) t
1
  • @ Peter If the table test contains for example 5 rows with id as primary key when above code is executed from different 5 system concurrently each system should get only one row which is not locked like sys1->id1 row, sys2->id2 row...sys5->id5..rows.
    – kiranking
    Commented Jan 3, 2011 at 9:35

2 Answers 2

4

As far as I understood, you are trying to obtain an advisory lock for the first row that has not been marked with an advisory lock.

Edit: after trying this the pg_try_advisory_lock() in the inner select is apparently not working (should have thought about that earlier). But simply excluding all rows from the table by checking objid should work:

select pg_try_advisory_lock(id), * 
from 
( 
  select  id,val
  from test
  where id not in (select objid from pg_locks where locktype = 'advisory')
  order by id limit 1
) t

Edit

Although advisory locks will show up in pg_locks, they will never have any information about a table or row that has been locked. After all pg_advisory_lock() is simply passed a number which has no connection to the underlying table or row.

SELECT pg_advisory_lock(42)

will create exactly the same advisory lock as

SELECT pg_advisory_lock(id) 
FROM foo 
WHERE id = 42

So your join using classid will never succeed.

0

Sorry about my mistake. I should have gone through the first ref link line by line. The trick the author was doing is just set the classid while calling lock function.The fallowing code works for me finally with LIMIT

select pg_try_advisory_lock(tableoid('test'),id) , * from
(
select  id,val
from test
LEFT JOIN pg_locks pgl
       ON pgl.classid = tableoid('test')
      AND pgl.objid = test.id
      AND pgl.pid <> pg_backend_pid()

    WHERE pgl.objid IS NULL
order by id    limit 1)f

Tableoid Function(copied from ref link#1)

CREATE OR REPLACE FUNCTION tableoid(tablename TEXT) RETURNS INTEGER AS $$
BEGIN
  RETURN (SELECT oid FROM pg_class WHERE relname = tablename);
END
$$
LANGUAGE plpgsql IMMUTABLE SECURITY DEFINER;

Than you
a_horse_with_no_name for your time and effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.