Repository navigation
fix(tool/mysql-list-active-queries): compute transaction durations in the server's system time zone - #4517
Conversation
… the server's system time zone information_schema.innodb_trx.trx_started and trx_wait_started are in the server's system time zone, but the durations were computed against UTC_TIMESTAMP(), so they were off by the UTC offset on servers whose system time zone is not UTC. Compare against UNIX_TIMESTAMP() and convert the start times from SYSTEM to the session time zone before UNIX_TIMESTAMP() reads them. Fixes googleapis#4516
There was a problem hiding this comment.
Code Review
This pull request updates the active query listing statements for MySQL and CloudSQL to calculate transaction and wait durations by converting transaction start times from the system timezone to the session timezone. The reviewer points out that using @@session.time_zone with CONVERT_TZ can result in NULL values if timezone tables are unpopulated, and suggests a more robust alternative using TIMESTAMPDIFF and converting to UTC ('+00:00').
| (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds, | ||
| (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds, |
There was a problem hiding this comment.
Using @@session.time_zone in CONVERT_TZ can return NULL if the session timezone is set to a named timezone (e.g., 'Asia/Kolkata') and the MySQL timezone tables are not populated (which is common in default or minimal MySQL installations).
A more robust approach that is completely immune to unpopulated timezone tables is to convert the system time to UTC ('+00:00') and compare it with UTC_TIMESTAMP() using TIMESTAMPDIFF. Both 'SYSTEM' and '+00:00' are always recognized by CONVERT_TZ without requiring timezone tables.
| (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds, | |
| (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds, | |
| TIMESTAMPDIFF(SECOND, CONVERT_TZ(t.trx_started, 'SYSTEM', '+00:00'), UTC_TIMESTAMP()) AS trx_duration_seconds, | |
| TIMESTAMPDIFF(SECOND, CONVERT_TZ(t.trx_wait_started, 'SYSTEM', '+00:00'), UTC_TIMESTAMP()) AS trx_wait_duration_seconds, |
| (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds, | ||
| (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds, |
There was a problem hiding this comment.
Using @@session.time_zone in CONVERT_TZ can return NULL if the session timezone is set to a named timezone (e.g., 'Asia/Kolkata') and the MySQL timezone tables are not populated (which is common in default or minimal MySQL installations).
A more robust approach that is completely immune to unpopulated timezone tables is to convert the system time to UTC ('+00:00') and compare it with UTC_TIMESTAMP() using TIMESTAMPDIFF. Both 'SYSTEM' and '+00:00' are always recognized by CONVERT_TZ without requiring timezone tables.
| (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds, | |
| (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds, | |
| TIMESTAMPDIFF(SECOND, CONVERT_TZ(t.trx_started, 'SYSTEM', '+00:00'), UTC_TIMESTAMP()) AS trx_duration_seconds, | |
| TIMESTAMPDIFF(SECOND, CONVERT_TZ(t.trx_wait_started, 'SYSTEM', '+00:00'), UTC_TIMESTAMP()) AS trx_wait_duration_seconds, |
Description
mysql-list-active-queriescomputedtrx_duration_secondsandtrx_wait_duration_secondsasUNIX_TIMESTAMP(UTC_TIMESTAMP()) - UNIX_TIMESTAMP(t.trx_started).information_schema.innodb_trx.trx_started/trx_wait_startedare in the server's system time zone, whileUTC_TIMESTAMP()is UTC wall-clock time, so both columns were off by the server's UTC offset whenever the system time zone is not UTC (e.g. -19794 instead of 6 on a server in IST, see #4516).This change compares against
UNIX_TIMESTAMP()(current Unix time) and converts the start time from the system time zone to the session time zone beforeUNIX_TIMESTAMP()interprets it:CONVERT_TZis a no-op when the session time zone isSYSTEM, and NULL stays NULL for transactions that are not waiting. Both statements (MySQL and Cloud SQL for MySQL) are changed so they stay identical.Verification: with the mysql client against MySQL 8.0.46 started with
TZ=Asia/Kolkata, I ran the updatedlistActiveQueriesStatementMySQLas a prepared statement while one transaction held a row lock and a second waited on it: it returnedtrx_duration_seconds = 6for both andtrx_wait_duration_seconds = 6for the waiting one (the old statement returns -19794 for those values). I also checked the new expression on that server with sessiontime_zone+00:00and-08:00, and on a server with system time zone UTC (sessionSYSTEMand+05:00), where it returns the same values as the old one.Test: no test change. CI runs MySQL with a UTC system time zone, where the old and new expressions return the same values, so an integration test there would not catch this. The existing
RunMySQLListActiveQueriesTeststill exercises the statement. I could not run the Go tests locally (my environment cannot download the Go toolchain required bygo.modor the test images), so I'm relying on CI for them.Security: the change only touches the SELECT list of a fixed statement; the parameters and their binding are unchanged.
PR Checklist
!if this involve a breaking change (not breaking)🛠️ Fixes #4516