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
Open
expxiaoli
wants to merge
6
commits into
pingcap:master
Choose a base branch
from
expxiaoli:read_only
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+188
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ddb06f
docs: document database-level read-only mode
expxiaoli f59a816
docs: clarify read-only restore and batch DML behavior
expxiaoli e6e53b6
docs: clarify batch DELETE and restore write protection
expxiaoli 3cdca10
docs: clarify PITR read-only state for reused databases
expxiaoli 7d5a971
docs: clarify writes during read-only transitions
expxiaoli dd8f12a
docs: specify database read-only mode introduction version
expxiaoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| 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 | ||
|
|
||
| Starting from v8.5.9, TiDB supports database-level read-only mode, which restricts writes to an individual database while keeping ordinary queries available. Use it for migration cutover or validation while other databases remain writable. | ||
|
|
||
| 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. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| - 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. | | ||
|
expxiaoli marked this conversation as resolved.
Resolved
|
||
| | 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) | ||
40 changes: 40 additions & 0 deletions
40
information-schema/information-schema-schemata-extensions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.