discourse/db/post_migrate
Alan Guo Xiang Tan 8ee71d439b
FIX: Add migration to reindex invalid indexes (#19858)
In Discourse, there are many migration files where we CREATE INDEX CONCURRENTLY which requires us to set disable_ddl_transaction!. Setting disable_ddl_transaction! in a migration file runs the SQL statements outside of a transaction. The implication of this is that there is no ROLLBACK should any of the SQL statements fail.

We have seen lock timeouts occuring when running CREATE INDEX CONCURRENTLY. When that happens, the index would still have been created but marked as invalid by Postgres.

Per the postgres documentation:

> If a problem arises while scanning the table, such as a deadlock or a uniqueness violation in a unique index, the CREATE INDEX command will fail but leave behind an “invalid” index. This index will be ignored for querying purposes because it might be incomplete; however it will still consume update overhead.
> The recommended recovery method in such cases is to drop the index and try again to perform CREATE INDEX CONCURRENTLY . (Another possibility is to rebuild the index with REINDEX INDEX CONCURRENTLY ).

When such scenarios happen, we are supposed to either drop and create the index again or run a REINDEX operation. However, I noticed today that we have not been doing so in Discourse. Instead, we’ve been incorrectly working around the problem by checking for the index existence before creating the index in order to make the migration idempotent. What this potentially mean is that we might have invalid indexes which are lying around in the database which PG will ignore for querying purposes.

This commits adds a migration which queries for all the
invalid indexes in the `public` namespace and reindexes them.
2023-01-13 11:04:26 +08:00
..
20220202223955_migrate_selectable_avatars_enabled.rb FEATURE: upload an avatar option for uploading avatars with selectable avatars (#15878) 2022-02-24 12:57:39 -08:00
20220214224506_reset_custom_emoji_post_bakes_version_secure_fix.rb DEV: Apply syntax_tree formatting to `db/*` 2023-01-09 11:59:41 +00:00
20220215015538_drop_user_stat_count_constraints.rb DEV: Drop user_stats count column constraints (#15949) 2022-02-16 12:49:11 +11:00
20220220234155_conform_bounce_error_code.rb DEV: Fix SMTP bounce regexp (#16019) 2022-02-22 08:54:01 +10:00
20220302171443_rebake_old_avatar_service_urls.rb DEV: Apply syntax_tree formatting to `db/*` 2023-01-09 11:59:41 +00:00
20220309132720_copy_post_uploads_to_upload_references_for_sync.rb FEATURE: Create upload_references table (#16146) 2022-06-09 09:24:30 +10:00
20220325064954_make_some_bookmark_columns_nullable.rb DEV: Apply syntax_tree formatting to `db/*` 2023-01-09 11:59:41 +00:00
20220401140745_drop_category_required_tag_group_columns.rb DEV: Apply syntax_tree formatting to `db/*` 2023-01-09 11:59:41 +00:00
20220429164301_delete_hotlinked_image_custom_fields.rb DEV: Drop old hotlinked image data from post_custom_fields (#16594) 2022-05-12 15:34:35 +01:00
20220512011531_backfill_polymorphic_bookmarks.rb FEATURE: Promote polymorphic bookmarks to default and migrate (#16729) 2022-05-23 10:07:15 +10:00
20220617151846_rename_default_categories_regular_setting.rb UX: Change wording for 'regular' categories to 'normal' (#17134) 2022-06-20 06:49:33 +03:00
20220621164914_drop_flair_url_from_groups.rb DEV: Apply syntax_tree formatting to `db/*` 2023-01-09 11:59:41 +00:00
20220825005115_backfill_outbound_message_id.rb FEATURE: Overhaul email threading (#17996) 2022-09-26 09:14:24 +10:00
20221026035440_security_log_out_invite_redemption_invited_users.rb DEV: Apply syntax_tree formatting to `db/*` 2023-01-09 11:59:41 +00:00
20221108032233_drop_old_bookmark_columns_v2.rb DEV: Apply syntax_tree formatting to `db/*` 2023-01-09 11:59:41 +00:00
20221212234948_drop_topic_allowed_users_backup_nov_2022.rb DEV: Delete tmp table from migration for invites (#19431) 2022-12-13 10:17:25 +10:00
20230113002617_reindex_invalid_indexes.rb FIX: Add migration to reindex invalid indexes (#19858) 2023-01-13 11:04:26 +08:00