ALTER does not go through because earlier mutations of the same table have not finished. A common cause is a mutation that fails on every attempt: everything submitted after it queues up behind it. Find that mutation in system.mutations, read its latest_fail_reason, and either wait for it or remove it with KILL MUTATION.
The error
In ClickHouse Cloud, anALTER such as DROP PROJECTION can be rejected with a message of this form:
MergeTree table in the same situation reports it differently, as the example below shows. The diagnosis and the fix are the same.
Example
Create a table and a mutation that cannot succeed:UPDATE returns without an error, because mutations run asynchronously by default. A later ALTER then fails:
ALTER is not undone. SHOW CREATE TABLE page_views no longer lists the projection, and DROP PROJECTION by_user is queued as mutation_3.txt behind the failing mutation. Failed mutations are retried with growing delays, so depending on the timing the statement can also wait for a long time before it returns this error.
Find the mutation that blocks the queue
latest_fail_reason values are shortened here. Read the result like this:
- A failure is recorded for every mutation that was applied to the part in the same attempt, so all rows can show the same
latest_fail_reason. The position of a row does not tell which mutation fails: a valid mutation that was submitted before the failing one, but was not applied yet, reports the same reason. - Find the failing mutation by its
command. The full text of the reason names the expression that failed afterwhile executing, heretoUInt32(duration_text), and onlymutation_2.txtcontains it. If the reason does not make it clear, test the expression of every unfinished command with aSELECT. - An empty
latest_fail_reasonand aparts_to_dovalue that goes down between two runs of the query mean that the mutation is working. Wait for it, then run theALTERagain. - The same
latest_fail_reasonin every run means that the mutation will not finish on its own.
system.mutations on every node shows all mutations. In a self-managed cluster, query all replicas with clusterAllReplicas.
Remove the stuck mutation
KILL MUTATIONdoes not roll back the parts that the mutation has already rewritten.- In ClickHouse Cloud, a killed mutation can stay at
is_killed = 1andis_done = 0for a while if another long-running mutation is ahead of it. - Before you repeat the
ALTERthat failed, checkSHOW CREATE TABLEandsystem.mutations. It may be applied or queued already, as in this example.
A related error
AnALTER that drops or renames a column used by an unfinished mutation is rejected right away, with the same remedy:
How to avoid it
- Test the expression of an
UPDATEorDELETEwith aSELECTbefore you submit the mutation. In the example,SELECT count() FROM page_views WHERE toUInt32OrNull(duration_text) IS NULLreturns1, the row that makestoUInt32fail. - Do not send many
ALTERstatements to one table at the same time. Combine them into one statement, or run them one after another and wait for each, as described in ConcurrentALTERassignment on one table. - Watch the number of unfinished mutations, see How do I view the number of active or queued mutations?.