fix(postgres): write no audit row when retention prunes nothing (#1810)
deploy / plan (push) Successful in 10s
deploy / gate (push) Successful in 8m29s
deploy / agent-image (push) Successful in 12s
deploy / dist-zip (push) Skipped
deploy / archied-image (push) Successful in 2m55s

This commit is contained in:
sam committed 2026-11-08 17:27:30 +11:00
1 parent 1d9eff6529
commit 5939ef6ff1
1 file changed
+9 -7
@@ -33,7 +33,7 @@ type auditPruneRecord struct {
// PruneAuditEvents deletes events of the given kinds older than cutoff in
// bounded batches and appends one sys_audit row recording the cutoff and the
// total removed, even when that total is zero. The whole sweep is one
// total removed, only when it is non-zero. The whole sweep is one
// transaction under the audit prune lock, so the lock covers every batch and a
// concurrent sweep gets ErrAuditPruneBusy without touching a row. The kind
// list is the caller's allowlist: this method deletes nothing else.
@@ -68,12 +68,14 @@ func (s *Store) PruneAuditEvents(ctx context.Context, kinds []string, cutoff tim
break
}
}
// The sweep is not an RPC, so it attributes its own row: the State Store
// pruned the events and the retention setting decided how many.
ctx = access.WithActor(access.WithSource(ctx, auditRetentionSource), auditRetentionActor)
if err := insertAuditValues(ctx, q, "audit_retention", "runtime_events", "pruned", nil,
auditPruneRecord{Cutoff: cutoff.UTC(), Rows: total}, 0, storecontract.DefaultOrgID); err != nil {
return 0, err
if total > 0 {
// The sweep is not an RPC, so it attributes its own row: the State
// Store pruned the events and the retention setting decided how many.
ctx = access.WithActor(access.WithSource(ctx, auditRetentionSource), auditRetentionActor)
if err := insertAuditValues(ctx, q, "audit_retention", "runtime_events", "pruned", nil,
auditPruneRecord{Cutoff: cutoff.UTC(), Rows: total}, 0, storecontract.DefaultOrgID); err != nil {
return 0, err
}
}
if err := tx.Commit(ctx); err != nil {
return 0, fmt.Errorf("prune audit events: %w", err)