Skip to content

Commit 0a8fe59

Browse files
committed
Simplify the presentation
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
1 parent bef01d1 commit 0a8fe59

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

app/helpers/pg_hero/home_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def pghero_js_value(value)
1616
json_escape(value.to_json(root: false)).html_safe
1717
end
1818

19+
def pghero_drop_idx_concurrently_explanation
20+
if @database.drop_idx_concurrently_supported?
21+
ret = "<h2>Tip: Use CONCURRENTLY for DROP INDEX</h2>"
22+
ret << "<ul><li>Add <code>disable_ddl_transaction!</code> to your migration</li>"
23+
ret << "<li>Add <code>algorithm: :concurrently</code> to each <code>remove_index</code></li></ul>"
24+
ret.html_safe
25+
end
26+
end
27+
1928
def pghero_remove_index(query)
2029
if query[:columns]
2130
columns = query[:columns].map(&:to_sym)
@@ -24,6 +33,7 @@ def pghero_remove_index(query)
2433
ret = String.new("remove_index #{query[:table].to_sym.inspect}")
2534
ret << ", name: #{(query[:name] || query[:index]).to_s.inspect}"
2635
ret << ", column: #{columns.inspect}" if columns
36+
ret << ", algorithm: concurrently" if @database.drop_idx_concurrently_supported?
2737
ret
2838
end
2939

app/views/pg_hero/home/index.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@
390390

391391
<div id="migration2" style="display: none;">
392392
<pre>rails generate migration remove_unneeded_indexes</pre>
393+
<% if @database.drop_idx_concurrently_supported? %>
394+
<p><%= pghero_drop_idx_concurrently_explanation %></p>
395+
<% end %>
393396
<p>And paste</p>
394397
<pre style="overflow: scroll; white-space: pre; word-break: normal;"><% @duplicate_indexes.each do |query| %>
395398
<%= pghero_remove_index(query[:unneeded_index]) %><% end %></pre>

app/views/pg_hero/home/space.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
<div id="migration" style="display: none;">
3333
<pre>rails generate migration remove_unused_indexes</pre>
34+
<% if @database.drop_idx_concurrently_supported? %>
35+
<p><%= pghero_drop_idx_concurrently_explanation %></p>
36+
<% end %>
3437
<p>And paste</p>
3538
<pre style="overflow: scroll; white-space: pre; word-break: normal;"><% @unused_indexes.sort_by { |q| [-q[:size_bytes], q[:index]] }.each do |query| %>
3639
<%= pghero_remove_index(query) %><% end %></pre>

lib/pghero/methods/basic.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def quote_ident(value)
3434
connection.quote_column_name(value)
3535
end
3636

37+
def drop_idx_concurrently_supported?
38+
server_version_num >= 140000
39+
end
40+
3741
private
3842

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

0 commit comments

Comments
 (0)