v2.1.37 - Replace pg_class estimates with actual SQL counts for WebSocket connections and active subscriptions in stats and kind_1_report APIs

This commit is contained in:
Laan Tungir
2026-08-04 06:05:56 -04:00
parent ca7a4b6722
commit a71ec75aa5
6 changed files with 3133 additions and 17 deletions
+13 -8
View File
@@ -31,18 +31,23 @@ try {
$process_id = strval($pid);
} catch (PDOException $e) {}
// WebSocket connections: tracked in-memory by the relay (g_connection_count).
// The subscriptions table is a 32M-row historical log, not a live state table.
// Use pg_class for an instant estimate (updated by autovacuum).
// WebSocket connections: count distinct wsi_pointer values with active subscriptions.
// The subscriptions table is a historical log, so we filter for rows where
// event_type = 'created' AND ended_at IS NULL (i.e., still open).
$ws_connections = 0;
try {
$est = intval($pdo->query("SELECT reltuples FROM pg_class WHERE relname = 'subscriptions'")->fetchColumn());
// ~60% are 'created', ~40% are 'closed' — active ≈ created - closed
$ws_connections = max(0, intval($est * 0.2));
$ws_connections = intval($pdo->query(
"SELECT COUNT(DISTINCT wsi_pointer) FROM subscriptions WHERE event_type = 'created' AND ended_at IS NULL"
)->fetchColumn());
} catch (PDOException $e) {}
// Active subscriptions: same estimate approach.
$active_subscriptions = $ws_connections;
// Active subscriptions: count rows where event_type = 'created' AND ended_at IS NULL.
$active_subscriptions = 0;
try {
$active_subscriptions = intval($pdo->query(
"SELECT COUNT(*) FROM subscriptions WHERE event_type = 'created' AND ended_at IS NULL"
)->fetchColumn());
} catch (PDOException $e) {}
// Memory/CPU (from /proc on Linux)
$memory_usage = '-';