We conducted this randomized controlled trial to determine whether the intraoperative measurement and correction of ionized plasma magnesium can reduce the risk of cardiac arrhythmia after cardiopulmonary bypass. Eighty-five patients presenting for coronary artery bypass grafting were randomly assigned either to the magnesium-corrected group, which received magnesium sulfate on the basis of measured levels of ionized plasma magnesium (n = 43), or to the control group, in which magnesium levels were identified but not corrected (n = 42). Ionized magnesium was determined with an ion-selective electrode with minimal delay, and further samples were taken for laboratory analysis of total plasma magnesium. All patients had Holter electrocardiogram monitoring for 72 h after surgery. Total hypomagnesemia (45 patients; 53% of all patients) was more common than ionized hypomagnesemia (11 patients; 13%) before cardiopulmonary bypass. Both total and ionized magnesium levels declined further during the course of cardiopulmonary bypass in the control group. The incidence of ventricular tachycardia in the first 24 h was less frequent in the magnesium-corrected group (3 patients; 7%) than the control group (12 patients, 30%; P < 0.01). Patients in the magnesium-corrected group were more likely to display continuous sinus rhythm (Lown Grade 0) in the first 24 h (14 patients; 34%) than patients in the control group (2 patients, 5%; P < 0.001). Our results suggest that the intraoperative correction of ionized magnesium is associated with a reduction in postoperative ventricular arrhythmia in cardiac surgical patients.