pglock
pglock : Lightweight distributed lock service inside PostgreSQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4140 | pglock | pglock | 1.0.0 | UTIL | PostgreSQL | SQL |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
----d-- | No | No | No | Yes | no | no |
| Relationships | |
|---|---|
| Schemas | pglock |
| Requires | pg_cron |
| See Also | pgmb pgmq pgq pg_cron |
Packaging patches the upstream pgmb.control mismatch and installs the extension as pglock.
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.0.0 | 18 17 16 15 14 | pglock | pg_cron |
| RPM | PIGSTY | 1.0.0 | 18 17 16 15 14 | pglock_$v | pg_cron_$v |
| DEB | PIGSTY | 1.0.0 | 18 17 16 15 14 | postgresql-$v-pglock | postgresql-$v-cron |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el8.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el9.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el9.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el10.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el10.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
d12.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
d12.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
d13.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
d13.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
u22.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
u22.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
u24.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
u24.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
Source
pig build pkg pglock; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pglock; # install via package name, for the active PG version
pig install pglock -v 18; # install for PG 18
pig install pglock -v 17; # install for PG 17
pig install pglock -v 16; # install for PG 16
pig install pglock -v 15; # install for PG 15
pig install pglock -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION pglock CASCADE; -- requires pg_cronUsage
Syntax:
SELECT pglock.lock('b3d8a762-3a0e-495b-b6a1-dc8609839f7b', 'users'); SELECT pglock.unlock('b3d8a762-3a0e-495b-b6a1-dc8609839f7b', 'users'); SELECT pglock.ttl();Source: README
pglock is a lightweight distributed lock service implemented inside PostgreSQL. It stores locks in a table and supports TTL-based expiration for stale locks.
Basic Functions
The README documents four core functions:
pglock.lock(id, resource)to acquire a lockpglock.unlock(id, resource)to release a lockpglock.ttl()to expire stale lockspglock.set_serializable()to switch to serializable isolation
Acquire a lock:
SELECT pglock.lock('worker-1', 'users');Release it:
SELECT pglock.unlock('worker-1', 'users');Isolation
The upstream docs recommend serializable isolation for correctness under concurrency:
SELECT pglock.set_serializable();or:
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT pglock.lock('my-id', 'my-resource');
SELECT pglock.unlock('my-id', 'my-resource');
COMMIT;TTL Expiration
Locks have a configurable TTL with a documented default of 5 minutes. pglock.ttl() unlocks records whose updated_at is older than the TTL:
SELECT pglock.ttl();If pg_cron is installed, the README says a cron job can run pglock.ttl() every minute.
Schema
The lock table is pglock.locks with columns:
idresourcelockedttlcreated_atupdated_at
The primary key is (id, resource).