-
Notifications
You must be signed in to change notification settings - Fork 794
perf: add pg_trgm GIN indexes for backoffice organization search #14325
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06f5eb1
perf: add pg_trgm GIN indexes for backoffice organization search
pieterbeulque 6d9179f
fix(backoffice): make org slug search actually use the trigram index
claude 79e99f0
fix(backoffice): normalize the search query in the status counts too
claude 098a875
refactor(backoffice): share the organization name/slug search predicate
claude 11dc083
Merge remote-tracking branch 'origin/main' into pieter/pg-trgm-org-se…
pieterbeulque dcdcf57
fix(migrations): update pg_trgm migration head
pieterbeulque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
server/migrations/versions/2026-09-09-2145_add_pg_trgm_gin_indexes_for_backoffice_.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """add pg_trgm gin indexes for backoffice organization search | ||
|
|
||
| Revision ID: 1b299ae956f3 | ||
| Revises: 382c4661fdb2 | ||
| Create Date: 2026-09-09 21:45:27.915455 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # Polar Custom Imports | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "1b299ae956f3" | ||
| down_revision = "382c4661fdb2" | ||
| branch_labels: tuple[str] | None = None | ||
| depends_on: tuple[str] | None = None | ||
|
|
||
| NAME_INDEX = "ix_organizations_name_trgm" | ||
| SLUG_INDEX = "ix_organizations_slug_trgm" | ||
| EMAIL_INDEX = "ix_organizations_email_trgm" | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| # The backoffice organization search runs a leading-wildcard | ||
| # `ILIKE '%q%'` across name/slug/email, which no btree index can serve, so | ||
| # it seq-scans the now-large organizations table (recently hit the 30s | ||
| # statement timeout). pg_trgm GIN indexes make those `ILIKE` predicates | ||
| # index-backed. Built CONCURRENTLY so the deploy never locks the table. | ||
| op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm") | ||
|
|
||
| # CREATE INDEX CONCURRENTLY cannot run inside a transaction; autocommit_block | ||
| # commits the pending extension change and runs each build in autocommit. | ||
| with op.get_context().autocommit_block(): | ||
| for index_name, expression in ( | ||
| (NAME_INDEX, "name"), | ||
| # `slug` is CITEXT: `slug ILIKE ...` resolves to citext's operator, | ||
| # which `gin_trgm_ops` (a `text` opclass) doesn't serve, so index | ||
| # the `slug::text` expression the search casts to. | ||
| (SLUG_INDEX, "(slug::text)"), | ||
| (EMAIL_INDEX, "email"), | ||
| ): | ||
| # A previously-interrupted concurrent build leaves an INVALID index of | ||
| # the same name; drop it first so a re-run doesn't fail on "already | ||
| # exists". | ||
| op.drop_index( | ||
| index_name, | ||
| table_name="organizations", | ||
| if_exists=True, | ||
| postgresql_concurrently=True, | ||
| ) | ||
| op.create_index( | ||
| index_name, | ||
| "organizations", | ||
| [sa.text(f"{expression} gin_trgm_ops")], | ||
| postgresql_concurrently=True, | ||
| postgresql_using="gin", | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # Leave the pg_trgm extension in place; other indexes may rely on it. | ||
| with op.get_context().autocommit_block(): | ||
| for index_name in (NAME_INDEX, SLUG_INDEX, EMAIL_INDEX): | ||
| op.drop_index( | ||
| index_name, | ||
| table_name="organizations", | ||
| if_exists=True, | ||
| postgresql_concurrently=True, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from sqlalchemy import ColumnElement, Text, or_ | ||
|
|
||
| from polar.models import Organization | ||
|
|
||
|
|
||
| def organization_ilike(term: str) -> ColumnElement[bool]: | ||
| """Match an organization by name or slug, e.g. ``organization_ilike("%acme%")``. | ||
|
|
||
| ``Organization.slug`` is ``CITEXT``, so an uncast ``ILIKE`` binds citext's | ||
| own operator and can't use ``ix_organizations_slug_trgm`` — which also | ||
| stops the planner from using the name index, since neither branch of the | ||
| ``OR`` would be index-backed. Cast to ``text`` so both apply. | ||
| """ | ||
| return or_( | ||
| Organization.name.ilike(term), | ||
| Organization.slug.cast(Text).ilike(term), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: When an environment has already applied
1b299ae956f3with its old parent, this reparenting makes382c4661fdb2an ancestor that Alembic will skip because the database is already stamped at the current head. Preserve the old parent and add a merge/bridge revision, or explicitly reconcile those databases, so the member index migration still runs.Prompt for AI agents