This skill helps diagnose ClickHouse RAM usage, OOM risks, and memory pressure, guiding you to optimize queries and configurations.
npx playbooks add skill altinity/skills --skill altinity-expert-clickhouse-memory
Answers "where is the RAM going, and what will OOM next?" from system.asynchronous_metrics, system.metrics, system.dictionaries, system.parts, system.server_settings and system.query_log.
Run altinity-expert-clickhouse-connection first if the connection mode, cluster and time window are not yet established.
checks.sql — 15 checks: RAM total vs ClickHouse resident, component breakdown (dictionaries, Memory/Set/Join tables, primary keys, caches), allocation audit, top memory consumers among queries, dictionaries, Memory-engine tables and primary keys, memory over time, MEMORY_LIMIT_EXCEEDED exceptions, a query plus part_log memory timeline, server memory settings, memory used by non-ClickHouse processes, and high-memory GROUP BY and JOIN query shapes. Check memory-08 needs system.asynchronous_metric_log and memory-11 needs system.part_log; skip them when those tables are absent.reference.md — background (settings, sizing, anti-patterns); read only when you need to explain a recommendation.; and start with a -- @check <id> <title> header; keep the id with its result.-- @requires: skip the statement when the named table is missing, when keeper is required and the server has no Keeper/ZooKeeper, or when the version condition is not met. List skipped ids with the reason.{cluster} as written when a cluster macro exists; otherwise apply the connection skill's rewrite rule. Any other {placeholder} is a template variable: substitute a real value first or skip the statement.UNKNOWN_IDENTIFIER, run DESCRIBE TABLE system.<table> and drop the missing column.severity column is the verdict for that row. Copy it; do not re-grade.Critical above 90 percent, Major above 80). Compare it with memory-13 before blaming queries.MemoryTracking and MemoryResident means untracked allocations: caches, allocator fragmentation, or memory jemalloc has not returned to the OS. Tuning per-query limits will not close that gap; check cache sizes and consider a restart or SYSTEM JEMALLOC PURGE.Critical means other processes occupy the RAM reserved by max_server_memory_usage_to_ram_ratio. Fix the colocation or lower the ratio; raising ClickHouse limits makes the OOM killer more likely, not less.max_bytes_before_external_group_by to spill to disk, lower max_threads to cut per-thread hash tables, and reduce GROUP BY cardinality.max_bytes_in_join, switch join_algorithm to partial_merge or auto, and put the smaller table on the right side of the join.primary_key_bytes_in_memory (memory-02, memory-07) is a schema problem: too many columns in ORDER BY or an index_granularity that is too small. It is loaded for every active part and never evicted.complex_key_hashed and flat layouts dominate; a cache or direct layout trades RAM for latency.Set/Join tables hold everything in RAM with no spill path. Treat any multi-GiB entry here as a design finding.Live per-query memory, to catch the consumer while it is still running:
SELECT query_id, user, round(elapsed, 1) AS elapsed_s, formatReadableSize(memory_usage) AS memory, substring(query, 1, 120) AS query_preview FROM system.processes ORDER BY memory_usage DESC LIMIT 10;
Primary-key RAM and mark count for one table named by memory-07:
SELECT database, table, formatReadableSize(sum(primary_key_bytes_in_memory)) AS pk_ram, sum(marks) AS marks, count() AS parts FROM system.parts WHERE active AND database = '{database}' AND table = '{table}' GROUP BY database, table;
check, severity, object, evidence, recommendation; one row per finding, Critical first. Evidence quotes the numbers from the result rows.altinity-expert-clickhouse-mergesaltinity-expert-clickhouse-dictionariesaltinity-expert-clickhouse-cachesaltinity-expert-clickhouse-schemaaltinity-expert-clickhouse-reportingaltinity-expert-clickhouse-metricsWeekly roundup of top Claude Code skills, MCP servers, and AI coding tips.