Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 【百天打卡004】开启系统变量tidb_general_log,实时跟踪sql

Learning and practicing from time to time, isn’t it a pleasure~
Sharing a system variable that achieves a similar trace statement function as SQL Server. Once enabled, you can track data in real-time without needing to use slow queries to trace statements anymore~
tidb_general_log
- Scope: GLOBAL
- Persisted to the cluster: No, only applies to the current TiDB instance connection
- Default value:
OFF
- This variable is used to set whether to record all SQL statements in the log. This feature is disabled by default. If system administrators need to trace all SQL records during troubleshooting, they can consider enabling this feature.
- When the TiDB configuration item
log.level
is set to"info"
or"debug"
, you can locate all records of this feature in the log by querying the"GENERAL_LOG"
string. The log will record the following content:conn
: ID corresponding to the current sessionuser
: Current session userschemaVersion
: Current schema versiontxnStartTS
: Start timestamp of the current transactionforUpdateTS
: Current timestamp of the SQL statement when the transaction mode is pessimistic. If a write conflict occurs within a pessimistic transaction, the current execution statement will be retried, and this timestamp will be updated. The number of retries is configured bymax-retry-count
. When the transaction mode is optimistic, this entry is equivalent totxnStartTS
.isReadConsistency
: Whether the current transaction isolation level is Read Committed (RC)current_db
: Current database nametxn_mode
: Transaction mode. Options:OPTIMISTIC
(optimistic transaction mode) orPESSIMISTIC
(pessimistic transaction mode)sql
: SQL statement corresponding to the current query