From 59f7d4a9a36a2a7b2a22c4a2bac4c54ea62ef9fe Mon Sep 17 00:00:00 2001 From: Michael Buckley Date: Tue, 30 Jan 2024 20:04:27 -0500 Subject: [PATCH] Index review fixes (#5649) * Don't hold locks while adding indices during upgrade. * concurrent indexing is non-transactional in PG. --- .../fhir/changelog/7_0_0/5649-index-review.yaml | 4 ++++ .../migrate/tasks/HapiFhirJpaMigrationTasks.java | 14 +++++++++++--- .../ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5649-index-review.yaml diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5649-index-review.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5649-index-review.yaml new file mode 100644 index 00000000000..bdd4182bde9 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5649-index-review.yaml @@ -0,0 +1,4 @@ +--- +type: fix +issue: 5649 +title: "Change database upgrade script to avoid holding locks while adding indices." 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 340a6cbee2d..4046890a9f0 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 @@ -133,10 +133,12 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { mdmLinkTable .addIndex("20230911.1", "IDX_EMPI_TGT_MR_LS") .unique(false) + .online(true) .withColumns("TARGET_TYPE", "MATCH_RESULT", "LINK_SOURCE"); mdmLinkTable .addIndex("20230911.2", "IDX_EMPi_TGT_MR_SCore") .unique(false) + .online(true) .withColumns("TARGET_TYPE", "MATCH_RESULT", "SCORE"); // Move forced_id constraints to hfj_resource and the new fhir_id column @@ -166,7 +168,11 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { .withColumns("RES_TYPE", "FHIR_ID"); // For resolving references that don't supply the type. - hfjResource.addIndex("20231027.3", "IDX_RES_FHIR_ID").unique(false).withColumns("FHIR_ID"); + hfjResource + .addIndex("20231027.3", "IDX_RES_FHIR_ID") + .unique(false) + .online(true) + .withColumns("FHIR_ID"); Builder.BuilderWithTableName batch2JobInstanceTable = version.onTable("BT2_JOB_INSTANCE"); @@ -177,7 +183,8 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { { version.executeRawSql( "20231212.1", - "CREATE INDEX idx_sp_string_hash_nrm_pattern_ops ON public.hfj_spidx_string USING btree (hash_norm_prefix, sp_value_normalized varchar_pattern_ops, res_id, partition_id)") + "CREATE INDEX CONCURRENTLY idx_sp_string_hash_nrm_pattern_ops ON public.hfj_spidx_string USING btree (hash_norm_prefix, sp_value_normalized varchar_pattern_ops, res_id, partition_id)") + .setTransactional(false) .onlyAppliesToPlatforms(DriverTypeEnum.POSTGRES_9_4) .onlyIf( String.format( @@ -190,7 +197,8 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { "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)") + "CREATE UNIQUE INDEX CONCURRENTLY 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)") + .setTransactional(false) .onlyAppliesToPlatforms(DriverTypeEnum.POSTGRES_9_4) .onlyIf( String.format( 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 39365829c77..387cea1aeb7 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 @@ -614,6 +614,11 @@ public class Builder { myTask.setRunDuringSchemaInitialization(true); return this; } + + public BuilderCompleteTask setTransactional(boolean theFlag) { + myTask.setTransactional(theFlag); + return this; + } } public class BuilderAddTableRawSql {