Skip to content

Commit

Permalink
Use concurrently for remove_index
Browse files Browse the repository at this point in the history
Present a explanation box recommending to use CONCURRENTLY with DROP
INDEX. In Active Record, this requires two things:
disable_ddl_transaction! once at the top of the migration file, and
"algorithm: :concurrently" option added to every remove_index line

Since this is a sensible default when PostgreSQL 14 or greater is in
use, since it can avoid downtime from an index drop, make it the
default.
Remove the ability to disable it since it seems unnecessary in
retrospect.

Only show the explanation box and only add the option to
remove_index when version 14 or greater is in use
  • Loading branch information
andyatkinson committed Jan 3, 2024
1 parent 8c42de4 commit 67fd867
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/helpers/pg_hero/home_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ def pghero_js_value(value)
json_escape(value.to_json(root: false)).html_safe
end

def pghero_drop_idx_concurrently_explanation
if @database.drop_idx_concurrently_supported?
ret = "<h2>Tip: Use CONCURRENTLY for DROP INDEX</h2>"
ret << "<ul><li>Add <code>disable_ddl_transaction!</code> to your migration</li>"
ret << "<li>Add <code>algorithm: :concurrently</code> to each <code>remove_index</code></li></ul>"
ret.html_safe
end
end

def pghero_remove_index(query)
if query[:columns]
columns = query[:columns].map(&:to_sym)
Expand All @@ -24,6 +33,7 @@ def pghero_remove_index(query)
ret = String.new("remove_index #{query[:table].to_sym.inspect}")
ret << ", name: #{(query[:name] || query[:index]).to_s.inspect}"
ret << ", column: #{columns.inspect}" if columns
ret << ", algorithm: concurrently" if @database.drop_idx_concurrently_supported?
ret
end

Expand Down
4 changes: 4 additions & 0 deletions app/views/pg_hero/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@
<div id="migration2" style="display: none;">
<pre>rails generate migration remove_unneeded_indexes</pre>
<p>And paste</p>
<% if @database.drop_idx_concurrently_supported? %>
<p><%= pghero_drop_idx_concurrently_explanation %></p>
<% end %>
<p>And paste</p>
<pre style="overflow: scroll; white-space: pre; word-break: normal;"><% @duplicate_indexes.each do |query| %>
<%= pghero_remove_index(query[:unneeded_index]) %><% end %></pre>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/views/pg_hero/home/space.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

<div id="migration" style="display: none;">
<pre>rails generate migration remove_unused_indexes</pre>
<% if @database.drop_idx_concurrently_supported? %>
<p><%= pghero_drop_idx_concurrently_explanation %></p>
<% end %>
<p>And paste</p>
<pre style="overflow: scroll; white-space: pre; word-break: normal;"><% @unused_indexes.sort_by { |q| [-q[:size_bytes], q[:index]] }.each do |query| %>
<%= pghero_remove_index(query) %><% end %></pre>
Expand Down
4 changes: 4 additions & 0 deletions lib/pghero/methods/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def quote_ident(value)
connection.quote_column_name(value)
end

def drop_idx_concurrently_supported?
server_version_num >= 140000
end

private

def select_all(sql, conn: nil, query_columns: [])
Expand Down

0 comments on commit 67fd867

Please sign in to comment.