CloudMesh.NetworkMutex.Postgres
2.0.101
dotnet add package CloudMesh.NetworkMutex.Postgres --version 2.0.101
NuGet\Install-Package CloudMesh.NetworkMutex.Postgres -Version 2.0.101
<PackageReference Include="CloudMesh.NetworkMutex.Postgres" Version="2.0.101" />
<PackageVersion Include="CloudMesh.NetworkMutex.Postgres" Version="2.0.101" />
<PackageReference Include="CloudMesh.NetworkMutex.Postgres" />
paket add CloudMesh.NetworkMutex.Postgres --version 2.0.101
#r "nuget: CloudMesh.NetworkMutex.Postgres, 2.0.101"
#:package CloudMesh.NetworkMutex.Postgres@2.0.101
#addin nuget:?package=CloudMesh.NetworkMutex.Postgres&version=2.0.101
#tool nuget:?package=CloudMesh.NetworkMutex.Postgres&version=2.0.101
CloudMesh.NetworkMutex.Postgres
A Postgres-backed distributed lock — cluster-wide mutual exclusion that borrows a Postgres transaction and a row/table lock so only one holder anywhere on the network owns a named lock at a time.
This is a backend for the CloudMesh.NetworkMutex.Abstractions contract. Prefer this backend when you already run Postgres and want blocking acquisition with deterministic, crash-safe release (no lease to tune). For a DynamoDB backend, see CloudMesh.NetworkMutex.DynamoDB.
- Targets: .NET 8, 9, 10 — License: MIT
How it works
Acquiring a lock opens a connection, starts a transaction, takes a ROW EXCLUSIVE lock on a mutexes table and
upserts the named row. A contender's LOCK TABLE/upsert blocks server-side (up to a one-hour command timeout)
until the current holder's transaction commits — which happens when its lock handle is disposed. If the owning
process crashes, Postgres rolls the transaction back and frees the lock automatically. There is no lease to renew.
Install
dotnet add package CloudMesh.NetworkMutex.Postgres
Quick start
using CloudMesh.NetworkMutex.Abstractions;
using CloudMesh.NetworkMutex.Postgres;
var mutex = new PostgresqlMutex("Host=db;Username=app;Password=...;Database=app");
// Once at startup: create the backing table if it doesn't exist.
await mutex.EnsureTablesExistAsync(CancellationToken.None);
// Acquire, work, release.
await using var handle = await mutex.TryAcquireLockAsync("nightly-report", TimeSpan.FromSeconds(30));
if (handle is null)
return; // another node holds the lock — skip
await RunNightlyReportAsync();
// Disposing `handle` (await using) commits the transaction and releases the lock.
You can also supply a connection factory instead of a connection string — useful for pooling or per-tenant databases:
var mutex = new PostgresqlMutex(() => new NpgsqlConnection(connectionString));
Use cases
- Leader election / singleton jobs across a scaled-out service that already uses Postgres.
- Serializing a migration or maintenance task to exactly one node.
- Any critical section where you want a contender to wait for the lock rather than fail fast.
Gotchas
- Call
EnsureTablesExistAsynconce before acquiring locks (idempotent). - A
nullresult means the wait was cancelled/timed out — it is normal contention, not an error. - Each held lock keeps a dedicated connection open for its lifetime; keep critical sections short and always
await usingthe handle so the connection/transaction are released promptly. - Acquisition blocks inside Postgres; the command timeout caps the wait at one hour even if your token never fires.
MIT © Jessie Wadman. Part of CloudMesh.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- CloudMesh.NetworkMutex.Abstractions (>= 2.0.101)
- Microsoft.SourceLink.GitHub (>= 8.0.0)
- Npgsql (>= 8.0.3)
-
net8.0
- CloudMesh.NetworkMutex.Abstractions (>= 2.0.101)
- Microsoft.SourceLink.GitHub (>= 8.0.0)
- Npgsql (>= 8.0.3)
- System.Text.Json (>= 10.0.0)
-
net9.0
- CloudMesh.NetworkMutex.Abstractions (>= 2.0.101)
- Microsoft.SourceLink.GitHub (>= 8.0.0)
- Npgsql (>= 8.0.3)
- System.Text.Json (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.101 | 98 | 8/29/2026 |
| 2.0.100 | 120 | 8/3/2026 |
| 2.0.99 | 126 | 7/14/2026 |
| 2.0.98 | 117 | 7/14/2026 |
| 2.0.97 | 121 | 7/14/2026 |
| 2.0.96 | 118 | 6/28/2026 |
| 2.0.95 | 115 | 6/28/2026 |
| 2.0.94 | 120 | 6/28/2026 |
| 2.0.93 | 119 | 6/27/2026 |
| 2.0.92 | 119 | 6/23/2026 |
| 2.0.89 | 339 | 12/18/2025 |
| 2.0.87-preview | 239 | 8/11/2025 |
| 2.0.85-preview | 172 | 1/29/2025 |
| 2.0.76-preview | 165 | 1/15/2025 |
| 2.0.75-preview | 172 | 1/15/2025 |
| 2.0.74-preview | 171 | 1/15/2025 |
| 2.0.73-preview | 177 | 1/13/2025 |
| 1.0.67 | 260 | 10/28/2024 |
| 1.0.66 | 235 | 10/22/2024 |
| 1.0.65 | 223 | 10/22/2024 |