> For the complete documentation index, see [llms.txt](https://brightercommand.gitbook.io/paramore-brighter-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://brightercommand.gitbook.io/paramore-brighter-documentation/outbox-and-inbox/distributedlock/postgresdistributedlock.md).

# Postgres Distributed Lock

The Postgres locking provider implements Brighter's distributed lock using PostgreSQL advisory locks, so a single Outbox Sweeper and Archiver run when you scale out.

> **Reference** · Applies to **Brighter V10**

The Postgres locking provider implements Brighter's [distributed lock](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md) using PostgreSQL **advisory locks**, so a single [Outbox Sweeper](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport.md#implicit-clear) and Archiver run when you scale out. It pairs naturally with the [Postgres Outbox](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport/postgresoutbox.md).

## Postgres Distributed Lock Package

* **Paramore.Brighter.Locking.PostgresSql**

Advisory locks are a built-in PostgreSQL feature, so there is **no table to provision** — see [Provisioning](#postgres-distributed-lock-provisioning).

## Postgres Distributed Lock Configuration

Configure the provider with `PostgresLockingProvider`, passing a `PostgresLockingProviderOptions` that carries the connection string:

```csharp
using Paramore.Brighter.Locking.PostgresSql;

new PostgresLockingProvider(
    new PostgresLockingProviderOptions(
        connectionString: "Host=localhost;Database=orders;Username=app;Password=secret"));
```

| Option             | Type     | Default | Description                                     |
| ------------------ | -------- | ------- | ----------------------------------------------- |
| `connectionString` | `string` | `none`  | Opens the session that holds the advisory lock. |

That constructor parameter is the whole of the type: it surfaces as the get-only `ConnectionString`, and there is nothing else to set. In particular the provider does **not** take a [relational database configuration](/paramore-brighter-documentation/brighter-configuration/brighterbasicconfiguration/relationaldatabaseconfigurationreference.md), even though the Postgres Outbox beside it does.

## Postgres Distributed Lock Example

```csharp
using Microsoft.Extensions.DependencyInjection;
using Paramore.Brighter;
using Paramore.Brighter.Extensions.DependencyInjection;
using Paramore.Brighter.Locking.PostgresSql;
using Paramore.Brighter.Outbox.Hosting;
using Paramore.Brighter.PostgreSql;

const string connectionString = "Host=localhost;Database=orders;Username=app;Password=secret";

services.AddSingleton<IAmARelationalDatabaseConfiguration>(new RelationalDatabaseConfiguration(connectionString));

services
    .AddBrighter()
    .AddProducers(opt =>
    {
        opt.Outbox = outbox; // your Postgres Outbox
        opt.ConnectionProvider = typeof(PostgreSqlConnectionProvider);
        opt.TransactionProvider = typeof(PostgreSqlTransactionProvider);

        opt.DistributedLock = new PostgresLockingProvider(
            new PostgresLockingProviderOptions(connectionString));
    })
    .UseOutboxSweeper(opt => { opt.BatchSize = 10; });
```

## Postgres Distributed Lock Provisioning

None. PostgreSQL advisory locks are session-scoped locks that live in the server's memory, so the provider needs no table or schema. The lock is held for the lifetime of the database session and is released when that session closes — including if the holding instance crashes and its connection drops.

## Postgres Distributed Lock Notes

* Because the lock is tied to a session rather than a timed lease, there is no `LeaseValidity` to tune. Recovery after a crash happens as soon as the dropped connection is detected by the server.
* Point the provider at the same PostgreSQL instance as your Outbox so the Sweeper, Archiver, and lock all share infrastructure.

## Further Reading

* [Distributed Lock](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md) — concepts and the full provider list
* [Postgres Outbox](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport/postgresoutbox.md) — the matching Outbox
* [Outbox Support](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport.md) — the Sweeper and Archiver


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://brightercommand.gitbook.io/paramore-brighter-documentation/outbox-and-inbox/distributedlock/postgresdistributedlock.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
