#6868·optuna

`RDBStorage` shifts trial timestamps written before v5.0 by the local UTC offset: legacy naive-local rows are read as UTC since #6776 (`3.2.0.a.db`: 13:23 on 4.9.0, 22:23 on 5.x in Asia/Tokyo)

Author: glaziermagCreated Sep 24, 2026Updated Sep 25, 2026

Motivation

Since #6776 (shipped in v5.0.0), TrialModel stores datetime_start / datetime_complete as naive UTC and converts them to naive local time on read (models.py L183-L200 at 984b37b). Databases written by Optuna ≤ 4.9 hold naive local wall-clock values (the previous datetime.now()), and SCHEMA_VERSION is still 12, so 5.x opens them without optuna storage upgrade and returns every pre-existing timestamp shifted by the reader's UTC offset (+9 h in Asia/Tokyo, −4 h in America/New_York). The same file read with 4.9.0 returns the stored value unchanged in every timezone. JournalStorage is not affected: naive strings in an old journal pass through unchanged, so the two backends now disagree about the same history.

#6776 describes itself as normalizing "newly stored trial timestamps to UTC while preserving the existing public representation of trial datetimes"; for existing RDB rows the representation is not preserved.

Reproduction (committed asset, ~5 s, at 984b37b)

bash
$ cp tests/storages_tests/rdb_tests/test_upgrade_assets/3.2.0.a.db /tmp/a.db
$ sqlite3 /tmp/a.db "select datetime_start from trials where trial_id=1"
2023-03-13 13:23:06.481467
$ for tz in UTC Asia/Tokyo America/New_York; do TZ=$tz python -c "import optuna; print('$tz', optuna.load_study(study_name='single', storage='sqlite:////tmp/a.db').trials[0].datetime_start)"; done
UTC 2023-03-13 13:23:06.481467
Asia/Tokyo 2023-03-13 22:23:06.481467
America/New_York 2023-03-13 09:23:06.481467

With optuna==4.9.0 on the same file, all three print 2023-03-13 13:23:06.481467. The journal asset tests/storages_tests/journal_tests/assets/4.0.0.dev.log (naive strings such as 2024-06-07T17:27:27.682770) prints 2024-06-07 17:27:27.682770 under all three TZ values on master.

A study that spans the upgrade ends up with inverted timestamps. In Asia/Tokyo, two trials run with 4.9.0 at 01:28:04 and 01:28:06, then one trial run with master one second later:

number  datetime_start (read by master, TZ=Asia/Tokyo)
0       2026-09-25 10:28:04
1       2026-09-25 10:28:06
2       2026-09-25 01:28:06

Trial 2 ran last but is reported nine hours before trials 0 and 1, and trials_dataframe, plot_timeline and Optuna Dashboard show it that way.

Suggestion

Legacy and UTC rows are indistinguishable without a marker, so I think this needs the migration #6776 avoided: bump SCHEMA_VERSION and add an alembic revision that rewrites the two columns from local to UTC (value.astimezone(timezone.utc) in the upgrading process's zone) when the user runs optuna storage upgrade, which the existing version check then requires before 5.x opens a pre-5.0 database. If you would rather keep the current behaviour, the v5.0.0 breaking-changes entry ("Normalize trial timestamps to UTC in RDBStorage and JournalStorage") should say that existing databases read differently after upgrading.

Environment: master 984b37b (5.1.0.dev), Python 3.14.4, SQLite 3.53.0, macOS; control run with optuna 4.9.0 in a separate venv.

Prepared with LLM assistance (Claude); every command above was executed and its output pasted verbatim.