Skip to main content
Every replica of a ClickHouse Cloud service records the CPU and memory allocated to it as the CGroupMaxCPU and CGroupMemoryTotal metrics in system.asynchronous_metric_log. Aggregate these rows per minute to get the number of replicas, the total CPU and the total memory of the service over time. Compare every minute with the previous one to get the list of moments when the size changed.

Replicas, CPU and memory per minute

Set start_time and end_time to the period you are interested in:
The inner query returns the allocation of every replica in every minute. The hostname column is the name of the server that wrote the row, so the outer query counts the replicas that were running in that minute and adds up their CPU cores and memory.

Only the moments when the size changed

For a long period, keep only the minutes in which the number of replicas, the CPU or the memory differs from the previous minute:
WITH FILL adds a row of zeros for every minute of the period without data, including the minutes before the first and after the last row of the log, and lagInFrame returns the values of the previous minute. The first row of the result is the size at the start of the period or, if the service was not running then, its first start. Every following row is a change, and a stop that lasts until the end of the period is the last row. In both queries, set end_time at least one minute before the current time. The server writes rows to the log tables with a delay of a few seconds, so later minutes have no rows yet, or not all of them, and the result would show them as a stop or as a smaller size.

How to read the result

  • The numbers are totals over all replicas. To see every replica separately, run the inner SELECT of the first example together with its WITH clause.
  • ClickHouse Cloud scales and upgrades a service by adding new replicas before it removes the old ones, see make before break. While both exist, the result shows more replicas, CPU and memory than the service is configured for.
  • A row with zero replicas is not a measured size. It means that the log tables have no rows for that minute. Usually the service was idle or stopped, but the query cannot tell this apart from a minute in which running replicas wrote no metrics, and it reports the next minute with rows as a start.
  • The queries read the log tables of the replicas that exist when you run them. The make before break page says that 30 days of system table data are kept when replicas are replaced, so do not expect older history.
  • Queries on system tables prevent the service from idling, so do not run them on a tight schedule.