Commit 38758bb9 authored by Andreas Brandl's avatar Andreas Brandl

Do not reindex exclusion constraint indexes

Postgres exclusion constraints are based on indexes. Recreating those
will interfere with constraint checking.

For now, we only have one of this type.
parent 4fcec8a4
......@@ -14,6 +14,7 @@ class AddPostgresIndexView < ActiveRecord::Migration[6.0]
pg_index.indisunique as unique,
pg_index.indisvalid as valid_index,
pg_class.relispartition as partitioned,
pg_index.indisexclusion as exclusion,
pg_indexes.indexdef as definition,
pg_relation_size(pg_class.oid) as ondisk_size_bytes
FROM pg_index
......
......@@ -14419,6 +14419,7 @@ CREATE VIEW postgres_indexes AS
pg_index.indisunique AS "unique",
pg_index.indisvalid AS valid_index,
pg_class.relispartition AS partitioned,
pg_index.indisexclusion AS exclusion,
pg_indexes.indexdef AS definition,
pg_relation_size((pg_class.oid)::regclass) AS ondisk_size_bytes
FROM (((pg_index
......
......@@ -12,8 +12,10 @@ module Gitlab
find(identifier)
end
scope :non_unique, -> { where(unique: false) }
scope :non_partitioned, -> { where(partitioned: false) }
# A 'regular' index is a non-unique index,
# that does not serve an exclusion constraint and
# is defined on a table that is not partitioned.
scope :regular, -> { where(unique: false, partitioned: false, exclusion: false)}
scope :random_few, ->(how_many) do
limit(how_many).order(Arel.sql('RANDOM()'))
......
......@@ -23,6 +23,7 @@ module Gitlab
def perform
raise ReindexError, 'UNIQUE indexes are currently not supported' if index.unique?
raise ReindexError, 'partitioned indexes are currently not supported' if index.partitioned?
raise ReindexError, 'indexes serving an exclusion constraint are currently not supported' if index.exclusion?
logger.info "Starting reindex of #{index}"
......
......@@ -177,7 +177,7 @@ namespace :gitlab do
indexes = if args[:index_name]
Gitlab::Database::PostgresIndex.by_identifier(args[:index_name])
else
Gitlab::Database::PostgresIndex.non_unique.non_partitioned.random_few(2)
Gitlab::Database::PostgresIndex.regular.random_few(2)
end
Gitlab::Database::Reindexing.perform(indexes)
......
......@@ -30,15 +30,17 @@ RSpec.describe Gitlab::Database::PostgresIndex do
end
end
describe '#non_unique' do
describe '#regular' do
it 'only non-unique indexes' do
expect(described_class.non_unique).to all(have_attributes(unique: false))
expect(described_class.regular).to all(have_attributes(unique: false))
end
end
describe '#non_partitioned' do
it 'only non partitioned indexes ' do
expect(described_class.non_partitioned).to all(have_attributes(partitioned: false))
expect(described_class.regular).to all(have_attributes(partitioned: false))
end
it 'only indexes that dont serve an exclusion constraint' do
expect(described_class.regular).to all(have_attributes(exclusion: false))
end
end
......
......@@ -8,7 +8,7 @@ RSpec.describe Gitlab::Database::Reindexing::ConcurrentReindex, '#perform' do
let(:table_name) { '_test_reindex_table' }
let(:column_name) { '_test_column' }
let(:index_name) { '_test_reindex_index' }
let(:index) { instance_double(Gitlab::Database::PostgresIndex, indexrelid: 42, name: index_name, schema: 'public', partitioned?: false, unique?: false, definition: 'CREATE INDEX _test_reindex_index ON public._test_reindex_table USING btree (_test_column)') }
let(:index) { instance_double(Gitlab::Database::PostgresIndex, indexrelid: 42, name: index_name, schema: 'public', partitioned?: false, unique?: false, exclusion?: false, definition: 'CREATE INDEX _test_reindex_index ON public._test_reindex_table USING btree (_test_column)') }
let(:logger) { double('logger', debug: nil, info: nil, error: nil ) }
let(:connection) { ActiveRecord::Base.connection }
......@@ -46,6 +46,18 @@ RSpec.describe Gitlab::Database::Reindexing::ConcurrentReindex, '#perform' do
end
end
context 'when the index serves an exclusion constraint' do
before do
allow(index).to receive(:exclusion?).and_return(true)
end
it 'raises an error' do
expect do
subject.perform
end.to raise_error(described_class::ReindexError, /indexes serving an exclusion constraint are currently not supported/)
end
end
context 'replacing the original index with a rebuilt copy' do
let(:replacement_name) { 'tmp_reindex_42' }
let(:replaced_name) { 'old_reindex_42' }
......
......@@ -170,7 +170,7 @@ RSpec.describe 'gitlab:db namespace rake task' do
context 'when no index_name is given' do
it 'rebuilds a random number of large indexes' do
expect(Gitlab::Database::PostgresIndex).to receive_message_chain('non_unique.non_partitioned.random_few').and_return(indexes)
expect(Gitlab::Database::PostgresIndex).to receive_message_chain('regular.random_few').and_return(indexes)
expect(Gitlab::Database::Reindexing).to receive(:perform).with(indexes)
run_rake_task('gitlab:db:reindex')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment