pingcap / pingcap/tidb

Support `ALTER DATABASE READ ONLY = 1` to set individual databases as read-only

Open
#62,122 0 comments 1 reaction 1 assignee Claimed by @fzzf678 View on GitHub
component/ddl needs-cherry-pick-release-8.5 type/feature-request
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Feature Request

**Is your feature request related to a problem? Please describe:**

In large-scale data migration or consolidation scenarios, users often need to migrate multiple upstream MySQL or Aurora shards into a single TiDB cluster. Each shard typically maps to an independent database. During migration, to ensure data safety and system resilience, it’s crucial to be able to mark individual databases as read-only. For example, to handle exception rollback, allow reverse replication via TiCDC, or prevent data modification during validation.

Currently, TiDB only supports cluster-level read-only mode via the `tidb_restricted_read_only` variable. This lacks the granularity needed for per-database isolation. Additionally, `LOCK TABLES` is not practical due to implementation limitations and scalability concerns in production.

**Describe the feature you'd like:**

- `READ ONLY = 1` makes the database read-only, rejecting all DML and most DDL operations.
- `READ ONLY = 0 or DEFAULT` restores the database to a writable state.
- During the state transition to read-only, TiDB should wait for existing transactions that have accessed the database to complete before enforcing the read-only state.
- Temporary tables are exempted from the read-only restriction.
- The read-only state is visible in `SHOW CREATE DATABASE` and in a new `INFORMATION_SCHEMA.SCHEMATA_EXTENSIONS` view.

**Describe alternatives you've considered:**

- `tidb_restricted_read_only`: Not suitable because it applies cluster-wide, not per-database.
- `LOCK TABLES`: Not scalable for databases with hundreds of tables, and not yet production-ready for this purpose.
- Custom access control or application-layer restrictions: Complex to maintain and error-prone during rapid traffic switchovers.

**Teachability, Documentation, Adoption, Migration Strategy:**

- Teachability: Aligns with MySQL’s behavior and syntax for READ ONLY (Not total same), lowering the learning curve for MySQL users.
- Documentation: Requires updates to TiDB SQL syntax documentation and system table references, including new view `SCHEMATA_EXTENSIONS`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.