pglock
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pglock | 1.0.0 | UTIL | PostgreSQL | SQL |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4140 | pglock | No | No | No | Yes | No | No | pglock |
| Related | pg_cron pgmb pgmq pgq pg_cron |
|---|
Packaging patches the upstream pgmb.control mismatch and installs the extension as pglock.
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.0.0 | 1817161514 | pglock | pg_cron |
| RPM | PIGSTY | 1.0.0 | 1817161514 | pglock_$v | pg_cron_$v |
| DEB | PIGSTY | 1.0.0 | 1817161514 | postgresql-$v-pglock | postgresql-$v-cron |
Build
You can build the RPM / DEB packages for pglock using pig build:
pig build pkg pglock # build RPM / DEB packages
Install
You can install pglock directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
pig repo add pgsql -u # Add repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install pglock; # Install for current active PG version
pig ext install -y pglock -v 18 # PG 18
pig ext install -y pglock -v 17 # PG 17
pig ext install -y pglock -v 16 # PG 16
pig ext install -y pglock -v 15 # PG 15
pig ext install -y pglock -v 14 # PG 14
dnf install -y pglock_18 # PG 18
dnf install -y pglock_17 # PG 17
dnf install -y pglock_16 # PG 16
dnf install -y pglock_15 # PG 15
dnf install -y pglock_14 # PG 14
apt install -y postgresql-18-pglock # PG 18
apt install -y postgresql-17-pglock # PG 17
apt install -y postgresql-16-pglock # PG 16
apt install -y postgresql-15-pglock # PG 15
apt install -y postgresql-14-pglock # PG 14
Create Extension:
CREATE EXTENSION pglock CASCADE; -- requires: pg_cron
Usage
- Source: README
pglock is a lightweight distributed lock service implemented inside PostgreSQL. It stores lock state in pglock.locks and uses TTL-based cleanup for stale rows.
Create The Extension
CREATE EXTENSION pglock;
The upstream README lists PostgreSQL 9.1+ and plpgsql as requirements.
Acquire And Release Locks
SELECT pglock.lock('worker-1', 'users');
SELECT pglock.unlock('worker-1', 'users');
pglock.lock(id, resource) returns true if the lock is acquired and false if another process already holds it. pglock.unlock(id, resource) is documented as idempotent.
Isolation And Expiration
The README recommends serializable isolation for correctness under concurrency:
SELECT pglock.set_serializable();
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT pglock.lock('my-id', 'my-resource');
SELECT pglock.unlock('my-id', 'my-resource');
COMMIT;
Stale locks are expired with:
SELECT pglock.ttl();
The documented default TTL is 5 minutes. If pg_cron is available, the README says pglock.ttl() can be scheduled every minute.
Lock Table
The upstream schema is pglock.locks with columns id, resource, locked, ttl, created_at, and updated_at. The primary key is (id, resource).
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.