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)
- Delete partitions for a currently partitioned table like
audit_events
from the rails consoleGitlab::Database::PostgresPartitionedTable.by_identifier('public.audit_events').postgres_partitions.each { |partition| ActiveRecord::Base.connection.execute("drop table #{partition.identifier}") }
- Verify the partitions are gone in postgres:
\d+ audit_events -- at the bottom of the output, look for Number of partitions: 0
- Run the rake task to create partitions
rails gitlab:db:create_dynamic_partitions
- 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
-
I have included changelog trailers, or none are needed. (Does this MR need a changelog?) -
I have added/updated documentation, or it's not needed. (Is documentation required?) -
I have properly separated EE content from FOSS, or this MR is FOSS only. (Where should EE code go?) -
I have added information for database reviewers in the MR description, or it's not needed. (Does this MR have database related changes?) -
I have self-reviewed this MR per code review guidelines. -
This MR does not harm performance, or I have asked a reviewer to help assess the performance impact. (Merge request performance guidelines) -
I have followed the style guides. -
This change is backwards compatible across updates, or this does not apply.
Availability and Testing
-
I have added/updated tests following the Testing Guide, or it's not needed. (Consider all test levels. See the Test Planning Process.) -
I have tested this MR in all supported browsers, or it's not needed. -
I have informed the Infrastructure department of a default or new setting change per definition of done, or it's not needed.