> For the complete documentation index, see [llms.txt](https://crystal-garage.gitbook.io/lustra-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://crystal-garage.gitbook.io/lustra-docs/model/locks.md).

# Locks

Lustra exposes PostgreSQL locks in two ways:

* row locks with `with_lock` on select and model collection queries
* table locks with `Lustra::SQL.lock`

Use locks inside a transaction so the lock is held for the intended unit of work.

## Row Locks

`with_lock` appends a lock clause to the generated `SELECT`.

```crystal
Lustra::SQL.transaction do
  User.query
    .where(organization: "Crystal Lang")
    .with_lock
    .each do |user|
      user.update!(active: false)
    end
end
```

By default, `with_lock` uses `FOR UPDATE`.

```sql
SELECT * FROM "users" WHERE ("organization" = 'Crystal Lang') FOR UPDATE
```

Pass a custom PostgreSQL lock clause when needed.

```crystal
User.query
  .where(processed: false)
  .with_lock("FOR UPDATE SKIP LOCKED")
  .limit(100)
  .each do |job|
    job.update!(processed: true)
  end
```

The same method is available on low-level select builders.

```crystal
Lustra::SQL.select
  .from(:jobs)
  .where(processed: false)
  .with_lock("FOR UPDATE SKIP LOCKED")
  .to_a
```

## Table Locks

Use `Lustra::SQL.lock` to lock a full table for the duration of a block.

```crystal
Lustra::SQL.lock(:repositories) do
  Lustra::SQL.insert(:repositories, {name: "lustra"}).execute
end
```

The default lock mode is `ACCESS EXCLUSIVE`.

Pass another PostgreSQL lock mode as the second argument.

```crystal
Lustra::SQL.lock(:repositories, "SHARE") do
  count = Repository.query.count
end
```

`Lustra::SQL.lock` opens a transaction internally and yields the checked-out connection.

```crystal
Lustra::SQL.lock(:repositories) do |connection|
  connection.exec("SELECT 1")
end
```

PostgreSQL supports lock modes such as `ACCESS SHARE`, `ROW SHARE`, `ROW EXCLUSIVE`, `SHARE UPDATE EXCLUSIVE`, `SHARE`, `SHARE ROW EXCLUSIVE`, `EXCLUSIVE`, and `ACCESS EXCLUSIVE`.

See the PostgreSQL locking documentation for the exact compatibility rules between lock modes.


---

# 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://crystal-garage.gitbook.io/lustra-docs/model/locks.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.
