Skip to content

fix: remove-redundant-indexes-on-staging-tables - #6525

Open
ttoomey wants to merge 3 commits into
mainfrom
remove-redudnant-indexes-on-staging-tables
Open

fix: remove-redundant-indexes-on-staging-tables#6525
ttoomey wants to merge 3 commits into
mainfrom
remove-redudnant-indexes-on-staging-tables

Conversation

@ttoomey

@ttoomey ttoomey commented May 25, 2026

Copy link
Copy Markdown
Contributor

Merging this PR

  • use the squash-merge strategy for PRs targeting a release-X branch

Description

Remove redundant indexes on staging tables. These duplicates add bloat and degrade import performance.

Note the included migration is not reversible. It does not run in production though; the intent is for it to be run manually with:

# Dry-run (default) — shows what would be dropped
Dba::StagingIndexDeduplicator.new(table_pattern: 'hmis_2026%', dry_run: false).run!

# Actually drop the indexes
Dba::StagingIndexDeduplicator.new(table_pattern: 'hmis_2026%', dry_run: true).run!

Type of change

Schema cleanup, performance fix

Checklist before requesting review

  • I have performed a self-review of my code
  • I have run the code that is being changed under ideal conditions, and it doesn't fail
  • I have updated the documentation (or not applicable)
  • I have added spec tests (or not applicable)
  • I have provided testing instructions in this PR or the related issue (or not applicable)

@ttoomey
ttoomey force-pushed the remove-redudnant-indexes-on-staging-tables branch from c8f0d34 to 5446d33 Compare May 25, 2026 02:07
Comment thread app/models/dba/staging_index_deduplicator.rb Outdated
@ttoomey ttoomey changed the title fix: remove-redudnant-indexes-on-staging-tables fix: remove-redundant-indexes-on-staging-tables May 25, 2026
i.relname AS index_name,
am.amname AS access_method,
pg_get_indexdef(i.oid) AS index_def,
array_to_json(ARRAY(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gets a list of columns for each index. Postgres stores these as an array of column numbers, which this query translates into names. It also splits out any extra payload columns (INCLUDE columns that are part of the index but not part of the core search key) into include_columns_json

ix.indisunique AS is_unique,
ix.indisprimary AS is_primary,
ix.indpred IS NOT NULL AS is_partial,
pg_get_expr(ix.indpred, ix.indrelid) AS predicate,

@ttoomey ttoomey Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for partial indexes, this is the WHERE clause

@ttoomey
ttoomey requested a review from dtgreiner June 2, 2026 01:07
@ttoomey
ttoomey marked this pull request as ready for review June 2, 2026 01:08
@ttoomey
ttoomey force-pushed the remove-redudnant-indexes-on-staging-tables branch from 5c1bb01 to 16ba392 Compare June 2, 2026 01:20
@ttoomey
ttoomey force-pushed the remove-redudnant-indexes-on-staging-tables branch from 16ba392 to 8495cf2 Compare June 2, 2026 01:21
def staging_tables
conn.select_values(<<~SQL)
SELECT tablename FROM pg_tables
WHERE schemaname = 'public'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this:

Suggested change
WHERE schemaname = 'public'
WHERE schemaname in ('public', 'hmis')

That should find the indexes on the old 2022 tables if they exist.

Comment on lines +80 to +81
WHERE t.relname = #{conn.quote(table)}
AND n.nspname = 'public'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would need to update this as well.

@eanders eanders left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good idea to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants