Skip to content

Fix a join in the postgres_indexes view

Patrick Bair requested to merge pb-fix-postgres-indexes-duplicates into master

What does this MR do and why?

Fix a join in the postgres_indexes view that didn't join index information properly when multiple indexes with the same name existed in different schemas. This could result in queries returning multiple rows for a single index, with potentially different index definitions.

How to set up and validate locally

  1. Before running the migration from this MR, setup the database:
    create table main_schema_table (id serial not null primary key);
    create table gitlab_partitions_dynamic.gpd_schema_table (id serial not null primary key);
    create index testi1 on main_schema_table (id);
    create index testi1 on gitlab_partitions_dynamic.gpd_schema_table (id);
  2. Verify that we see extra entries for each index:
    select schema, tablename, name, definition from postgres_indexes where name = 'testi1';
              schema           |     tablename     |  name  |                                     definition
    ---------------------------+-------------------+--------+------------------------------------------------------------------------------------
     public                    | main_schema_table | testi1 | CREATE INDEX testi1 ON public.main_schema_table USING btree (id)
     public                    | gpd_schema_table  | testi1 | CREATE INDEX testi1 ON gitlab_partitions_dynamic.gpd_schema_table USING btree (id)
     gitlab_partitions_dynamic | main_schema_table | testi1 | CREATE INDEX testi1 ON public.main_schema_table USING btree (id)
     gitlab_partitions_dynamic | gpd_schema_table  | testi1 | CREATE INDEX testi1 ON gitlab_partitions_dynamic.gpd_schema_table USING btree (id)
    (4 rows)
  3. Run the migration
    rails db:migrate:up VERSION=20211018161447
  4. Verify that we only see a single entry per index per schema:
    select schema, tablename, name, definition from postgres_indexes where name = 'testi1';
              schema           |     tablename     |  name  |                                     definition
    ---------------------------+-------------------+--------+------------------------------------------------------------------------------------
     public                    | main_schema_table | testi1 | CREATE INDEX testi1 ON public.main_schema_table USING btree (id)
     gitlab_partitions_dynamic | gpd_schema_table  | testi1 | CREATE INDEX testi1 ON gitlab_partitions_dynamic.gpd_schema_table USING btree (id)
    (2 rows)

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Patrick Bair

Merge request reports

Loading