perf(database): add contact-tag/embedding indexes and orphan-attachment cleanup - #1197
Open
realcodesiman wants to merge 2 commits into
Open
realcodesiman wants to merge 2 commits into
realcodesiman wants to merge 2 commits into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Implements P0 item A1/A2 and P2 item A8 (partial) from the system-design-review audit.
A1 — ContactToTag index
ContactToTagonly had the composite PK(contactId, tagId)— every "contacts with tag X" query (segments, broadcast audience, filters) scanned poorly at scale. AddedContactToTag_tagId_contactId_idxon(tagId, contactId).A2 — AIEmbedding HNSW index
AIEmbedding.embedding(vector(1536)) only had a btreeworkspaceIdindex — similarity search was a full seq scan + cosine per row. Its sibling tableAIConversationEmbeddingalready has an HNSW index; added the equivalentAIEmbedding_embedding_idx(hnsw,vector_cosine_ops).Both index creations use
CREATE INDEX CONCURRENTLY IF NOT EXISTSin the generated migration, and were applied and verified against a real local Postgres (timescale/timescaledb-ha:pg18-all).A8 — orphaned attachment cleanup (partial)
Message/Attachmentare hypertable rows with no FK to their parent (compressed-chunk deletes can't carry an FK) — so attachments can outlive their message. Addedapps/worker/src/schedule/handlers/purge-orphaned-attachments.ts, a new scheduled job (guarded by an in-process flag +distributedLock, batched 1000/run) that reconciles orphaned attachment rows. The retention period forMessageitself is an explicit business decision, not made here — this only closes the orphan-attachment gap the audit called out as independently actionable.Verification
pnpm --filter @chatbotx.io/database check-types && test && test:db— 729 + 16 integration tests pass against real local Postgrespnpm --filter worker check-types && test— 2342/2342 passpnpm --filter @chatbotx.io/database db:migrateagainst local Postgres