Skip to main content
A MergeTree table keeps the primary index of its data parts in memory once it is loaded, whether or not anyone still queries the table. Tables left behind by a rename-based migration (sessions_old, events_backup) are the usual case: the new table is small, the old one still holds its whole index. Find such tables with the primary_key_bytes_in_memory column of system.parts, release the memory with SYSTEM UNLOAD PRIMARY KEY, and drop the tables when you no longer need them.

Example

Create a table, fill it, then migrate to a new sorting key by renaming the old table and creating a new one:
Break the memory used by primary keys down by exact table name:
The renamed table still holds its index. The numbers are tiny here because the table is tiny: the index has one entry per mark, so its size grows with the number of marks and with the width of the key columns. Unload the primary key of the table that is no longer used, and run the query again:
The statement only frees the in-memory copy of the index. It doesn’t change the data on disk, and the table stays readable.

Why the memory is not released on its own

  • A part written by an INSERT or by a merge keeps the index it has built in memory.
  • After a server restart, the primary_key_lazy_load setting (enabled by default) loads the index of a part when a query first needs it, so a table that nobody queries starts with nothing loaded.
  • Once loaded, the index of an active part stays in memory. RENAME TABLE doesn’t touch the parts, so the renamed table keeps everything it had loaded.
When you look for candidates, group by the exact table name as in the query above. A filter or a roll-up by a name pattern adds the old and the new table together and hides which of them holds the memory.

The index is loaded again on the next use

Unloading is not permanent. A query that filters on the key loads the index again:
After this query the diagnostic query shows the old table at the top again:
Parts created by later merges of the table hold their index as well. To keep the memory released, drop the table once you have confirmed that nothing reads it and that any backfill from it is complete:

Clusters and privileges

SYSTEM UNLOAD PRIMARY KEY acts on the server that executes it. On a cluster, run it on every replica or add ON CLUSTER:
To see the memory per replica, read system.parts through clusterAllReplicas and add hostName() to the grouping. In ClickHouse Cloud the cluster name is default, as described in Querying across nodes:
The statement needs the SYSTEM UNLOAD PRIMARY KEY privilege, which can be granted for one table:
Without a table name, SYSTEM UNLOAD PRIMARY KEY unloads the primary keys of all tables and needs the privilege globally. The statement is available since ClickHouse 24.4.

Large tables that are still in use

Unloading helps for tables that are not read. For a table that is queried and has a large primary index, look at the use_primary_key_cache setting instead: with it, the indexes are kept in a shared cache of limited size and are not held by every part. Parts that already hold their index keep it until it is unloaded.