pglock

Lightweight distributed lock service inside PostgreSQL

Overview

PackageVersionCategoryLicenseLanguage
pglock1.0.0UTILPostgreSQLSQL
IDExtensionBinLibLoadCreateTrustRelocSchema
4140pglockNoNoNoYesNoNopglock
Relatedpg_cron pgmb pgmq pgq pg_cron

Packaging patches the upstream pgmb.control mismatch and installs the extension as pglock.

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.0.01817161514pglockpg_cron
RPMPIGSTY1.0.01817161514pglock_$vpg_cron_$v
DEBPIGSTY1.0.01817161514postgresql-$v-pglockpostgresql-$v-cron
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
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

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

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 lock
  • pglock.unlock(id, resource) to release a lock
  • pglock.ttl() to expire stale locks
  • pglock.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:

  • id
  • resource
  • locked
  • ttl
  • created_at
  • updated_at

The primary key is (id, resource).


Last Modified 2026-04-14: update extension catalog (29617e5)