Magnesium deficiency, which is known to be highly prevalent among patients with diabetes, has been associated with insulin resistance and poor glucose control. Here, we aimed to investigate the effects of intraoperative magnesium administration on postoperative glucose control in patients with diabetes. We retrospectively reviewed the medical records of patients with type 2 diabetes who had undergone total joint arthroplasty at a tertiary hospital, where intraoperative magnesium sulfate injections were frequently performed for postoperative analgesia. The patients were grouped based on whether treated with magnesium or not (magnesium vs. control groups). We investigated postoperative blood glucose levels and sliding scale insulin requirements. After propensity matching, 170 patients were allotted to each group. Both the mean glucose level and the incidence of a mean glucose level of >200mg/dL were significantly lower in the magnesium group than in the control group ( = 0.040 and 0.013, respectively). There was also a lower insulin requirement in the magnesium group ( = 0.043). Multivariate logistic regression revealed that magnesium treatment was significantly related to a less frequent incidence of a mean blood glucose level of >200 mg/dL ( = 0.047). This study demonstrated that magnesium sulfate infusion was associated with an improved postoperative blood glucose profile in patients with diabetes.