Background Despite a lack of demonstrated efficacy, potassium and magnesium supplementation are commonly thought to prevent postoperative atrial fibrillation (POAF) after cardiac operation. Our aim was to evaluate the natural time course of electrolyte level changes after cardiac operation and their relation to POAF occurrence. Methods Data were reviewed from 2,041 adult patients without preoperative AF who underwent coronary artery bypass grafting, valve operation, or both between 2009 and 2013. In patients with POAF, the plasma potassium and magnesium levels nearest to the first AF onset time were compared with time-matched electrolyte levels in patients without AF. Results POAF occurred in 752 patients (36.8%). At the time of AF onset or the matched time point, patients with POAF had higher potassium (4.30 versus 4.21 mmol/L, p < 0.001) and magnesium (2.33 versus 2.16 mg/dL, p < 0.001) levels than controls. A stepwise increase in AF risk occurred with increasing potassium or magnesium quintile (p < 0.001). On multivariate logistic regression analysis, magnesium level was an independent predictor of POAF (odds ratio 4.26, p < 0.001), in addition to age, Caucasian race, preoperative β-blocker use, valve operation, and postoperative pneumonia. Prophylactic potassium supplementation did not reduce the POAF rate (37% versus 37%, p = 0.813), whereas magnesium supplementation was associated with increased POAF (47% versus 36%, p = 0.005). Conclusions Higher serum potassium and magnesium levels were associated with increased risk of POAF after cardiac operation. Potassium supplementation was not protective against POAF, and magnesium supplementation was even associated with increased POAF risk. These findings help explain the poor efficacy of electrolyte supplementation in POAF prophylaxis.