Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

⚡️ perf(database): correlate the data export's message-ref visitor filter (#21709)

Arvin Xuedaf1812a470
diff --git a/packages/database/src/repositories/dataExporter/index.test.ts b/packages/database/src/repositories/dataExporter/index.test.ts
index e44253d3799..d7301ab339f 100644
--- a/packages/database/src/repositories/dataExporter/index.test.ts
+++ b/packages/database/src/repositories/dataExporter/index.test.ts
@@ -392,6 +392,21 @@ describe('DataExporterRepos', () => {
expect(result.messagePlugins.map((plugin) => plugin.id)).toEqual([testIds.messageId]);
});
+ it('should correlate the message-ref visitor filter on the parent message id', () => {
+ // An uncorrelated `id IN (SELECT id FROM messages …)` under the OR runs as
+ // a hashed SubPlan over the whole messages table in production. The
+ // filter must look up each row's own parent message instead.
+ const repo = new DataExporterRepos(db, userId) as any;
+ const condition = repo.buildShareVisitorCondition(
+ { column: 'id', via: 'messageRef' },
+ messagePlugins,
+ );
+ const { sql } = db.select().from(messagePlugins).where(condition).toSQL();
+
+ expect(sql).toContain('"messages"."id" = "message_plugins"."id"');
+ expect(sql).not.toMatch(/"message_plugins"\."id" in \(select/i);
+ });
+
it('should export data for a different user', async () => {
// Create another user
const anotherUserId = 'another-user-id';
diff --git a/packages/database/src/repositories/dataExporter/index.ts b/packages/database/src/repositories/dataExporter/index.ts
index 41ad8b8b22f..2a580752d55 100644
--- a/packages/database/src/repositories/dataExporter/index.ts
+++ b/packages/database/src/repositories/dataExporter/index.ts
@@ -1,4 +1,4 @@
-import { and, eq, inArray, isNull, or, type SQL } from 'drizzle-orm';
+import { and, eq, exists, inArray, isNull, or, type SQL, sql } from 'drizzle-orm';
import pMap from 'p-map';
import * as EXPORT_TABLES from '../../schemas';
@@ -141,11 +141,20 @@ export class DataExporterRepos {
// `messageRef`: no topic column here, so keep only rows whose parent
// message itself survives `notShareVisitorMessage()`. A NULL message
// reference is trivially kept, matching the shared helpers' semantics.
+ //
+ // Correlate on the parent message id. The earlier `column IN (SELECT id
+ // FROM messages WHERE …)` sat under an OR, so Postgres could not pull it
+ // up into a join and ran it as a hashed SubPlan: a parallel seq scan of
+ // the whole `messages` table (~38M rows, 110 GB) on every export. Those
+ // scans outlived the 300s function timeout, piled up on client retries
+ // and saturated the primary's storage IO on 2026-10-27.
return or(
isNull(column),
- inArray(
- column,
- this.db.select({ id: messages.id }).from(messages).where(notShareVisitorMessage()),
+ exists(
+ this.db
+ .select({ one: sql`1` })
+ .from(messages)
+ .where(and(eq(messages.id, column), notShareVisitorMessage())),
),
);
}