Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For PostgreSQL 14+, provide DROP INDEX migration lines that use CONCURRENTLY #437

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Perform DROP INDEX operations using CONCURRENTLY</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
3 changes: 3 additions & 0 deletions app/views/pg_hero/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@

<div id="migration2" style="display: none;">
<pre>rails generate migration remove_unneeded_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;"><% @duplicate_indexes.each do |query| %>
<%= pghero_remove_index(query[:unneeded_index]) %><% end %></pre>
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