Skip to content

feat: implement performant stale chat participant cleanup using postgres unnest#209

Merged
danoctua merged 3 commits intodevfrom
feat/stale-user-cleanup-unnest
Dec 24, 2025
Merged

feat: implement performant stale chat participant cleanup using postgres unnest#209
danoctua merged 3 commits intodevfrom
feat/stale-user-cleanup-unnest

Conversation

@danoctua
Copy link
Copy Markdown
Collaborator

@danoctua danoctua commented Dec 24, 2025

feat: Performant Stale Chat Participant Cleanup

Description

This pull request implements an efficient mechanism to clean up stale users from the database during the chat participant refresh process.

Key improvements:

  • Targeted Execution: Cleanup logic now only triggers during the refresh flow. Initial chat creation skips this step as no stale data exists, improving onboarding performance.
  • Efficient Deletion: Uses Postgres unnest for a single-query cleanup, avoiding argument limits and minimizing database round-trips.
  • Robustness: Includes explicit SQL type casting to handle cases where the active user list is empty (e.g., during bot indexing errors).

Checklist

  • I ran all tests and they passed successfully.
  • I reviewed my own code and followed the project's code style.
  • I included relevant details in the PR description or commit messages.

Changes

  • chat.py: Added cleanup flag to _index and _load_participants. Enabled only in _refresh.
  • user.py: Implemented delete_stale_participants with Postgres unnest and type casting.
  • tests: Added a new unit test for cleanup logic and verified against existing action tests.

How Has This Been Tested?

Tested in Linux Docker environment using pytest.

  • Verified cleanup only runs when cleanup=True.
  • Verified performance for large lists via unnest.

@danoctua danoctua self-assigned this Dec 24, 2025
@danoctua danoctua merged commit d3f2cbc into dev Dec 24, 2025
2 checks passed
@danoctua danoctua deleted the feat/stale-user-cleanup-unnest branch December 26, 2025 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant