Allow LooseForeignKeys to use partition pruning
Problem
LooseForeignKeys
cleanup worker scans the entire table to find records to delete/nullify and that is a problem for partitioned tables because it needs to access all partitions at the same time for a query.
This is the top source for unpartitioned queries for ci_builds
: LooseForeignKeys::CleanupWorker - 20906
, https://gitlab.slack.com/archives/C02P0FZLQMD/p1709317509262709
Proposal
The worker creates a dynamic model for each table that it processes. We could use Gitlab::Database::PostgresPartitionedTable.each_partition(:p_ci_builds)
create a model for each partition or include the partition.condition
on the queries and iterate the same query through all partitions.