Skip to content

Add adapter level read-only transaction support#741

Open
cdvx wants to merge 3 commits into
elixir-ecto:masterfrom
cdvx:add-read-only-opt-to-transaction
Open

Add adapter level read-only transaction support#741
cdvx wants to merge 3 commits into
elixir-ecto:masterfrom
cdvx:add-read-only-opt-to-transaction

Conversation

@cdvx

@cdvx cdvx commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Adds repo.transaction(read_only: true) support for SQL adapters by moving the read-only transaction handling into adapter-level connection callbacks.

This lets mix ecto.query use the shared transaction option instead of adapter-specific transaction SQL itself.

Closes #736.

- Add adapter-level read_only_transaction callbacks for SQL adapters
- Update mix ecto.query to use the transaction read_only opt instead of  adapter-specific transaction SQL.
@cdvx cdvx force-pushed the add-read-only-opt-to-transaction branch from e221a67 to c31b6eb Compare July 13, 2026 09:06
Comment thread lib/ecto/adapters/myxql/connection.ex Outdated
Comment thread lib/ecto/adapters/tds/connection.ex Outdated
Comment thread lib/ecto/adapters/myxql/connection.ex Outdated
- Log SET TRANSACTION READ ONLY through the transaction logger
- Remove the TDS read_only_transaction callback so Ecto.Adapters.SQL raises the unsupported adapter error
@cdvx cdvx requested review from josevalim and v0idpwn July 13, 2026 17:19
Comment thread lib/ecto/adapters/sql.ex Outdated

## Connection helpers

defp read_only_transaction(adapter_meta, connection, opts, callback) do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm.... what happens if we are already inside a transaction, which is not read-only, and we call this? One option is to detect and raise but that means additional bookkeeping. And because the database themselves are very different when it comes to this functionality, the API is awkward.

I am thinking we don't add this functionality as part of the official repo or transaction API. We just add this exclusively to the adapter and call the adapter directly in mix ecto.query?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 🤔, let me adjust to this

- Move read-only query execution out of the Repo.transaction API
- Call the SQL adapter hook directly from mix ecto.query.
@cdvx cdvx requested a review from josevalim July 15, 2026 08:28

@josevalim josevalim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Any other remarks @v0idpwn?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add read_only: true option to transaction

3 participants