Skip to content

Commit

Permalink
Change materialized views to be refreshed concurrently to avoid locks (
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored Jan 30, 2024
1 parent c4af668 commit fa0ba67
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/models/account_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class AccountSummary < ApplicationRecord
scope :filtered, -> { where.missing(:follow_recommendation_suppressions) }

def self.refresh
Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false)
rescue ActiveRecord::StatementInvalid
Scenic.database.refresh_materialized_view(table_name, concurrently: false, cascade: false)
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/follow_recommendation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class FollowRecommendation < ApplicationRecord
scope :localized, ->(locale) { joins(:account_summary).merge(AccountSummary.localized(locale)) }

def self.refresh
Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false)
rescue ActiveRecord::StatementInvalid
Scenic.database.refresh_materialized_view(table_name, concurrently: false, cascade: false)
end

Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20210322164601_create_account_summaries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class CreateAccountSummaries < ActiveRecord::Migration[5.2]
def change
create_view :account_summaries, materialized: { no_data: true }
create_view :account_summaries, materialized: true

# To be able to refresh the view concurrently,
# at least one unique index is required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class UpdateFollowRecommendationsToVersion2 < ActiveRecord::Migration[6.1]

def up
drop_view :follow_recommendations
create_view :follow_recommendations, version: 2, materialized: { no_data: true }
create_view :follow_recommendations, version: 2, materialized: true

# To be able to refresh the view concurrently,
# at least one unique index is required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ class UpdateAccountSummariesToVersion2 < ActiveRecord::Migration[6.1]
def up
reapplication_follow_recommendations_v2 do
drop_view :account_summaries, materialized: true
create_view :account_summaries, version: 2, materialized: { no_data: true }
create_view :account_summaries, version: 2, materialized: true
safety_assured { add_index :account_summaries, :account_id, unique: true }
end
end

def down
reapplication_follow_recommendations_v2 do
drop_view :account_summaries, materialized: true
create_view :account_summaries, version: 1, materialized: { no_data: true }
create_view :account_summaries, version: 1, materialized: true
safety_assured { add_index :account_summaries, :account_id, unique: true }
end
end

def reapplication_follow_recommendations_v2
drop_view :follow_recommendations, materialized: true
yield
create_view :follow_recommendations, version: 2, materialized: { no_data: true }
create_view :follow_recommendations, version: 2, materialized: true
safety_assured { add_index :follow_recommendations, :account_id, unique: true }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class CreateGlobalFollowRecommendations < ActiveRecord::Migration[7.0]
def change
create_view :global_follow_recommendations, materialized: { no_data: true }
create_view :global_follow_recommendations, materialized: true
safety_assured { add_index :global_follow_recommendations, :account_id, unique: true }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def up
end

def down
create_view :follow_recommendations, version: 2, materialized: { no_data: true }
create_view :follow_recommendations, version: 2, materialized: true
safety_assured { add_index :follow_recommendations, :account_id, unique: true }
end
end

0 comments on commit fa0ba67

Please sign in to comment.