Skip to content

pglock

Lightweight distributed lock service inside PostgreSQL

pglock · Lightweight distributed lock service inside PostgreSQL

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4140pglockpglock1.0.0UTILPostgreSQLSQL
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
----d--NoNoNoYesnono
Packaging patches the upstream pgmb.control mismatch and installs the extension as pglock.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY1.0.019 18 17 16 15pglockpg_cron
RPMPIGSTY1.0.019 18 17 16 15pglock_$vpg_cron_$v
DEBPIGSTY1.0.019 18 17 16 15postgresql-$v-pglockpostgresql-$v-cron
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el8.aarch64N/APIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el9.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el9.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el10.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
el10.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
d12.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
d12.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
d13.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
d13.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u22.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u22.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u24.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u24.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u26.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
u26.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0
PackageVersionOSORGSIZEFile URL
pglock_191.0.0el9.x86_64pigsty10.2 KiBpglock_19-1.0.0-1PGSTY.el9.noarch.rpm
pglock_191.0.0el9.aarch64pigsty10.2 KiBpglock_19-1.0.0-1PGSTY.el9.noarch.rpm
pglock_191.0.0el10.x86_64pigsty10.4 KiBpglock_19-1.0.0-1PGSTY.el10.noarch.rpm
pglock_191.0.0el10.aarch64pigsty10.4 KiBpglock_19-1.0.0-1PGSTY.el10.noarch.rpm
postgresql-19-pglock1.0.0d12.x86_64pigsty4.7 KiBpostgresql-19-pglock_1.0.0-1PGSTY~bookworm_all.deb
postgresql-19-pglock1.0.0d12.aarch64pigsty4.7 KiBpostgresql-19-pglock_1.0.0-1PGSTY~bookworm_all.deb
postgresql-19-pglock1.0.0d13.x86_64pigsty4.7 KiBpostgresql-19-pglock_1.0.0-1PGSTY~trixie_all.deb
postgresql-19-pglock1.0.0d13.aarch64pigsty4.7 KiBpostgresql-19-pglock_1.0.0-1PGSTY~trixie_all.deb
postgresql-19-pglock1.0.0u22.x86_64pigsty4.0 KiBpostgresql-19-pglock_1.0.0-1PGSTY~jammy_all.deb
postgresql-19-pglock1.0.0u22.aarch64pigsty4.0 KiBpostgresql-19-pglock_1.0.0-1PGSTY~jammy_all.deb
postgresql-19-pglock1.0.0u24.x86_64pigsty4.0 KiBpostgresql-19-pglock_1.0.0-1PGSTY~noble_all.deb
postgresql-19-pglock1.0.0u24.aarch64pigsty4.0 KiBpostgresql-19-pglock_1.0.0-1PGSTY~noble_all.deb
postgresql-19-pglock1.0.0u26.x86_64pigsty4.0 KiBpostgresql-19-pglock_1.0.0-1PGSTY~resolute_all.deb
postgresql-19-pglock1.0.0u26.aarch64pigsty4.0 KiBpostgresql-19-pglock_1.0.0-1PGSTY~resolute_all.deb

Source

pig build pkg pglock;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pglock;		# install by package name

pig install pglock -v 19;   # install for PG 19
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

Create this extension with:

CREATE EXTENSION pglock CASCADE; -- requires pg_cron

Usage

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).

Table reader