Atrial fibrillation (AF) is the most commonly sustained arrhythmia after pulmonary resection, which has been shown to predict higher hospital morbidity and mortality. The lack of strong evidence-based medical evidence makes doctors have very few options for medications to prevent new-onset AF following thoracic surgery. Magnesium can prevent perioperative AF in patients undergoing cardiac surgery. However, this has not yet been fully studied in patients undergoing non-cardiac thoracic surgery, which is the aim of this study. This is a single-center, prospective, double-blind, randomized controlled trial. In total, 838 eligible patients were randomly assigned to one of two study groups, namely, the control group or the magnesium group. The patients in the magnesium group preoperatively received 80 mg magnesium sulfate/kg ideal weight in 100 ml normal saline 30 min. The control group received the same volumes of normal saline simultaneously. The primary outcome is the incidence of new-onset AF intra-operative and on the first, second, and third postoperative days. The secondary outcomes are bradycardia, hypertension, hypotension, and flushing. The occurrence of stroke or any other type of arrhythmia is also recorded. Postoperative respiratory suppression and gastrointestinal discomfort, intensive care unit stays and total duration of hospital stays, in-hospital mortality, and 3-month all-cause mortality are also recorded as important outcomes. This study aims to prospectively evaluate the prophylactic effects of magnesium sulfate against AF compared with a placebo control group during and following anatomic pulmonary resection. The results may provide reliable evidence for the prophylactic value of magnesium against AF in patients with lung cancer. The trial was approved by the Clinical Research Ethics Committee of Shanghai Pulmonary Hospital and has been registered at Chinese Clinical Trial Registry: www.chictr.org.cn, identifier: ChiCTR2300068046.