diff --git a/internal/infrastructure/postgres/audit_retention.go b/internal/infrastructure/postgres/audit_retention.go index 8154b7125..296c1e325 100644 --- a/internal/infrastructure/postgres/audit_retention.go +++ b/internal/infrastructure/postgres/audit_retention.go @@ -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)