From ebe5ce666760ca930e23e5ab43dc9aae5886f76f Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Fri, 27 Nov 2026 17:06:29 +0800 Subject: [PATCH] init --- TOC-tidb-cloud-essential.md | 1 + TOC-tidb-cloud-premium.md | 1 + TOC-tidb-cloud-starter.md | 1 + TOC-tidb-cloud.md | 1 + TOC.md | 1 + tidb-commit-ts.md | 109 ++++++++++++++++++++++++++++++++++++ 6 files changed, 114 insertions(+) create mode 100644 tidb-commit-ts.md diff --git a/TOC-tidb-cloud-essential.md b/TOC-tidb-cloud-essential.md index 7bd203200fbce..d33147ea856b2 100644 --- a/TOC-tidb-cloud-essential.md +++ b/TOC-tidb-cloud-essential.md @@ -205,6 +205,7 @@ - [AUTO_INCREMENT](/auto-increment.md) - [AUTO_RANDOM](/auto-random.md) - [_tidb_rowid](/tidb-rowid.md) + - [_tidb_commit_ts](/tidb-commit-ts.md) - [SHARD_ROW_ID_BITS](/shard-row-id-bits.md) - [Literal Values](/literal-values.md) - [Schema Object Names](/schema-object-names.md) diff --git a/TOC-tidb-cloud-premium.md b/TOC-tidb-cloud-premium.md index ac098b876efed..063d1141be6ba 100644 --- a/TOC-tidb-cloud-premium.md +++ b/TOC-tidb-cloud-premium.md @@ -284,6 +284,7 @@ - [AUTO_INCREMENT](/auto-increment.md) - [AUTO_RANDOM](/auto-random.md) - [_tidb_rowid](/tidb-rowid.md) + - [_tidb_commit_ts](/tidb-commit-ts.md) - [SHARD_ROW_ID_BITS](/shard-row-id-bits.md) - [Literal Values](/literal-values.md) - [Schema Object Names](/schema-object-names.md) diff --git a/TOC-tidb-cloud-starter.md b/TOC-tidb-cloud-starter.md index 26cd2fc4852f9..8cb7beb66c7be 100644 --- a/TOC-tidb-cloud-starter.md +++ b/TOC-tidb-cloud-starter.md @@ -209,6 +209,7 @@ - [AUTO_INCREMENT](/auto-increment.md) - [AUTO_RANDOM](/auto-random.md) - [_tidb_rowid](/tidb-rowid.md) + - [_tidb_commit_ts](/tidb-commit-ts.md) - [SHARD_ROW_ID_BITS](/shard-row-id-bits.md) - [Literal Values](/literal-values.md) - [Schema Object Names](/schema-object-names.md) diff --git a/TOC-tidb-cloud.md b/TOC-tidb-cloud.md index 59486d0fcd687..573d2a41c9ba6 100644 --- a/TOC-tidb-cloud.md +++ b/TOC-tidb-cloud.md @@ -270,6 +270,7 @@ - [AUTO_INCREMENT](/auto-increment.md) - [AUTO_RANDOM](/auto-random.md) - [_tidb_rowid](/tidb-rowid.md) + - [_tidb_commit_ts](/tidb-commit-ts.md) - [SHARD_ROW_ID_BITS](/shard-row-id-bits.md) - [Literal Values](/literal-values.md) - [Schema Object Names](/schema-object-names.md) diff --git a/TOC.md b/TOC.md index b08fc72df8e27..d4ab1369d37ea 100644 --- a/TOC.md +++ b/TOC.md @@ -639,6 +639,7 @@ - [AUTO_INCREMENT](/auto-increment.md) - [AUTO_RANDOM](/auto-random.md) - [_tidb_rowid](/tidb-rowid.md) + - [_tidb_commit_ts](/tidb-commit-ts.md) - [SHARD_ROW_ID_BITS](/shard-row-id-bits.md) - [Literal Values](/literal-values.md) - [Schema Object Names](/schema-object-names.md) diff --git a/tidb-commit-ts.md b/tidb-commit-ts.md new file mode 100644 index 0000000000000..afb1f24e77056 --- /dev/null +++ b/tidb-commit-ts.md @@ -0,0 +1,109 @@ +--- +title: _tidb_commit_ts +summary: Learn how to read the hidden _tidb_commit_ts column, use it in queries, and understand its read-only and plan-related limitations. +--- + +# `_tidb_commit_ts` + +`_tidb_commit_ts` is a hidden system column that exposes the commit timestamp of a row in the local TiDB cluster. TiDB obtains this timestamp from the transaction's MVCC commit record and returns it as an unsigned `BIGINT` value. You cannot see this column with `SELECT *`, but you can reference it explicitly by name. + +The value is useful when you need to compare row versions, inspect the order in which rows were committed, or remove rows based on a commit timestamp. A committed row normally has a positive timestamp. The value can be `NULL` for a row version that has not been committed yet inside TiDB. + +## Read `_tidb_commit_ts` + +You can select `_tidb_commit_ts` from a regular TiDB table without enabling a session feature: + +```sql +CREATE TABLE t ( + id INT PRIMARY KEY, + value VARCHAR(20) +); + +INSERT INTO t VALUES (1, 'a'), (2, 'b'); + +SELECT id, _tidb_commit_ts > 0 AS has_commit_ts +FROM t +ORDER BY id; +``` + +```sql ++----+----------------+ +| id | has_commit_ts | ++----+----------------+ +| 1 | 1 | +| 2 | 1 | ++----+----------------+ +``` + +You can use the column in expressions and predicates, including joins and set operations: + +```sql +SELECT id +FROM t +WHERE _tidb_commit_ts > 0 +ORDER BY id; + +SELECT _tidb_commit_ts > 0 AS has_commit_ts FROM t +UNION ALL +SELECT _tidb_commit_ts > 0 FROM t; +``` + +Reading `_tidb_commit_ts` does not depend on the value of `tidb_mview_enable`. + +## Use `_tidb_commit_ts` with table and index scans + +When a query reads `_tidb_commit_ts`, TiDB uses a coprocessor scan that can return the commit timestamp together with the row. For example, a primary-key lookup is planned as a table range scan: + +```sql +EXPLAIN FORMAT = 'plan_tree' +SELECT _tidb_commit_ts FROM t WHERE id = 1; +``` + +The plan contains `TableRangeScan` under `TableReader`. For a secondary-index predicate, TiDB can use an index lookup: + +```sql +ALTER TABLE t ADD INDEX idx_value (value); + +EXPLAIN FORMAT = 'plan_tree' +SELECT _tidb_commit_ts +FROM t USE INDEX (idx_value) +WHERE value = 'a'; +``` + +The plan contains `IndexLookUp`, and the query returns the commit timestamp for the matching row. TiDB does not convert these queries to `Point_Get`, `Batch_Point_Get`, or a single `IndexReader` plan because those operators cannot provide `_tidb_commit_ts`. + +## Use `_tidb_commit_ts` in DML expressions + +You can read the column while filtering or computing values in `UPDATE`, `INSERT ... SELECT`, and `DELETE` statements. Reading the column does not modify it: + +```sql +UPDATE t +SET value = CONCAT(value, '-updated') +WHERE _tidb_commit_ts > 0; + +CREATE TABLE t_copy (id INT, commit_ts BIGINT UNSIGNED); + +INSERT INTO t_copy +SELECT id, _tidb_commit_ts +FROM t +WHERE _tidb_commit_ts > 0; + +DELETE FROM t +WHERE _tidb_commit_ts = 0; +``` + +The last statement does not delete rows when no row has a zero commit timestamp. You can use a range predicate, such as `_tidb_commit_ts > 0`, to delete rows whose commit timestamps meet your retention condition. + +## Restrictions + +- `_tidb_commit_ts` is read-only. You cannot assign a value to it in `INSERT`, `REPLACE`, `UPDATE`, or `INSERT ... ON DUPLICATE KEY UPDATE` statements. An attempted write returns an error similar to `Usage of column name '_tidb_commit_ts' is not supported for now`. +- Do not define a user column with the name `_tidb_commit_ts`. The name is reserved for the hidden system column. +- Queries that read `_tidb_commit_ts` cannot use index merge. If you force an index-merge plan, TiDB returns an error because it cannot find a proper physical plan. +- `_tidb_commit_ts` is not supported when reading a table through the table cache. A query that combines the column with a cached table returns an error similar to `'_tidb_commit_ts' is unsupported on cache tables.` +- The timestamp is an MVCC commit timestamp, not a wall-clock time. To interpret it as a time, use the timestamp utilities appropriate for your TiDB deployment instead of treating the numeric value as Unix time. + +## See also + +- [Optimistic transactions](/optimistic-transaction.md) +- [Pessimistic transactions](/pessimistic-transaction.md) +- [TiCDC protocols](/ticdc/ticdc-avro-protocol.md)