From 19d2d55011f5808bcbc177119765557a2900dea1 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 3 May 2022 15:47:58 +0100 Subject: [PATCH] FIX: Ensure post_hotlinked_media index does not exceed size limit (#16609) On some installations, this would fail with 'index row size exceeds btree version 4 maximum'. This commit replaces the (post_id, url)` index with a `(post_id, md5(url))` index, which is much more space efficient. --- app/models/post_hotlinked_media.rb | 2 +- ...20428094026_create_post_hotlinked_media.rb | 8 ++++++- ...28094027_fix_post_hotlinked_media_index.rb | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20220428094027_fix_post_hotlinked_media_index.rb diff --git a/app/models/post_hotlinked_media.rb b/app/models/post_hotlinked_media.rb index 6ff1f921736..1bf5d14e9e2 100644 --- a/app/models/post_hotlinked_media.rb +++ b/app/models/post_hotlinked_media.rb @@ -25,5 +25,5 @@ end # # Indexes # -# index_post_hotlinked_media_on_post_id_and_url (post_id,url) UNIQUE +# index_post_hotlinked_media_on_post_id_and_url_md5 (post_id, md5((url)::text)) UNIQUE # diff --git a/db/migrate/20220428094026_create_post_hotlinked_media.rb b/db/migrate/20220428094026_create_post_hotlinked_media.rb index 847f9990173..cc868310451 100644 --- a/db/migrate/20220428094026_create_post_hotlinked_media.rb +++ b/db/migrate/20220428094026_create_post_hotlinked_media.rb @@ -22,9 +22,15 @@ class CreatePostHotlinkedMedia < ActiveRecord::Migration[6.1] t.bigint :upload_id t.timestamps - t.index [:post_id, :url], unique: true + # Failed on some installations due to index size. Repaired in 20220428094027 + # t.index [:post_id, :url], unique: true end + execute <<~SQL + CREATE UNIQUE INDEX index_post_hotlinked_media_on_post_id_and_url_md5 + ON post_hotlinked_media (post_id, md5(url)); + SQL + reversible do |dir| dir.up do execute <<~SQL diff --git a/db/migrate/20220428094027_fix_post_hotlinked_media_index.rb b/db/migrate/20220428094027_fix_post_hotlinked_media_index.rb new file mode 100644 index 00000000000..28eb5af680c --- /dev/null +++ b/db/migrate/20220428094027_fix_post_hotlinked_media_index.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class FixPostHotlinkedMediaIndex < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + def up + execute <<~SQL + CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS index_post_hotlinked_media_on_post_id_and_url_md5 + ON post_hotlinked_media (post_id, md5(url)); + SQL + + # Failed index introduced in 20220428094026_create_post_hotlinked_media. On some installations it succeeded, + # so we need to clean it up. + execute <<~SQL + DROP INDEX CONCURRENTLY IF EXISTS index_post_hotlinked_media_on_post_id_and_url; + SQL + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end