Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing postgres indexes #3910

Merged
merged 2 commits into from
Nov 5, 2024
Merged

Conversation

jessicamcinchak
Copy link
Member

Follows on from publishing debugging today: https://opensystemslab.slack.com/archives/C01E3AC0C03/p1730715895034839

Approached this from two directions:

  1. Which indexes are we missing? Where is postgres is doing more sequence scans than index scans?
  1. Which existing indexes are unused? Can any be removed?
  • Ran select * from pg_stat_user_indexes where schemaname = 'public' order by idx_scan asc; for this one
  • We do have multiple cases of existing indexes that have never been hit, but they're all direct remnants of "Primary keys" or "Unique keys" created via Hasura and therefore don't make sense to drop
  • Screenshot from 2024-11-04 18-45-36

Wish that published_flows & flows would have come up higher on the "need to improve" checks, but the truth is they didn't !

CREATE INDEX "reconciliation_requests_session_id_idx" on
"public"."reconciliation_requests" using hash ("session_id");
CREATE INDEX "published_flows_created_at_idx" on
"public"."published_flows" using btree ("created_at");
Copy link
Member Author

@jessicamcinchak jessicamcinchak Nov 4, 2024

Choose a reason for hiding this comment

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

A couple notes about this migration:

  • Why a mix of hash versus btree index types?
    • I'm using hash indices when it's a column that is exclusively queried via an equality check (eg session id) and btree indices when a) it's a column that is queried via equality or range checks or b) multiple columns are combined into the same index
    • Postgres docs on this are helpful https://www.postgresql.org/docs/current/indexes-types.html
  • I'm adding an index to published_flows.created_at because that is how all of our current queries are ordering, but I have not actually checked/compared yet if there's a difference to sort by published flow id (or jogged my memory about why we don't do this already). Even if we swap how we order in the future, an index on created_at should still be useful/hit here when we do date comparisons for reconciliation

Copy link
Contributor

Choose a reason for hiding this comment

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

It may also be worth adding a DESC in here as we're always searching in this order (docs).

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool good shout - that's now updated 👍 Worth noting order isn't possible to specify in Hasura GUI for adding indexes, but worked no problem when added via "SQL" panel directly
Screenshot from 2024-11-05 09-13-40

@@ -157,7 +157,7 @@
definition:
enable_manual: false
insert:
columns: "*"
columns: '*'
Copy link
Member Author

Choose a reason for hiding this comment

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

This file is only formatting changes 🌀

@jessicamcinchak jessicamcinchak marked this pull request as ready for review November 4, 2024 18:26
@jessicamcinchak
Copy link
Member Author

@jessicamcinchak jessicamcinchak requested a review from a team November 4, 2024 18:28
Copy link

github-actions bot commented Nov 4, 2024

Removed vultr server and associated DNS entries

CREATE INDEX "reconciliation_requests_session_id_idx" on
"public"."reconciliation_requests" using hash ("session_id");
CREATE INDEX "published_flows_created_at_idx" on
"public"."published_flows" using btree ("created_at");
Copy link
Contributor

Choose a reason for hiding this comment

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

It may also be worth adding a DESC in here as we're always searching in this order (docs).

@jessicamcinchak jessicamcinchak merged commit aa80b32 into main Nov 5, 2024
12 checks passed
@jessicamcinchak jessicamcinchak deleted the jess/pg-indexes-maintenance branch November 5, 2024 08:30
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.

2 participants