perf: Make documents ownerIds index partial (#14776)

Score-only updates on documents are non-HOT, so every update wrote a new
entry into the full GIN index on ownerIds even though almost all rows have
no owners. Index only rows with owners.
This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-27 08:49:07 -05:00
1 parent 2fb1503d35
commit 46fc485a89
2 files changed
+26 -1

No files matched your search

@@ -0,0 +1,24 @@
"use strict";
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface) {
// Most documents have no owners. A partial index skips them, so updates to
// those rows do not write to this GIN index.
await queryInterface.sequelize.query(
`CREATE INDEX CONCURRENTLY IF NOT EXISTS "documents_owner_ids_nonempty" ON "documents" USING GIN ("ownerIds") WHERE "ownerIds" <> '{}';`
);
await queryInterface.sequelize.query(
'DROP INDEX CONCURRENTLY IF EXISTS "documents_owner_ids";'
);
},
async down(queryInterface) {
await queryInterface.sequelize.query(
'CREATE INDEX CONCURRENTLY IF NOT EXISTS "documents_owner_ids" ON "documents" USING GIN ("ownerIds");'
);
await queryInterface.sequelize.query(
'DROP INDEX CONCURRENTLY IF EXISTS "documents_owner_ids_nonempty";'
);
},
};
@@ -85,7 +85,8 @@ export default class UserDeletedProcessor extends BaseProcessor {
{
where: {
teamId: event.teamId,
ownerIds: { [Op.contains]: [event.userId] },
// The non-empty check lets Postgres use the partial owner index.
ownerIds: { [Op.contains]: [event.userId], [Op.ne]: [] },
},
paranoid: false,
silent: true,