Skip to content

Database vacuum signals don't work with partitioned tables

Problem

The mechanism that we use to defer background migrations and sidekiq jobs based on database health stats doesn't work with partitioned when checking for vacuum because we check for vacuum on the table name and the vacuum for partitioned tables is actually running on the partitions.

Proposal

diff --git i/lib/gitlab/database/postgres_autovacuum_activity.rb w/lib/gitlab/database/postgres_autovacuum_activity.rb
index 226af4908996..a019c7d212c3 100644
--- i/lib/gitlab/database/postgres_autovacuum_activity.rb
+++ w/lib/gitlab/database/postgres_autovacuum_activity.rb
@@ -9,13 +9,22 @@ class PostgresAutovacuumActivity < SharedModel
       scope :wraparound_prevention, -> { where(wraparound_prevention: true) }
 
       def self.for_tables(tables)
+        partitioned_tables, regular_tables = tables.partition do |table_name|
+          Gitlab::Database::PostgresPartitionedTable.find_by_name_in_current_schema(table_name).present?
+        end
+
+        partitions = PostgresPartition.with_parent_tables(partitioned_tables).pluck(:schema, :name)
+
         Gitlab::Database::LoadBalancing::SessionMap
           .current(load_balancer)
           .use_primary do
           # calling `.to_a` here to execute the query in the primary's scope
           # and to avoid having the scope chained and re-executed
           #
-          where('schema = current_schema()').where(table: tables).to_a
+          scope = where('schema = current_schema()').where(table: regular_tables)
+          partitions
+            .reduce(scope) { |scope, (schema, name)| scope.or(where(schema: schema, name: name)) }
+            .to_a
         end
       end
 
Edited by 🤖 GitLab Bot 🤖