Database upgrade fails when upgrading from 8.9 to 9.9

We have installed sonarqube 8.9.10 with postgresql . the database upgrade fails with the following message:

2023.02.17 10:18:41 ERROR web[][o.s.s.p.d.m.DatabaseMigrationImpl] DB migration ended with an exception
org.sonar.server.platform.db.migration.step.MigrationStepExecutionException: Execution of migration step #6700 'Move live measure variations to values' failed
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:79)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:67)
        at com.google.common.collect.ImmutableList.forEach(ImmutableList.java:422)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:52)
        at org.sonar.server.platform.db.migration.engine.MigrationEngineImpl.execute(MigrationEngineImpl.java:66)
        at org.sonar.server.platform.db.migration.DatabaseMigrationImpl.doUpgradeDb(DatabaseMigrationImpl.java:105)
        at org.sonar.server.platform.db.migration.DatabaseMigrationImpl.doDatabaseMigration(DatabaseMigrationImpl.java:80)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalStateException: Error during processing of row: [uuid=AX6b8sEHt6gyvapUNzCD,variation=0E-20]
        at org.sonar.server.platform.db.migration.step.SelectImpl.newExceptionWithRowDetails(SelectImpl.java:89)
        at org.sonar.server.platform.db.migration.step.SelectImpl.scroll(SelectImpl.java:81)
        at org.sonar.server.platform.db.migration.step.MassUpdate.execute(MassUpdate.java:93)
        at org.sonar.server.platform.db.migration.version.v98.VariationMigration.migrateVariation(VariationMigration.java:51)
        at org.sonar.server.platform.db.migration.version.v98.VariationMigration.execute(VariationMigration.java:41)
        at org.sonar.server.platform.db.migration.step.DataChange.execute(DataChange.java:44)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:75)
        ... 9 common frames omitted
Caused by: java.sql.BatchUpdateException: Batch entry 34 update live_measures set value = 0.0 where uuid = 'AX60sMK8t6gyvapUV8Xp' was aborted: FEHLER: doppelter Schlüsselwert verletzt Unique-Constraint »live_measures_component«
  Detail: Schlüssel »(component_uuid, metric_uuid)=(AX60sMA_t6gyvapUV72-, 4)« existiert bereits.  Call getNextException to see other errors in the batch.
        at org.postgresql.jdbc.BatchResultHandler.handleError(BatchResultHandler.java:165)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2367)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:560)
        at org.postgresql.jdbc.PgStatement.internalExecuteBatch(PgStatement.java:893)
        at org.postgresql.jdbc.PgStatement.executeBatch(PgStatement.java:916)
        at org.postgresql.jdbc.PgPreparedStatement.executeBatch(PgPreparedStatement.java:1684)
        at com.zaxxer.hikari.pool.ProxyStatement.executeBatch(ProxyStatement.java:127)
        at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeBatch(HikariProxyPreparedStatement.java)
        at org.sonar.server.platform.db.migration.step.UpsertImpl.addBatch(UpsertImpl.java:56)
        at org.sonar.server.platform.db.migration.step.MassUpdate.callSingleHandler(MassUpdate.java:120)
        at org.sonar.server.platform.db.migration.step.MassUpdate.lambda$execute$0(MassUpdate.java:93)
        at org.sonar.server.platform.db.migration.step.SelectImpl.scroll(SelectImpl.java:78)
        ... 14 common frames omitted
Caused by: org.postgresql.util.PSQLException: FEHLER: doppelter Schlüsselwert verletzt Unique-Constraint »live_measures_component«
  Detail: Schlüssel »(component_uuid, metric_uuid)=(AX60sMA_t6gyvapUV72-, 4)« existiert bereits.
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2676)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2366)
        ... 24 common frames omitted

i have searched for the error in the sources and have found some parts, which could help for solving the problem.
if i run the following query on the version 8.9.10, then i get many results:
select component_uuid, metric_uuid, count()
from live_measures
group by component_uuid, metric_uuid
HAVING count(
) > 1;

in different versions, the unique index “Live_measures_component” is created, which contains the columns component_uuid and metric_uuid. if i drop this index, then the upgrade runs. but is this the right way?

in the version 91 are some entries deleted in the table live_measures, which are user defined. i have check in the database, if we have user defined entries with the query " SELECT lm.uuid FROM metrics m join live_measures lm ON m.uuid = lm.metric_uuid WHERE m.user_managed <> ‘f’". Result is zero.

can someone solve the error?

Hey there.

This looks like a duplicate thread.

Please be patient – someone will looik at it soon.

I’ll close this thread.