Add view-backed db model for postgres partitions
What does this MR do?
Related to #241267 (closed)
Various pieces of the code that deal with partitioning have to query the state of the partitioned tables and their partitions. Over time, this runs the risk of repeating the same (or very similar) queries over and over. Not only is this repetitive, but the postgres system catalogs can be difficult to decipher, so it's best to abstract them with something more easily understood.
This MR is a continuation of work started here: !45591 (merged), and introduces an AR model backed by a database view that provides an easy way to query the Postgres system catalogs to find partitions of a table.
up
Migration rails db:migrate:up VERSION=20201019172704
== 20201019172704 AddPartitionsView: migrating ================================
-- execute("CREATE OR REPLACE VIEW postgres_partitions AS\nSELECT\n pg_namespace.nspname::text || '.'::text || pg_class.relname::text AS identifier,\n pg_class.oid AS oid,\n pg_namespace.nspname AS schema,\n pg_class.relname AS name,\n parent_namespace.nspname::text || '.'::text || parent_class.relname::text AS parent_identifier,\n pg_get_expr(pg_class.relpartbound, pg_inherits.inhrelid) AS condition\nFROM pg_class\nINNER JOIN pg_namespace\nON pg_namespace.oid = pg_class.relnamespace\nINNER JOIN pg_inherits\nON pg_class.oid = pg_inherits.inhrelid\nINNER JOIN pg_class parent_class\nON pg_inherits.inhparent = parent_class.oid\nINNER JOIN pg_namespace parent_namespace\nON parent_class.relnamespace = parent_namespace.oid\nWHERE pg_class.relispartition\nAND pg_namespace.nspname IN (\n current_schema(),\n 'gitlab_partitions_dynamic',\n 'gitlab_partitions_static'\n)\n")
-> 0.0036s
== 20201019172704 AddPartitionsView: migrated (0.0036s) =======================
down
Migration rails db:migrate:down VERSION=20201019172704
== 20201019172704 AddPartitionsView: reverting ================================
-- execute("DROP VIEW IF EXISTS postgres_partitions\n")
-> 0.0014s
== 20201019172704 AddPartitionsView: reverted (0.0014s) =======================
Does this MR meet the acceptance criteria?
Conformity
-
Changelog entry -
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
-
Label as security and @ mention @gitlab-com/gl-security/appsec
-
The MR includes necessary changes to maintain consistency between UI, API, email, or other methods -
Security reports checked/validated by a reviewer from the AppSec team