Skip to content

Add partitioned table model

Patrick Bair requested to merge 241267-add-partitioned-table-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 follows a pattern first discussed and introduced here: !42967 (merged), and introduces an AR model backed by a database view that provides an easy way to query the Postgres system catalogs to find information about partitioned tables.

A second MR !45592 (merged) expands on this work to add a view/model that can be used to query the table's partitions.

Migration up

rails db:migrate:up VERSION=20201019161924
== 20201019161924 AddPartitionedTableView: migrating ==========================
-- execute("CREATE OR REPLACE VIEW postgres_partitioned_tables 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  CASE partitioned_tables.partstrat\n  WHEN 'l' THEN 'list'\n  WHEN 'r' THEN 'range'\n  WHEN 'h' THEN 'hash'\n  END as strategy,\n  array_agg(pg_attribute.attname) as key_columns\n  FROM (\n    SELECT\n      partrelid,\n      partstrat,\n      unnest(partattrs) as column_position\n    FROM pg_partitioned_table\n  ) partitioned_tables\n  INNER JOIN pg_class\n  ON partitioned_tables.partrelid = pg_class.oid\n  INNER JOIN pg_namespace\n  ON pg_class.relnamespace = pg_namespace.oid\n  INNER JOIN pg_attribute\n  ON pg_attribute.attrelid = pg_class.oid\n  AND pg_attribute.attnum = partitioned_tables.column_position\n  WHERE pg_namespace.nspname = current_schema()\n  GROUP BY identifier, pg_class.oid, schema, name, strategy;\n")
   -> 0.0026s
== 20201019161924 AddPartitionedTableView: migrated (0.0027s) =================

Migration down

rails db:migrate:down VERSION=20201019161924
== 20201019161924 AddPartitionedTableView: reverting ==========================
-- execute("DROP VIEW IF EXISTS postgres_partitioned_tables\n")
   -> 0.0016s
== 20201019161924 AddPartitionedTableView: reverted (0.0017s) =================

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