All patients referred for non-emergent cardiac surgery for isolated CABG, isolated valvular heart surgery, or combined valvular and CABG surgery are eligible for the study. Subjects will be randomized prior to surgery (1:1) to receive 5 g magnesium or placebo (saline) bolus by the anaesthesiologist upon removal of the crossclamp. On postoperative days 1 through 4, subjects will receive either IV MgSO4 (5g in 250 ml normal saline) or IV placebo (250 ml normal saline) infusion over 4 hours daily. Atrial fibrillation (and other arrhythmias) will be detected by placing all subjects on continuous 24-hour ECG telemetry monitoring for postoperative days 0 through 4.