Skip to content

Commit 59410fc

Browse files
committed
fix(data-drains): expression indexes match date_trunc cursor ORDER BY
Btree indexes on raw timestamp columns can't satisfy ORDER BY `date_trunc('milliseconds', col)`. Switch the five drain-source composite indexes (and snapshot) to expression indexes on `(workspace_id, date_trunc('milliseconds', col), id)` so the planner can use them for both the WHERE filter and the sort step.
1 parent 2888404 commit 59410fc

3 files changed

Lines changed: 20 additions & 20 deletions

File tree

packages/db/migrations/0202_panoramic_dreaming_celestial.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ CREATE INDEX "data_drain_runs_drain_started_idx" ON "data_drain_runs" USING btre
4343
CREATE INDEX "data_drains_org_idx" ON "data_drains" USING btree ("organization_id");--> statement-breakpoint
4444
CREATE INDEX "data_drains_due_idx" ON "data_drains" USING btree ("enabled","last_run_at");--> statement-breakpoint
4545
CREATE UNIQUE INDEX "data_drains_org_name_unique" ON "data_drains" USING btree ("organization_id","name");--> statement-breakpoint
46-
CREATE INDEX "copilot_chats_workspace_created_at_id_idx" ON "copilot_chats" USING btree ("workspace_id","created_at","id");--> statement-breakpoint
47-
CREATE INDEX "copilot_runs_workspace_completed_at_id_idx" ON "copilot_runs" USING btree ("workspace_id","completed_at","id");--> statement-breakpoint
48-
CREATE INDEX "job_execution_logs_workspace_ended_at_id_idx" ON "job_execution_logs" USING btree ("workspace_id","ended_at","id");--> statement-breakpoint
49-
CREATE INDEX "workflow_execution_logs_workspace_ended_at_id_idx" ON "workflow_execution_logs" USING btree ("workspace_id","ended_at","id");--> statement-breakpoint
50-
CREATE INDEX "audit_log_workspace_created_at_id_idx" ON "audit_log" USING btree ("workspace_id","created_at","id");
46+
CREATE INDEX "copilot_chats_workspace_created_at_id_idx" ON "copilot_chats" USING btree ("workspace_id",date_trunc('milliseconds', "created_at"),"id");--> statement-breakpoint
47+
CREATE INDEX "copilot_runs_workspace_completed_at_id_idx" ON "copilot_runs" USING btree ("workspace_id",date_trunc('milliseconds', "completed_at"),"id");--> statement-breakpoint
48+
CREATE INDEX "job_execution_logs_workspace_ended_at_id_idx" ON "job_execution_logs" USING btree ("workspace_id",date_trunc('milliseconds', "ended_at"),"id");--> statement-breakpoint
49+
CREATE INDEX "workflow_execution_logs_workspace_ended_at_id_idx" ON "workflow_execution_logs" USING btree ("workspace_id",date_trunc('milliseconds', "ended_at"),"id");--> statement-breakpoint
50+
CREATE INDEX "audit_log_workspace_created_at_id_idx" ON "audit_log" USING btree ("workspace_id",date_trunc('milliseconds', "created_at"),"id");

packages/db/migrations/meta/0202_snapshot.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,8 @@
12691269
"nulls": "last"
12701270
},
12711271
{
1272-
"expression": "created_at",
1273-
"isExpression": false,
1272+
"expression": "date_trunc('milliseconds', \"created_at\")",
1273+
"isExpression": true,
12741274
"asc": true,
12751275
"nulls": "last"
12761276
},
@@ -1973,8 +1973,8 @@
19731973
"nulls": "last"
19741974
},
19751975
{
1976-
"expression": "created_at",
1977-
"isExpression": false,
1976+
"expression": "date_trunc('milliseconds', \"created_at\")",
1977+
"isExpression": true,
19781978
"asc": true,
19791979
"nulls": "last"
19801980
},
@@ -2610,8 +2610,8 @@
26102610
"nulls": "last"
26112611
},
26122612
{
2613-
"expression": "completed_at",
2614-
"isExpression": false,
2613+
"expression": "date_trunc('milliseconds', \"completed_at\")",
2614+
"isExpression": true,
26152615
"asc": true,
26162616
"nulls": "last"
26172617
},
@@ -6417,8 +6417,8 @@
64176417
"nulls": "last"
64186418
},
64196419
{
6420-
"expression": "ended_at",
6421-
"isExpression": false,
6420+
"expression": "date_trunc('milliseconds', \"ended_at\")",
6421+
"isExpression": true,
64226422
"asc": true,
64236423
"nulls": "last"
64246424
},
@@ -13267,8 +13267,8 @@
1326713267
"nulls": "last"
1326813268
},
1326913269
{
13270-
"expression": "ended_at",
13271-
"isExpression": false,
13270+
"expression": "date_trunc('milliseconds', \"ended_at\")",
13271+
"isExpression": true,
1327213272
"asc": true,
1327313273
"nulls": "last"
1327413274
},

packages/db/schema.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ export const workflowExecutionLogs = pgTable(
356356
),
357357
workspaceEndedAtIdIdx: index('workflow_execution_logs_workspace_ended_at_id_idx').on(
358358
table.workspaceId,
359-
table.endedAt,
359+
sql`date_trunc('milliseconds', ${table.endedAt})`,
360360
table.id
361361
),
362362
runningStartedAtIdx: index('workflow_execution_logs_running_started_at_idx')
@@ -590,7 +590,7 @@ export const jobExecutionLogs = pgTable(
590590
),
591591
workspaceEndedAtIdIdx: index('job_execution_logs_workspace_ended_at_id_idx').on(
592592
table.workspaceId,
593-
table.endedAt,
593+
sql`date_trunc('milliseconds', ${table.endedAt})`,
594594
table.id
595595
),
596596
executionIdUnique: uniqueIndex('job_execution_logs_execution_id_unique').on(table.executionId),
@@ -1699,7 +1699,7 @@ export const copilotChats = pgTable(
16991699
updatedAtIdx: index('copilot_chats_updated_at_idx').on(table.updatedAt),
17001700
workspaceCreatedAtIdIdx: index('copilot_chats_workspace_created_at_id_idx').on(
17011701
table.workspaceId,
1702-
table.createdAt,
1702+
sql`date_trunc('milliseconds', ${table.createdAt})`,
17031703
table.id
17041704
),
17051705
})
@@ -1835,7 +1835,7 @@ export const copilotRuns = pgTable(
18351835
),
18361836
workspaceCompletedAtIdIdx: index('copilot_runs_workspace_completed_at_id_idx').on(
18371837
table.workspaceId,
1838-
table.completedAt,
1838+
sql`date_trunc('milliseconds', ${table.completedAt})`,
18391839
table.id
18401840
),
18411841
streamIdUnique: uniqueIndex('copilot_runs_stream_id_unique').on(table.streamId),
@@ -2418,7 +2418,7 @@ export const auditLog = pgTable(
24182418
),
24192419
workspaceCreatedIdIdx: index('audit_log_workspace_created_at_id_idx').on(
24202420
table.workspaceId,
2421-
table.createdAt,
2421+
sql`date_trunc('milliseconds', ${table.createdAt})`,
24222422
table.id
24232423
),
24242424
actorCreatedIdx: index('audit_log_actor_created_idx').on(table.actorId, table.createdAt),

0 commit comments

Comments
 (0)