Hi phang,
magnesium will coat the calcium surface of sand, rocks to prevent free calcium ions on coating it
if there is too much free calcium ions, it would want to combine with carbonate to form calcium carbonate which is not solutable, adding magnesium to around 1200mg/l would able the calcium to bond with the magnesium thus reducing free calcium ions
the acid released when aerobic bacteria breaks down ammonia and nitrite will break the bond between magnesium and calcium
if everything is in equilibrium, calcium and magnesium will bond and break at the same rate.