Skip to content

Support multiple databases for partitioning

What does this MR do?

Our partition management code that creates/detaches partitions needs to be updated to support multiple databases. The previous approach in !67904 (closed) repurposed the Rails' connects_to API, but turned out to be impractical for now as it creates new connections separate from calling code.

This MR takes a new approach allows the shared models under lib/ to have their connection overridden with an external connection. From "shared" features that need to execute across multiple databases, we can use the properly configured connection from each application model and pass that into the shared model. This allows reuse of existing connections, keeping connection count low and increasing the ease to manage consistent views in transactional contexts.

As a follow-up MR, we'll also have to update the DetachedPartitionDropper to support multiple databases.

Related to #336898 (closed)

Screenshots or Screencasts (strongly suggested)

How to setup and validate locally (strongly suggested)

  1. Delete partitions for a currently partitioned table like audit_events from the rails console
    Gitlab::Database::PostgresPartitionedTable.by_identifier('public.audit_events').postgres_partitions.each { |partition| ActiveRecord::Base.connection.execute("drop table #{partition.identifier}") }
  2. Verify the partitions are gone in postgres:
    \d+ audit_events
    -- at the bottom of the output, look for
    Number of partitions: 0
  3. Run the rake task to create partitions
    rails gitlab:db:create_dynamic_partitions
  4. Verify the partitions are created in postgres:
    \d+ audit_events
    -- at the bottom of the output, look for
    Partitions: gitlab_partitions_dynamic.audit_events_000000 FOR VALUES FROM (MINVALUE) TO ('2021-08-01 00:00:00'),
                gitlab_partitions_dynamic.audit_events_202108 FOR VALUES FROM ('2021-08-01 00:00:00') TO ('2021-09-01 00:00:00'),
                gitlab_partitions_dynamic.audit_events_202109 FOR VALUES FROM ('2021-09-01 00:00:00') TO ('2021-10-01 00:00:00'),
                gitlab_partitions_dynamic.audit_events_202110 FOR VALUES FROM ('2021-10-01 00:00:00') TO ('2021-11-01 00:00:00'),
                gitlab_partitions_dynamic.audit_events_202111 FOR VALUES FROM ('2021-11-01 00:00:00') TO ('2021-12-01 00:00:00'),
                gitlab_partitions_dynamic.audit_events_202112 FOR VALUES FROM ('2021-12-01 00:00:00') TO ('2022-01-01 00:00:00'),
                gitlab_partitions_dynamic.audit_events_202201 FOR VALUES FROM ('2022-01-01 00:00:00') TO ('2022-02-01 00:00:00'),
                gitlab_partitions_dynamic.audit_events_202202 FOR VALUES FROM ('2022-02-01 00:00:00') TO ('2022-03-01 00:00:00')

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Edited by Patrick Bair

Merge request reports