diff --git a/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb b/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb
index fa58e349a30e8e48e8613b6e7e6db5975bb32da4..9a88eb8ea06742feb5f0c37833d47cf9860728d5 100644
--- a/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb
+++ b/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb
@@ -18,7 +18,7 @@ class ContainerExpirationPolicy < ActiveRecord::Base
       def perform(from_id, to_id)
         ContainerExpirationPolicy.where(enabled: true, project_id: from_id..to_id).each_batch(of: BATCH_SIZE) do |batch|
           sql = <<-SQL
-            WITH batched_relation AS MATERIALIZED (#{batch.select(:project_id).limit(BATCH_SIZE).to_sql})
+            WITH batched_relation AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} (#{batch.select(:project_id).limit(BATCH_SIZE).to_sql})
             UPDATE container_expiration_policies
             SET enabled = FALSE
             FROM batched_relation