diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5547-collation-index-fix.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5547-collation-index-fix.yaml new file mode 100644 index 00000000000..c6ab2f02556 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5547-collation-index-fix.yaml @@ -0,0 +1,5 @@ +--- +type: fix +issue: 5547 +title: "The addition of the indexes `idx_sp_uri_hash_identity_pattern_ops` and `idx_sp_string_hash_nrm_pattern_ops` could occasionally timeout during migration in Postgresql on large databases, leaving the migration table in a failed state, and Smile CDR unable to boot. +Now existence of the index is checked before attempting to add it again." diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java index 9d50199fb88..636b436746e 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java @@ -184,8 +184,10 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { QUERY_FOR_COLUMN_COLLATION_TEMPLATE, "HFJ_SPIDX_STRING".toLowerCase(), "SP_VALUE_NORMALIZED".toLowerCase()), - "Column HFJ_SPIDX_STRING.SP_VALUE_NORMALIZED already has a collation of 'C' so doing nothing"); - + "Column HFJ_SPIDX_STRING.SP_VALUE_NORMALIZED already has a collation of 'C' so doing nothing") + .onlyIf( + "SELECT NOT EXISTS(select 1 from pg_indexes where indexname='idx_sp_string_hash_nrm_pattern_ops')", + "Index idx_sp_string_hash_nrm_pattern_ops already exists"); version.executeRawSql( "20231212.2", "CREATE UNIQUE INDEX idx_sp_uri_hash_identity_pattern_ops ON public.hfj_spidx_uri USING btree (hash_identity, sp_uri varchar_pattern_ops, res_id, partition_id)") @@ -195,7 +197,10 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { QUERY_FOR_COLUMN_COLLATION_TEMPLATE, "HFJ_SPIDX_URI".toLowerCase(), "SP_URI".toLowerCase()), - "Column HFJ_SPIDX_STRING.SP_VALUE_NORMALIZED already has a collation of 'C' so doing nothing"); + "Column HFJ_SPIDX_STRING.SP_VALUE_NORMALIZED already has a collation of 'C' so doing nothing") + .onlyIf( + "SELECT NOT EXISTS(select 1 from pg_indexes where indexname='idx_sp_uri_hash_identity_pattern_ops')", + "Index idx_sp_uri_hash_identity_pattern_ops already exists."); } // This fix was bad for MSSQL, it has been set to do nothing. diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java index 3f47dc4606d..39365829c77 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java @@ -597,11 +597,11 @@ public class Builder { "Only SELECT statements (including CTEs) are allowed here. Please check your SQL: [%s]", theSql)); } - ourLog.info("SQL to evaluate: {}", theSql); + ourLog.debug("SQL to evaluate: {}", theSql); myTask.addPrecondition(new ExecuteTaskPrecondition( () -> { - ourLog.info("Checking precondition for SQL: {}", theSql); + ourLog.debug("Checking precondition for SQL: {}", theSql); return MigrationJdbcUtils.queryForSingleBooleanResultMultipleThrowsException( theSql, myTask.newJdbcTemplate()); },