> 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](/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/postgresoutbox.md).

## Package

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

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

## Configuration

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

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

| Setting            | Type     | Default      | Description                                                                  |
| ------------------ | -------- | ------------ | ---------------------------------------------------------------------------- |
| `ConnectionString` | `string` | *(required)* | The connection string used to open the session that holds the advisory lock. |

## Example

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

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

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

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

## 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/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
