Repository navigation
docs: document database-level read-only mode #24769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0ddb06f
f59a816
e6e53b6
3cdca10
7d5a971
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| --- | ||
| title: Database-Level Read-Only Mode | ||
| summary: Learn how to set individual TiDB databases to read-only for migration and validation, and understand write restrictions and privileges. | ||
| --- | ||
|
|
||
| # Database-Level Read-Only Mode | ||
|
|
||
| Database-level read-only mode restricts writes to an individual database while keeping ordinary queries available. Use it for migration cutover or validation while other databases remain writable. | ||
|
|
||
| <!-- TODO: Confirm the first release that includes database-level read-only mode and the SQL sequence restore fix. --> | ||
|
|
||
| New databases are writable. The setting persists across TiDB nodes and restarts. For cluster-wide control, use [`tidb_super_read_only`](/system-variables.md#tidb_super_read_only-new-in-v531) or [`tidb_restricted_read_only`](/system-variables.md#tidb_restricted_read_only-new-in-v520). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - All TiDB nodes must support this feature before you enable it or restore read-only database metadata. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identify the first supported release. This prerequisite does not tell users which TiDB version supports the feature. They cannot use it to plan an upgrade or restore. Confirm the first release that includes the feature and the sequence restore fix, then state it here. The feature was merged into the release-8.5 branch on November 24, 2026. (github.com)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that the first supported release needs to be stated before publication. The exact release version is still awaiting confirmation, so the existing explicit TODO is retained for now. Merging the feature into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @expxiaoli A merge into I’m not marking the finding as addressed while the versions remain unconfirmed. Learnings added
You are interacting with an AI system. |
||
| - Changing the setting requires the `ALTER` privilege on the database. | ||
| - System databases, including `mysql`, `sys`, `INFORMATION_SCHEMA`, `PERFORMANCE_SCHEMA`, and `METRICS_SCHEMA`, cannot be set to read-only. | ||
|
|
||
| ## Set and check read-only mode | ||
|
|
||
| Create a database and enable read-only mode: | ||
|
|
||
| ```sql | ||
| CREATE DATABASE app; | ||
| CREATE TABLE app.orders (id INT PRIMARY KEY); | ||
| INSERT INTO app.orders VALUES (1); | ||
| ALTER DATABASE app READ ONLY = 1; | ||
| ``` | ||
|
|
||
| Queries remain available: | ||
|
|
||
| ```sql | ||
| SELECT * FROM app.orders; | ||
| ``` | ||
|
|
||
| Without the bypass privilege, writes fail: | ||
|
|
||
| ```sql | ||
| INSERT INTO app.orders VALUES (2); | ||
| ``` | ||
|
|
||
| ```text | ||
| ERROR 3989 (HY000): Schema 'app' is in read only mode. | ||
| ``` | ||
|
|
||
| Check [`INFORMATION_SCHEMA.SCHEMATA_EXTENSIONS`](/information-schema/information-schema-schemata-extensions.md): | ||
|
|
||
| ```sql | ||
| SELECT SCHEMA_NAME, OPTIONS | ||
| FROM INFORMATION_SCHEMA.SCHEMATA_EXTENSIONS | ||
| WHERE SCHEMA_NAME = 'app'; | ||
| ``` | ||
|
|
||
| `OPTIONS` is `READ ONLY=1` for a read-only database, or empty for a writable database. [`SHOW CREATE DATABASE app`](/sql-statements/sql-statement-show-create-database.md) also shows `/* READ ONLY = 1 */` for read-only databases. | ||
|
|
||
| To restore write access: | ||
|
|
||
| ```sql | ||
| ALTER DATABASE app READ ONLY = 0; | ||
| ``` | ||
|
|
||
| `READ ONLY = DEFAULT` also restores write access. For complete syntax, see [`ALTER DATABASE`](/sql-statements/sql-statement-alter-database.md). | ||
|
|
||
| ## Transaction behavior | ||
|
|
||
| Enabling read-only mode waits for existing transactions that accessed tables in the database to finish, including read transactions on other nodes. After the flag propagates, writes from new transactions without the bypass privilege are rejected. Existing transactions may still issue ordinary writes to tables they accessed before the transition while the DDL waits for them to finish. Wait for `ALTER DATABASE ... READ ONLY = 1` to complete before treating the transition as finished. | ||
|
|
||
| Finish blocking transactions or cancel the DDL using [`ADMIN CANCEL DDL JOBS`](/sql-statements/sql-statement-admin-cancel-ddl.md). Failed or canceled transitions roll back the setting. Disabling read-only mode does not require transaction draining. | ||
|
|
||
| An older transaction first accessing the database after a state change can fail with error `8028`. Retry the transaction. | ||
|
|
||
| ## SQL behavior and privileges | ||
|
|
||
| Without the bypass privilege, the following restrictions apply: | ||
|
|
||
| | Operation | Behavior | | ||
| | --- | --- | | ||
| | Ordinary `SELECT`, `SHOW`, and `USE` | Allowed. | | ||
| | `INSERT`, `REPLACE`, `UPDATE`, `DELETE`, `LOAD DATA`, and `IMPORT INTO` | Rejected for read-only write targets. | | ||
| | DDL that changes databases, tables, views, indexes, or sequences | Rejected, except changing `READ ONLY` itself or compacting TiFlash replicas. | | ||
| | Locking reads | Rejected when the actual lock target is in a read-only database. | | ||
| | `NEXTVAL` and `SETVAL` | Rejected for sequences in a read-only database. | | ||
| | Temporary tables | Creation and writes are restricted, including writes to existing local temporary tables. | | ||
| | TTL deletion | Cannot delete rows in a read-only database. | | ||
|
|
||
| Cross-database checks cover write targets, foreign-key cascades, and both databases in table moves. Reading a read-only database while writing to a writable database is allowed. | ||
|
|
||
| Explicitly granting [`RESTRICTED_REPLICA_WRITER_ADMIN`](/privilege-management.md#dynamic-privileges) to a user or active role permits bypassing read-only restrictions. Operation-specific privileges are still required. `SUPER` alone is insufficient. Prepared execution rechecks current bypass privileges and roles. | ||
|
|
||
| ## Compatibility and limitations | ||
|
|
||
| - `READ ONLY` is supported by `ALTER DATABASE`, but not `CREATE DATABASE`. Specify it separately from character set, collation, or placement options. | ||
| - Compared with MySQL 8.0, TiDB also restricts local temporary tables and can reject write statements during `PREPARE`. `EXPLAIN` of restricted writes can also return error `3989`. | ||
| - Cluster-wide read-only restrictions still apply. Setting a database to writable does not override them. | ||
| - `IMPORT INTO` checks read-only mode at statement entry. A physical import that already started can continue after the database becomes read-only. Finish or cancel such imports before relying on unchanged data. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| - With the deprecated batch DML feature enabled (`tidb_enable_batch_dml = ON` and `tidb_dml_batch_size > 0`), an `INSERT` with `tidb_batch_insert = ON` or a `DELETE` with `tidb_batch_delete = ON` can partially commit before a read-only transition and then fail when it runs outside an explicit transaction. Previously committed batches are not rolled back. Let the statement finish before enabling read-only mode, and check for committed changes before retrying a failed statement. | ||
|
|
||
| ### Interaction with other tools | ||
|
|
||
| | Tool | Behavior | | ||
| | --- | --- | | ||
| | TiDB Lightning | Physical imports using the `local` backend can write directly to TiKV into existing tables despite read-only mode. Control these imports separately. Logical imports obey SQL restrictions. | | ||
| | BR and SQL `BACKUP` / `RESTORE` | Snapshot restore preserves backed-up read-only metadata when it creates a database. If the target database already exists, restore retains its current read-only setting. Authorized restores can restore tables and sequences into read-only databases; ordinary writes to read-only databases remain restricted afterward. If you require read-only protection during and after restoring to an existing database, run `ALTER DATABASE ... READ ONLY = 1` and wait for it to complete before starting the restore. If the target database is writable, restored tables can accept ordinary writes before read-only protection takes effect. After restoring, check `INFORMATION_SCHEMA.SCHEMATA_EXTENSIONS`. PITR retains the current read-only setting of reused target databases; do not assume it matches the source setting at the selected time. | | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Qualify SQL sequence restore for release-8.5. If an operator follows this row and enables READ ONLY on an existing target before restoring a snapshot containing a sequence, SQL RESTORE can fail while restoring the sequence value. In the release-8.5 feature commit,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fix is already merged on master via pingcap/tidb#73748. The PR description explicitly states that the release-8.5 documentation assumes pingcap/tidb#73801 has been merged. Before cherry-picking the documentation to release-8.5 and publishing it, we will confirm that the target release includes the fix. I will keep the document body unchanged for now. |
||
| | Dumpling | Can export read-only databases. Replaying the SQL read-only comment does not enable read-only mode. | | ||
| | TiCDC v8.5.8 | Does not replicate database read-only DDL to MySQL downstream databases. | | ||
| | DM v8.5.8 | Pauses with a parsing error on upstream `ALTER DATABASE ... READ ONLY` statements. Explicitly skip these statements to resume. Writes to a read-only downstream database are rejected without the bypass privilege. | | ||
|
|
||
| ## See also | ||
|
|
||
| - [`ALTER DATABASE`](/sql-statements/sql-statement-alter-database.md) | ||
| - [`SCHEMATA_EXTENSIONS`](/information-schema/information-schema-schemata-extensions.md) | ||
| - [Privilege Management](/privilege-management.md) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| --- | ||
| title: SCHEMATA_EXTENSIONS | ||
| summary: Learn how to query the SCHEMATA_EXTENSIONS table to check database-level read-only settings for databases visible to your TiDB user. | ||
| --- | ||
|
|
||
| # SCHEMATA_EXTENSIONS | ||
|
|
||
| The `SCHEMATA_EXTENSIONS` table provides [database-level read-only settings](/database-read-only.md) for databases visible to the current user. | ||
|
|
||
| | Column | Type | Description | | ||
| | --- | --- | --- | | ||
| | `CATALOG_NAME` | `varchar(64)` | The catalog name. This value is always `def`. | | ||
| | `SCHEMA_NAME` | `varchar(64)` | The database name. | | ||
| | `OPTIONS` | `varchar(256)` | `READ ONLY=1` if the database is read-only; an empty string if the database is writable. | | ||
|
|
||
| ## Example | ||
|
|
||
| ```sql | ||
| CREATE DATABASE app; | ||
| ALTER DATABASE app READ ONLY = 1; | ||
| SELECT CATALOG_NAME, SCHEMA_NAME, OPTIONS | ||
| FROM INFORMATION_SCHEMA.SCHEMATA_EXTENSIONS | ||
| WHERE SCHEMA_NAME = 'app'; | ||
| ``` | ||
|
|
||
| ```text | ||
| +--------------+-------------+-------------+ | ||
| | CATALOG_NAME | SCHEMA_NAME | OPTIONS | | ||
| +--------------+-------------+-------------+ | ||
| | def | app | READ ONLY=1 | | ||
| +--------------+-------------+-------------+ | ||
| ``` | ||
|
|
||
| `OPTIONS` describes the database-level setting. It does not indicate whether cluster-wide read-only restrictions are enabled. | ||
|
|
||
| ## See also | ||
|
|
||
| - [`SCHEMATA`](/information-schema/information-schema-schemata.md) | ||
| - [`ALTER DATABASE`](/sql-statements/sql-statement-alter-database.md) | ||
| - [Database-Level Read-Only Mode](/database-read-only.md) |
Uh oh!
There was an error while loading. Please reload this page.