fixed CPU logging from db

This commit is contained in:
erik 2025-05-26 21:35:22 +00:00
parent add24e5c9d
commit 4de85b8db4
2 changed files with 27 additions and 59 deletions

View file

@ -143,13 +143,17 @@ async def init_db_async():
))
except Exception as e:
print(f"Warning: failed to create composite index ix_telemetry_events_char_ts: {e}")
# Disable parallel workers at the system level to avoid OOMs from large parallel scans
# Add retention and compression policies on the hypertable
try:
# Apply settings outside transaction for ALTER SYSTEM
conn2 = engine.connect().execution_options(isolation_level="AUTOCOMMIT")
conn2.execute(text("ALTER SYSTEM SET max_parallel_workers_per_gather = 0"))
conn2.execute(text("ALTER SYSTEM SET max_parallel_workers = 0"))
conn2.execute(text("SELECT pg_reload_conf()"))
conn2.close()
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
# Retain only recent data (default 7 days or override via DB_RETENTION_DAYS)
days = int(os.getenv('DB_RETENTION_DAYS', '7'))
conn.execute(text(
f"SELECT add_retention_policy('telemetry_events', INTERVAL '{days} days')"
))
# Compress chunks older than 1 day
conn.execute(text(
"SELECT add_compression_policy('telemetry_events', INTERVAL '1 day')"
))
except Exception as e:
print(f"Warning: failed to disable parallel workers: {e}")
print(f"Warning: failed to set retention/compression policies: {e}")