Skip to content

Instantly share code, notes, and snippets.

@shyiko
Created December 27, 2011 13:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyiko/1523620 to your computer and use it in GitHub Desktop.
Save shyiko/1523620 to your computer and use it in GitHub Desktop.
PostgreSQL Advisory Locking
<tableid> = select oid from pg_class where relname = '<tablename>';
<rowid> = select id from (
select rr.id from <tablename> rr left join pg_locks pgl
on pgl.classid = <tableid> and pgl.objid = rr.id
where pgl.objid is null limit 1
) iq where pg_try_advisory_lock(<tableid>, id);
-- select * from pg_locks where locktype = 'advisory';
select pg_advisory_unlock(<tableid>, <rowid>);
@shyiko
Copy link
Author

shyiko commented Dec 27, 2011

@shyiko
Copy link
Author

shyiko commented Dec 28, 2011

select pg_try_advisory_lock(<tableid>, id) from <tablename> where id = <rowid>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment