Skip to content

Add view-backed db model for postgres partitions

Patrick Bair requested to merge 241267-add-postgres-partition-model into master

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.

Migration up

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) =======================

Migration down

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

Availability and Testing

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
Edited by Patrick Bair

Merge request reports