by Matteo Scaramuccia.
Hi Ben,
that upgrade stage is part of MDL-43541, https://github.com/moodle/moodle/commit/9f3a4cbbcfd0688ec61485d36e5c26234c689dd5#diff-8f409afdb710dc2d678acde021558174R205.
3621 means The statement has been terminated (due to HY000), as written in the stack trace: have you already try to run the UPDATE query manually?
The key point is to understand why it fails in waiting for the exclusive lock - LCK_M_X, occurs when a task is waiting to acquire an Exclusive lock - unless $DB->get_recordset_sql is the culprit, coded here for performance reasons on large sets. Then, each UPDATE query has a PHP timeout of 60s so the timeout (and the error) should occur after 1 minute in your case.
HTH,
Matteo