跳转到主要内容

pglock

Lightweight distributed lock service inside PostgreSQL

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
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
u26.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0
u26.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0
Package Version OS ORG SIZE File URL
pglock_18 1.0.0 el8.x86_64 pigsty 10.3 KiB pglock_18-1.0.0-1PIGSTY.el8.x86_64.rpm
pglock_18 1.0.0 el8.aarch64 pigsty 10.3 KiB pglock_18-1.0.0-1PIGSTY.el8.aarch64.rpm
pglock_18 1.0.0 el9.x86_64 pigsty 10.3 KiB pglock_18-1.0.0-1PIGSTY.el9.x86_64.rpm
pglock_18 1.0.0 el9.aarch64 pigsty 10.3 KiB pglock_18-1.0.0-1PIGSTY.el9.aarch64.rpm
pglock_18 1.0.0 el10.x86_64 pigsty 10.5 KiB pglock_18-1.0.0-1PIGSTY.el10.x86_64.rpm
pglock_18 1.0.0 el10.aarch64 pigsty 10.4 KiB pglock_18-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-18-pglock 1.0.0 d12.x86_64 pigsty 4.6 KiB postgresql-18-pglock_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-18-pglock 1.0.0 d12.aarch64 pigsty 4.6 KiB postgresql-18-pglock_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-18-pglock 1.0.0 d13.x86_64 pigsty 4.6 KiB postgresql-18-pglock_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-18-pglock 1.0.0 d13.aarch64 pigsty 4.6 KiB postgresql-18-pglock_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-18-pglock 1.0.0 u22.x86_64 pigsty 4.0 KiB postgresql-18-pglock_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-18-pglock 1.0.0 u22.aarch64 pigsty 4.0 KiB postgresql-18-pglock_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-18-pglock 1.0.0 u24.x86_64 pigsty 4.0 KiB postgresql-18-pglock_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-18-pglock 1.0.0 u24.aarch64 pigsty 4.0 KiB postgresql-18-pglock_1.0.0-1PIGSTY~noble_arm64.deb
postgresql-18-pglock 1.0.0 u26.x86_64 pigsty 4.0 KiB postgresql-18-pglock_1.0.0-1PIGSTY~resolute_amd64.deb
postgresql-18-pglock 1.0.0 u26.aarch64 pigsty 4.0 KiB postgresql-18-pglock_1.0.0-1PIGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
pglock_17 1.0.0 el8.x86_64 pigsty 10.3 KiB pglock_17-1.0.0-1PIGSTY.el8.x86_64.rpm
pglock_17 1.0.0 el8.aarch64 pigsty 10.3 KiB pglock_17-1.0.0-1PIGSTY.el8.aarch64.rpm
pglock_17 1.0.0 el9.x86_64 pigsty 10.3 KiB pglock_17-1.0.0-1PIGSTY.el9.x86_64.rpm
pglock_17 1.0.0 el9.aarch64 pigsty 10.3 KiB pglock_17-1.0.0-1PIGSTY.el9.aarch64.rpm
pglock_17 1.0.0 el10.x86_64 pigsty 10.5 KiB pglock_17-1.0.0-1PIGSTY.el10.x86_64.rpm
pglock_17 1.0.0 el10.aarch64 pigsty 10.4 KiB pglock_17-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-17-pglock 1.0.0 d12.x86_64 pigsty 4.6 KiB postgresql-17-pglock_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-17-pglock 1.0.0 d12.aarch64 pigsty 4.6 KiB postgresql-17-pglock_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-17-pglock 1.0.0 d13.x86_64 pigsty 4.6 KiB postgresql-17-pglock_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-17-pglock 1.0.0 d13.aarch64 pigsty 4.6 KiB postgresql-17-pglock_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-17-pglock 1.0.0 u22.x86_64 pigsty 4.0 KiB postgresql-17-pglock_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-17-pglock 1.0.0 u22.aarch64 pigsty 4.0 KiB postgresql-17-pglock_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-17-pglock 1.0.0 u24.x86_64 pigsty 4.0 KiB postgresql-17-pglock_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-17-pglock 1.0.0 u24.aarch64 pigsty 4.0 KiB postgresql-17-pglock_1.0.0-1PIGSTY~noble_arm64.deb
postgresql-17-pglock 1.0.0 u26.x86_64 pigsty 4.0 KiB postgresql-17-pglock_1.0.0-1PIGSTY~resolute_amd64.deb
postgresql-17-pglock 1.0.0 u26.aarch64 pigsty 4.0 KiB postgresql-17-pglock_1.0.0-1PIGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
pglock_16 1.0.0 el8.x86_64 pigsty 10.3 KiB pglock_16-1.0.0-1PIGSTY.el8.x86_64.rpm
pglock_16 1.0.0 el8.aarch64 pigsty 10.3 KiB pglock_16-1.0.0-1PIGSTY.el8.aarch64.rpm
pglock_16 1.0.0 el9.x86_64 pigsty 10.3 KiB pglock_16-1.0.0-1PIGSTY.el9.x86_64.rpm
pglock_16 1.0.0 el9.aarch64 pigsty 10.3 KiB pglock_16-1.0.0-1PIGSTY.el9.aarch64.rpm
pglock_16 1.0.0 el10.x86_64 pigsty 10.5 KiB pglock_16-1.0.0-1PIGSTY.el10.x86_64.rpm
pglock_16 1.0.0 el10.aarch64 pigsty 10.4 KiB pglock_16-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-16-pglock 1.0.0 d12.x86_64 pigsty 4.6 KiB postgresql-16-pglock_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-16-pglock 1.0.0 d12.aarch64 pigsty 4.6 KiB postgresql-16-pglock_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-16-pglock 1.0.0 d13.x86_64 pigsty 4.6 KiB postgresql-16-pglock_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-16-pglock 1.0.0 d13.aarch64 pigsty 4.6 KiB postgresql-16-pglock_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-16-pglock 1.0.0 u22.x86_64 pigsty 4.0 KiB postgresql-16-pglock_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-16-pglock 1.0.0 u22.aarch64 pigsty 4.0 KiB postgresql-16-pglock_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-16-pglock 1.0.0 u24.x86_64 pigsty 4.0 KiB postgresql-16-pglock_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-16-pglock 1.0.0 u24.aarch64 pigsty 4.0 KiB postgresql-16-pglock_1.0.0-1PIGSTY~noble_arm64.deb
postgresql-16-pglock 1.0.0 u26.x86_64 pigsty 4.0 KiB postgresql-16-pglock_1.0.0-1PIGSTY~resolute_amd64.deb
postgresql-16-pglock 1.0.0 u26.aarch64 pigsty 4.0 KiB postgresql-16-pglock_1.0.0-1PIGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
pglock_15 1.0.0 el8.x86_64 pigsty 10.3 KiB pglock_15-1.0.0-1PIGSTY.el8.x86_64.rpm
pglock_15 1.0.0 el8.aarch64 pigsty 10.3 KiB pglock_15-1.0.0-1PIGSTY.el8.aarch64.rpm
pglock_15 1.0.0 el9.x86_64 pigsty 10.3 KiB pglock_15-1.0.0-1PIGSTY.el9.x86_64.rpm
pglock_15 1.0.0 el9.aarch64 pigsty 10.3 KiB pglock_15-1.0.0-1PIGSTY.el9.aarch64.rpm
pglock_15 1.0.0 el10.x86_64 pigsty 10.5 KiB pglock_15-1.0.0-1PIGSTY.el10.x86_64.rpm
pglock_15 1.0.0 el10.aarch64 pigsty 10.4 KiB pglock_15-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-15-pglock 1.0.0 d12.x86_64 pigsty 4.6 KiB postgresql-15-pglock_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-15-pglock 1.0.0 d12.aarch64 pigsty 4.6 KiB postgresql-15-pglock_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-15-pglock 1.0.0 d13.x86_64 pigsty 4.6 KiB postgresql-15-pglock_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-15-pglock 1.0.0 d13.aarch64 pigsty 4.6 KiB postgresql-15-pglock_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-15-pglock 1.0.0 u22.x86_64 pigsty 4.0 KiB postgresql-15-pglock_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-15-pglock 1.0.0 u22.aarch64 pigsty 4.0 KiB postgresql-15-pglock_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-15-pglock 1.0.0 u24.x86_64 pigsty 4.0 KiB postgresql-15-pglock_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-15-pglock 1.0.0 u24.aarch64 pigsty 4.0 KiB postgresql-15-pglock_1.0.0-1PIGSTY~noble_arm64.deb
postgresql-15-pglock 1.0.0 u26.x86_64 pigsty 4.0 KiB postgresql-15-pglock_1.0.0-1PIGSTY~resolute_amd64.deb
postgresql-15-pglock 1.0.0 u26.aarch64 pigsty 4.0 KiB postgresql-15-pglock_1.0.0-1PIGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
pglock_14 1.0.0 el8.x86_64 pigsty 10.3 KiB pglock_14-1.0.0-1PIGSTY.el8.x86_64.rpm
pglock_14 1.0.0 el8.aarch64 pigsty 10.3 KiB pglock_14-1.0.0-1PIGSTY.el8.aarch64.rpm
pglock_14 1.0.0 el9.x86_64 pigsty 10.3 KiB pglock_14-1.0.0-1PIGSTY.el9.x86_64.rpm
pglock_14 1.0.0 el9.aarch64 pigsty 10.3 KiB pglock_14-1.0.0-1PIGSTY.el9.aarch64.rpm
pglock_14 1.0.0 el10.x86_64 pigsty 10.5 KiB pglock_14-1.0.0-1PIGSTY.el10.x86_64.rpm
pglock_14 1.0.0 el10.aarch64 pigsty 10.4 KiB pglock_14-1.0.0-1PIGSTY.el10.aarch64.rpm
postgresql-14-pglock 1.0.0 d12.x86_64 pigsty 4.6 KiB postgresql-14-pglock_1.0.0-1PIGSTY~bookworm_amd64.deb
postgresql-14-pglock 1.0.0 d12.aarch64 pigsty 4.6 KiB postgresql-14-pglock_1.0.0-1PIGSTY~bookworm_arm64.deb
postgresql-14-pglock 1.0.0 d13.x86_64 pigsty 4.6 KiB postgresql-14-pglock_1.0.0-1PIGSTY~trixie_amd64.deb
postgresql-14-pglock 1.0.0 d13.aarch64 pigsty 4.6 KiB postgresql-14-pglock_1.0.0-1PIGSTY~trixie_arm64.deb
postgresql-14-pglock 1.0.0 u22.x86_64 pigsty 4.0 KiB postgresql-14-pglock_1.0.0-1PIGSTY~jammy_amd64.deb
postgresql-14-pglock 1.0.0 u22.aarch64 pigsty 4.0 KiB postgresql-14-pglock_1.0.0-1PIGSTY~jammy_arm64.deb
postgresql-14-pglock 1.0.0 u24.x86_64 pigsty 4.0 KiB postgresql-14-pglock_1.0.0-1PIGSTY~noble_amd64.deb
postgresql-14-pglock 1.0.0 u24.aarch64 pigsty 4.0 KiB postgresql-14-pglock_1.0.0-1PIGSTY~noble_arm64.deb
postgresql-14-pglock 1.0.0 u26.x86_64 pigsty 4.0 KiB postgresql-14-pglock_1.0.0-1PIGSTY~resolute_amd64.deb
postgresql-14-pglock 1.0.0 u26.aarch64 pigsty 4.0 KiB postgresql-14-pglock_1.0.0-1PIGSTY~resolute_arm64.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 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 14

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