Skip to content

[Feature]: Buffer node-user usage history writes to reduce WAL #786

Closed

Description

@dr-hoseyn

Before you submit

  • I searched existing issues and did not find the same request.
  • This request is about PasarGuard itself, not support for my deployment.

Problem

record_user_usages runs every 10 seconds and immediately UPSERTs every active (created_at, user_id, node_id) sample into node_user_usages. Records already use a 10-minute bucket, so the same indexed row can be updated about 60 times per bucket.

On larger PostgreSQL/TimescaleDB installations this creates avoidable WAL and index/heap churn even after the set-based UNNEST work in #777/#778. An anonymized ~25k-user installation recorded the history UPSERT as the largest WAL statement in a fresh pg_stat_statements sample: about 63.6 MB of WAL across 19 calls, while the 19 calls affected about 91k source rows.

The authoritative users.used_traffic and admins.used_traffic counters must remain immediate; only analytical chart history needs write coalescing.

Proposed solution

  • Keep authoritative user/admin traffic updates on every recorder cycle.
  • For PostgreSQL/TimescaleDB only, aggregate pending node_user_usages deltas in memory and flush them at a configurable interval (60 seconds by default).
  • Flush before a 10-minute history bucket rolls over and during graceful shutdown.
  • Keep failed pending data available for a later retry and log failures clearly.
  • Keep the existing immediate MySQL and SQLite paths unchanged.
  • Aggregate duplicate deltas and omit only net-zero history values so valid corrections remain accurate.
  • Add focused tests for aggregation, flush timing, bucket rollover, failure retry, and unchanged fallback behavior.

This preserves the existing 10-minute chart granularity and totals while reducing the number of heavy PostgreSQL history UPSERTs by roughly 6x at the default job interval.

Alternatives considered

  • Increasing the main usage job interval: rejected because authoritative counters, online status, and limit enforcement need timely updates.
  • Changing the history bucket from 10 minutes to 1 minute: rejected because it would increase row/index growth.
  • Converting the existing table directly to a TimescaleDB hypertable: useful later, but unsafe as an automatic migration for large existing tables and does not by itself eliminate repeated writes to the current bucket.
  • Applying retention alone: controls old data size but does not reduce current-bucket WAL and can itself generate substantial WAL during deletion.

Area

Database

Impact

Important

Examples or references

Related but separate work: #777 and PR #778 reduce users table update amplification. This request targets the analytical node_user_usages write path.

Additional context

No schema migration should be required. The implementation should be based on the repository's active development branch (dev); CONTRIBUTING.md mentions next, but that branch does not currently exist on the upstream remote.

Activity

  1. T3ST3ST3R0N commented on Oct 10, 2026

    @T3ST3ST3R0N
    Collaborator

    Done in #787, merged into dev. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions