Files
supabase-postgres-best-prac…/skills/postgres-best-practices/rules/lock-advisory.md
Pedro Rodrigues a1b0257ec2 feat: Add initial PostgreSQL best practices rules (#1)
* Add 30 PostgreSQL best practices rules

Rules organized in 8 categories:
- Query Performance (5): indexes, partial indexes, composite, covering, types
- Connection Management (4): pooling, limits, idle timeout, prepared statements
- Schema Design (4): data types, primary keys, foreign key indexes, partitioning
- Concurrency & Locking (4): short transactions, SKIP LOCKED, advisory, deadlocks
- Security (3): RLS basics, RLS performance, privileges
- Data Access Patterns (4): N+1 queries, pagination, upsert, batch inserts
- Monitoring (3): EXPLAIN ANALYZE, pg_stat_statements, VACUUM/ANALYZE
- Advanced Features (3): JSONB indexing, full-text search, CTE materialization

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Update skills/postgresql-best-practices/rules/schema-primary-keys.md

Co-authored-by: samrose <samuel.rose@gmail.com>

* Update skills/postgresql-best-practices/rules/lock-deadlock-prevention.md

Co-authored-by: samrose <samuel.rose@gmail.com>

* resolve merge conflicts from postgres team suggestions

* Delete GETTING_STARTED.md

* Restore all 30 rule files that were lost during rebase

* update agents.md

* remove postgres 11 mention to advanced cte optimization

* update agents.md

* replace advanced cte with check contraints

* replace check contraints with schema lowercase identifiers

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: samrose <samuel.rose@gmail.com>
2026-01-21 15:09:06 +00: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