Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89ecbf2

Browse files
committed
delete in batches
1 parent 1df93be commit 89ecbf2

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,15 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
15261526
)
15271527
FROM
15281528
template_usage_stats
1529+
)
1530+
AND created_at < (
1531+
-- Delete at most in batches of 3 days (with a batch size of 3 days, we
1532+
-- can clear out the previous 6 months of data in ~60 iterations) whilst
1533+
-- keeping the DB load relatively low.
1534+
SELECT
1535+
COALESCE(MIN(created_at) + '3 days'::interval, NOW())
1536+
FROM
1537+
workspace_agent_stats
15291538
);
15301539
*/
15311540

@@ -1543,9 +1552,20 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
15431552
}
15441553

15451554
var validStats []database.WorkspaceAgentStat
1555+
var batchLimit time.Time
1556+
for _, stat := range q.workspaceAgentStats {
1557+
if batchLimit.IsZero() || stat.CreatedAt.Before(batchLimit) {
1558+
batchLimit = stat.CreatedAt
1559+
}
1560+
}
1561+
if batchLimit.IsZero() {
1562+
batchLimit = time.Now()
1563+
} else {
1564+
batchLimit = batchLimit.AddDate(0, 0, 3)
1565+
}
15461566
for _, stat := range q.workspaceAgentStats {
15471567
fmt.Println(stat.CreatedAt, limit)
1548-
if stat.CreatedAt.Before(limit) {
1568+
if stat.CreatedAt.Before(limit) && stat.CreatedAt.Before(batchLimit) {
15491569
fmt.Println("delete")
15501570
continue
15511571
}

coderd/database/queries/workspaceagentstats.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ WHERE
109109
)
110110
FROM
111111
template_usage_stats
112+
)
113+
AND created_at < (
114+
-- Delete at most in batches of 3 days (with a batch size of 3 days, we
115+
-- can clear out the previous 6 months of data in ~60 iterations) whilst
116+
-- keeping the DB load relatively low.
117+
SELECT
118+
COALESCE(MIN(created_at) + '3 days'::interval, NOW())
119+
FROM
120+
workspace_agent_stats
112121
);
113122

114123
-- name: GetDeploymentWorkspaceAgentStats :one

0 commit comments

Comments
 (0)