Add upsert in _set_trial_attr_without_commit for PostgreSQL#6282
Merged
gen740 merged 3 commits intooptuna:masterfrom Oct 3, 2025
Merged
Conversation
_set_trial_attr_without_commit for PostgreSQL
Member
|
@kAIto47802 Could you review this PR? |
Contributor
|
This pull request has not seen any recent activity. |
Member
|
@kAIto47802, @c-bata @nabenabe0928 @not522, could you review this PR? |
not522
reviewed
Oct 2, 2025
Member
not522
left a comment
There was a problem hiding this comment.
Sorry for the late response. It's almost LGTM. Could you update the TODO comment?
Co-authored-by: Naoto Mizuno <naotomizuno@preferred.jp>
gen740
approved these changes
Oct 3, 2025
Member
gen740
left a comment
There was a problem hiding this comment.
LGTM!
TBH, in my environment (MacOS 26 Tahoe, MacBook Pro M3 Max), there is not much difference in performance between master and this PR.
This PR
Benchmark 1: python3 pr6282.py
Time (mean ± σ): 727.3 ms ± 11.3 ms [User: 590.3 ms, System: 58.1 ms]
Range (min … max): 715.5 ms … 745.9 ms 10 runs
Master
Benchmark 1: python3 pr6282.py
Time (mean ± σ): 745.4 ms ± 18.8 ms [User: 581.6 ms, System: 63.4 ms]
Range (min … max): 710.2 ms … 782.1 ms 10 runs
Member
|
Note: I confirmed that this PR ensures faster performance if value updates occur. (8.02s -> 7.87s) import optuna
postgresql_url = "postgresql+psycopg2://user:test@127.0.0.1/optunatest"
study = optuna.create_study(storage=postgresql_url, study_name="test")
storage = study._storage
def objective(trial: optuna.Trial) -> float:
storage.set_trial_system_attr(trial._trial_id, "system_attr_1", "value_1")
storage.set_trial_system_attr(trial._trial_id, "system_attr_1", "new_value_1")
storage.set_trial_system_attr(trial._trial_id, "system_attr_2", "value_2")
return trial.suggest_float("x", 0.0, 1.0) ** 2
study.optimize(objective, n_trials=1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR adds upsert in _set_trial_attr_without_commit for postgresql. It is an expansion of #5741 and #5703. These two PR's added upsert for sqlite and mysql for trial attrs, but postgresql was marked as TODO, this adds support for postgresql.
Description of the changes
From my research it seems sqlalchemy already has the upsert function on_conflict_do_update similar to sqlite and mysql. I just had to add import dialects and a conditional call for postgres.
I checked sqlalchemy docs it looks like 1.4.2 supports postgresql on_conflict_do_update, so don't think we need to change any dependencies.
https://docs.sqlalchemy.org/en/14/dialects/postgresql.html#sqlalchemy.dialects.postgresql.Insert.on_conflict_do_update
Verification of SQL changes
I followed the same verification example provided in #5741 and #5703 for verification
Code
Output
Before
After
Performance Check
Code
Output
Release 4.5.0
Master 4.6.0.dev0 with fix
this is my first PR for optuna, so not sure If it has everything thats needed, if not please let me know.