Commit 1227ce6d authored by Stan Hu's avatar Stan Hu

Geo: Exclude tables that start with pg_ from FDW check

PostgreSQL-specific tables, such as `pg_stat_statements` and `pg_buffercache`,
aren't necessary for our FDW check to work and cause unnecessary confusion.
parent 7701c4ba
---
title: 'Geo: Exclude tables that start with pg_ from FDW check'
merge_request:
author:
type: fixed
...@@ -61,6 +61,7 @@ module Gitlab ...@@ -61,6 +61,7 @@ module Gitlab
FROM information_schema.tables FROM information_schema.tables
WHERE table_schema = '#{FDW_SCHEMA}' WHERE table_schema = '#{FDW_SCHEMA}'
AND table_type = 'FOREIGN TABLE' AND table_type = 'FOREIGN TABLE'
AND table_name NOT LIKE 'pg_%'
SQL SQL
::Geo::TrackingBase.connection.execute(sql).first.fetch('count').to_i ::Geo::TrackingBase.connection.execute(sql).first.fetch('count').to_i
...@@ -72,18 +73,26 @@ module Gitlab ...@@ -72,18 +73,26 @@ module Gitlab
# @return [Boolean] whether schemas match and are not empty # @return [Boolean] whether schemas match and are not empty
def self.foreign_schema_tables_match? def self.foreign_schema_tables_match?
Gitlab::Geo.cache_value(:geo_fdw_schema_tables_match) do Gitlab::Geo.cache_value(:geo_fdw_schema_tables_match) do
secondary = retrieve_schema_tables(ActiveRecord::Base, ActiveRecord::Base.connection_config[:database], DEFAULT_SCHEMA) schema = gitlab_schema
fdw = retrieve_schema_tables(::Geo::TrackingBase, Rails.configuration.geo_database['database'], FDW_SCHEMA)
s = secondary.to_a schema.present? && schema == fdw_schema
f = fdw.to_a
s.present? && s == f
end end
end end
def self.count_tables_match? def self.count_tables_match?
ActiveRecord::Schema.tables.count == count_tables gitlab_tables.count == count_tables
end
def self.gitlab_tables
ActiveRecord::Schema.tables.reject { |table| table.start_with?('pg_') }
end
def self.gitlab_schema
retrieve_schema_tables(ActiveRecord::Base, ActiveRecord::Base.connection_config[:database], DEFAULT_SCHEMA).to_a
end
def self.fdw_schema
retrieve_schema_tables(::Geo::TrackingBase, Rails.configuration.geo_database['database'], FDW_SCHEMA).to_a
end end
def self.retrieve_schema_tables(adapter, database, schema) def self.retrieve_schema_tables(adapter, database, schema)
...@@ -92,10 +101,11 @@ module Gitlab ...@@ -92,10 +101,11 @@ module Gitlab
FROM information_schema.columns FROM information_schema.columns
WHERE table_catalog = '#{database}' WHERE table_catalog = '#{database}'
AND table_schema = '#{schema}' AND table_schema = '#{schema}'
AND table_name NOT LIKE 'pg_%'
ORDER BY table_name, column_name, data_type ORDER BY table_name, column_name, data_type
SQL SQL
adapter.connection.execute(sql) adapter.connection.select_all(sql)
end end
private_class_method :retrieve_schema_tables private_class_method :retrieve_schema_tables
end end
......
...@@ -21,7 +21,7 @@ module Gitlab ...@@ -21,7 +21,7 @@ module Gitlab
unless Gitlab::Geo::Fdw.fdw_up_to_date? unless Gitlab::Geo::Fdw.fdw_up_to_date?
return "The Geo database has an outdated FDW remote schema.".tap do |output| return "The Geo database has an outdated FDW remote schema.".tap do |output|
output << " It contains #{Gitlab::Geo::Fdw.count_tables} of #{ActiveRecord::Schema.tables.count} expected tables." unless Gitlab::Geo::Fdw.count_tables_match? output << " It contains #{Gitlab::Geo::Fdw.count_tables} of #{Gitlab::Geo::Fdw.gitlab_tables.count} expected tables." unless Gitlab::Geo::Fdw.count_tables_match?
end end
end end
......
...@@ -36,6 +36,18 @@ describe Gitlab::Geo::Fdw, :geo do ...@@ -36,6 +36,18 @@ describe Gitlab::Geo::Fdw, :geo do
end end
end end
describe '.gitlab_tables' do
it 'excludes pg_ tables' do
tables = described_class.gitlab_tables
ActiveRecord::Base.connection.create_table(:pg_gitlab_test)
expect(described_class.gitlab_tables).to eq(tables)
ActiveRecord::Base.connection.drop_table(:pg_gitlab_test)
end
end
describe 'fdw_capable?' do describe 'fdw_capable?' do
context 'with mocked FDW environment' do context 'with mocked FDW environment' do
it 'returns true when PostgreSQL FDW is enabled' do it 'returns true when PostgreSQL FDW is enabled' do
...@@ -70,6 +82,20 @@ describe Gitlab::Geo::Fdw, :geo do ...@@ -70,6 +82,20 @@ describe Gitlab::Geo::Fdw, :geo do
it 'returns true' do it 'returns true' do
expect(described_class.fdw_capable?).to be_truthy expect(described_class.fdw_capable?).to be_truthy
end end
context 'with a pg_ table' do
before do
ActiveRecord::Base.connection.create_table(:pg_gitlab_test)
end
after do
ActiveRecord::Base.connection.drop_table(:pg_gitlab_test)
end
it 'returns true' do
expect(described_class.fdw_capable?).to be_truthy
end
end
end end
end end
......
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