agent-substrate / agent-substrate/substrate

Move PostgreSQL expired-lease cleanup off the lock acquisition path

Open
#1,040 0 comments 0 reactions 0 assignees View on GitHub
area/api-machinery kind/cleanup
Dominant language
Go
Stars
1.8k
Forks
316
Avg merge
2d 43m
Merged PRs (30d)
287

Description

## Problem

In #1023 `atepg.AcquireLock` currently runs `DELETE FROM leases WHERE expires_at <= clock_timestamp()` before every lease acquisition. The `expires_at` index keeps this bounded, but it still adds an extra database write and round trip to every lock acquisition, including when there are no expired leases.

The acquisition query already reclaims an expired lease for the requested key through `INSERT ... ON CONFLICT ... DO UPDATE ... WHERE leases.expires_at <= clock_timestamp()`, so global cleanup is not required for lock correctness. It only prevents expired rows for inactive keys from accumulating.

## Proposed follow-up

Move global expired-lease cleanup off the acquisition path, for example to a periodic background task with a bounded interval and/or batch size. In a multi-replica deployment, ensure cleanup work is not needlessly duplicated or that concurrent cleaners remain cheap and safe.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.