Problems Upgrading database from acitiviti

We are trying to upgrade or avtiviti 5.21 postgres-instance to camunda. But run into problems with the migrations scripts. Whe i run postgres-1-upgrade.sql. The script hangs forevere on this SQL:

UPDATE
ACT_RU_EXECUTION E
SET
ACT_INST_ID_ = (
SELECT
MIN(HAI.ID_)
FROM
ACT_HI_ACTINST HAI
INNER JOIN
ACT_RU_EXECUTION SCOPE
ON
HAI.EXECUTION_ID_ = SCOPE.ID_
AND
SCOPE.PARENT_ID_ = E.ID_
AND
SCOPE.IS_SCOPE_ = true
WHERE
HAI.END_TIME_ is null
AND
NOT EXISTS (
SELECT
ACT_INST_ID_
FROM
ACT_RU_EXECUTION CHILD
WHERE
CHILD.ACT_INST_ID_ = HAI.ID_
AND
E.ACT_ID_ is not null
)
)
WHERE
E.ACT_INST_ID_ is null;

Anyone got an idea how to solve this? I have checked and there are no locks involved. I suspect that the migrations script has not really been tested on any volumne of data. Grateful if someone could give any input?