From 46fc485a89dd5dd509d85ab92d4e38bd4748ce41 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 27 Nov 2026 08:49:07 -0500 Subject: [PATCH] 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. --- ...-make-documents-owner-ids-index-partial.js | 24 +++++++++++++++++++ .../queues/processors/UserDeletedProcessor.ts | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 server/migrations/20261127130000-make-documents-owner-ids-index-partial.js diff --git a/server/migrations/20261127130000-make-documents-owner-ids-index-partial.js b/server/migrations/20261127130000-make-documents-owner-ids-index-partial.js new file mode 100644 index 0000000000..d6a31d21e9 --- /dev/null +++ b/server/migrations/20261127130000-make-documents-owner-ids-index-partial.js @@ -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";' + ); + }, +}; diff --git a/server/queues/processors/UserDeletedProcessor.ts b/server/queues/processors/UserDeletedProcessor.ts index 2eb537a428..f49ac283d0 100644 --- a/server/queues/processors/UserDeletedProcessor.ts +++ b/server/queues/processors/UserDeletedProcessor.ts @@ -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,