NikolayS / postgres_dba

The missing set of useful tools for Postgres DBAs and all engineers
BSD 3-Clause "New" or "Revised" License
1.03k stars 113 forks source link

Fix some wrong detect redundant indexes when the table have more than 10 columns #50

Closed v0112358 closed 2 years ago

v0112358 commented 3 years ago

I have 2 tables with schema:

                                     Table "public.demo_line_items_9_columns"
    Column     |           Type           | Collation | Nullable | Default | Storage  | Stats target | Description
---------------+--------------------------+-----------+----------+---------+----------+--------------+-------------
 box_line_id   | uuid                     |           | not null |         | plain    |              |
 qty_delivered | integer                  |           |          |         | plain    |              |
 qty_returned  | integer                  |           |          |         | plain    |              |
 created_by    | text                     |           |          |         | extended |              |
 created_at    | timestamp with time zone |           |          |         | plain    |              |
 updated_by    | text                     |           |          |         | extended |              |
 updated_at    | timestamp with time zone |           |          |         | plain    |              |
 runsheet_id   | uuid                     |           | not null |         | plain    |              |
 box_id        | uuid                     |           | not null |         | plain    |              |
Indexes:
    "demo_line_items_9_columns_tmp_pk" PRIMARY KEY, btree (box_id, runsheet_id, box_line_id)
    "demo_line_items_9_columns_box_line_id_idx" btree (box_line_id)
    "demo_line_items_9_columns_runsheet_id_idx" btree (runsheet_id)
                                     Table "public.demo_line_items_16_columns"
    Column     |           Type           | Collation | Nullable | Default | Storage  | Stats target | Description
---------------+--------------------------+-----------+----------+---------+----------+--------------+-------------
 box_line_id   | uuid                     |           | not null |         | plain    |              |
 product_id    | integer                  |           |          |         | plain    |              |
 product_name  | text                     |           |          |         | extended |              |
 product_sku   | text                     |           |          |         | extended |              |
 qty           | integer                  |           |          |         | plain    |              |
 cod           | numeric(12,2)            |           |          |         | main     |              |
 qty_delivered | integer                  |           |          |         | plain    |              |
 qty_returned  | integer                  |           |          |         | plain    |              |
 created_by    | text                     |           |          |         | extended |              |
 created_at    | timestamp with time zone |           |          |         | plain    |              |
 updated_by    | text                     |           |          |         | extended |              |
 updated_at    | timestamp with time zone |           |          |         | plain    |              |
 runsheet_id   | uuid                     |           | not null |         | plain    |              |
 box_id        | uuid                     |           | not null |         | plain    |              |
 qty_reversed  | integer                  |           |          |         | plain    |              |
 status        | boolean                  |           |          | false   | plain    |              |
Indexes:
    "runsheet_line_items_pk" PRIMARY KEY, btree (box_id, runsheet_id, box_line_id)
    "demo_line_items_box_line_id_idx" btree (box_line_id)
    "runsheet_line_items_runsheet_id_index" btree (runsheet_id)
NikolayS commented 3 years ago

@v0112358 thank you! Somehow I've stopped receiving email notifications from GitHub, so missed this – going to work on it and other contributions soon.

NikolayS commented 2 years ago

@v0112358 I've reworked i2 to use lpaded keys (example: "001 012" instead of "1 12"), please check it out and test if possible: https://github.com/NikolayS/postgres_dba/pull/55

Also fixed it in postgres-checkup: https://gitlab.com/postgres-ai/postgres-checkup/-/merge_requests/457