Files
antigravity-skills-reference/skills/postgres-best-practices/rules/lock-advisory.md
Ahmed Rehan 29b45dd234 feat(skills): add supabase postgres best practices skill and update the Official Sources to include supabase/agent-skills repo
- Adds `skills/postgres-best-practices/`: A new skill containing comprehensive Postgres performance optimization rules and guidelines from Supabase.
- The skill includes rules for query performance, connection management, security (RLS), and schema design.
- Updates `README.md` to include the new skill in the directory.
2026-01-23 14:02:37 +05:00

1.5 KiB

title, impact, impactDescription, tags
title impact impactDescription tags
Use Advisory Locks for Application-Level Locking MEDIUM Efficient coordination without row-level lock overhead advisory-locks, coordination, application-locks

Use Advisory Locks for Application-Level Locking

Advisory locks provide application-level coordination without requiring database rows to lock.

Incorrect (creating rows just for locking):

-- Creating dummy rows to lock on
create table resource_locks (
  resource_name text primary key
);

insert into resource_locks values ('report_generator');

-- Lock by selecting the row
select * from resource_locks where resource_name = 'report_generator' for update;

Correct (advisory locks):

-- Session-level advisory lock (released on disconnect or unlock)
select pg_advisory_lock(hashtext('report_generator'));
-- ... do exclusive work ...
select pg_advisory_unlock(hashtext('report_generator'));

-- Transaction-level lock (released on commit/rollback)
begin;
select pg_advisory_xact_lock(hashtext('daily_report'));
-- ... do work ...
commit;  -- Lock automatically released

Try-lock for non-blocking operations:

-- Returns immediately with true/false instead of waiting
select pg_try_advisory_lock(hashtext('resource_name'));

-- Use in application
if (acquired) {
  -- Do work
  select pg_advisory_unlock(hashtext('resource_name'));
} else {
  -- Skip or retry later
}

Reference: Advisory Locks