From 8e86980105b069aee19c1a52735b6e4a7787b082 Mon Sep 17 00:00:00 2001 From: Sergiy Matusevych Date: Mon, 22 Sep 2025 10:05:48 -0700 Subject: [PATCH 1/3] update tables `trial_status` and `trial` in separate transactions (that's OK for MLOS) to make sure PG won't fail the update of `trial` if there is a duplicate record in `trial_status` --- mlos_bench/mlos_bench/storage/sql/trial.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mlos_bench/mlos_bench/storage/sql/trial.py b/mlos_bench/mlos_bench/storage/sql/trial.py index e1276c2881..1abaf329b0 100644 --- a/mlos_bench/mlos_bench/storage/sql/trial.py +++ b/mlos_bench/mlos_bench/storage/sql/trial.py @@ -116,6 +116,7 @@ def update( metrics = super().update(status, timestamp, metrics) with self._engine.begin() as conn: self._update_status(conn, status, timestamp) + with self._engine.begin() as conn: try: if status.is_completed(): # Final update of the status and ts_end: From 06a8d2ca353f82cc5db4607e0db34e45ada2796d Mon Sep 17 00:00:00 2001 From: Sergiy Matusevych Date: Mon, 22 Sep 2025 10:16:05 -0700 Subject: [PATCH 2/3] Update mlos_bench/mlos_bench/storage/sql/trial.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- mlos_bench/mlos_bench/storage/sql/trial.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mlos_bench/mlos_bench/storage/sql/trial.py b/mlos_bench/mlos_bench/storage/sql/trial.py index 1abaf329b0..f5e314c7eb 100644 --- a/mlos_bench/mlos_bench/storage/sql/trial.py +++ b/mlos_bench/mlos_bench/storage/sql/trial.py @@ -116,6 +116,12 @@ def update( metrics = super().update(status, timestamp, metrics) with self._engine.begin() as conn: self._update_status(conn, status, timestamp) + # Use a separate transaction for the following block to avoid issues with + # PostgreSQL's duplicate key constraint handling. If we attempt to insert + # or update rows that may violate unique constraints in the same transaction, + # the entire transaction can fail and be rolled back. Splitting into two + # transactions ensures that the status update is committed even if the + # subsequent insert fails due to a duplicate key. with self._engine.begin() as conn: try: if status.is_completed(): From 8e09bc8d172a184bbfcbee2f89c97d2f0fd822ab Mon Sep 17 00:00:00 2001 From: Sergiy Matusevych Date: Mon, 22 Sep 2025 10:18:13 -0700 Subject: [PATCH 3/3] fix the copilot-generated comment --- mlos_bench/mlos_bench/storage/sql/trial.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/mlos_bench/mlos_bench/storage/sql/trial.py b/mlos_bench/mlos_bench/storage/sql/trial.py index f5e314c7eb..5941d2c358 100644 --- a/mlos_bench/mlos_bench/storage/sql/trial.py +++ b/mlos_bench/mlos_bench/storage/sql/trial.py @@ -116,12 +116,8 @@ def update( metrics = super().update(status, timestamp, metrics) with self._engine.begin() as conn: self._update_status(conn, status, timestamp) - # Use a separate transaction for the following block to avoid issues with - # PostgreSQL's duplicate key constraint handling. If we attempt to insert - # or update rows that may violate unique constraints in the same transaction, - # the entire transaction can fail and be rolled back. Splitting into two - # transactions ensures that the status update is committed even if the - # subsequent insert fails due to a duplicate key. + # Use a separate transaction to avoid issues with PostgreSQL's duplicate key + # constraint handling. (See Issue #999). with self._engine.begin() as conn: try: if status.is_completed():