Skip to content

Commit 2ce8f62

Browse files
committed
refactor: optimize DeleteOldAuditLogs to use :execrows
Use :execrows instead of :one to simplify the query by removing the extra CTE wrapper. This lets PostgreSQL return the row count directly via RowsAffected() instead of requiring an explicit COUNT(*) scan.
1 parent 5a47352 commit 2ce8f62

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

coderd/database/queries.sql.go

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/auditlogs.sql

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ WHERE id IN (
254254
LIMIT @limit_count
255255
);
256256

257-
-- name: DeleteOldAuditLogs :one
257+
-- name: DeleteOldAuditLogs :execrows
258258
-- Deletes old audit logs based on retention policy, excluding deprecated
259259
-- connection events (connect, disconnect, open, close) which are handled
260260
-- separately by DeleteOldAuditLogConnectionEvents.
@@ -266,11 +266,7 @@ WITH old_logs AS (
266266
AND action NOT IN ('connect', 'disconnect', 'open', 'close')
267267
ORDER BY "time" ASC
268268
LIMIT @limit_count
269-
),
270-
deleted_rows AS (
271-
DELETE FROM audit_logs
272-
USING old_logs
273-
WHERE audit_logs.id = old_logs.id
274-
RETURNING audit_logs.id
275269
)
276-
SELECT COUNT(deleted_rows.id) AS deleted_count FROM deleted_rows;
270+
DELETE FROM audit_logs
271+
USING old_logs
272+
WHERE audit_logs.id = old_logs.id;

0 commit comments

Comments
 (0)