Influence of Mg doping on structural defects in Al0.13Ga0.87N layers grown on sapphire substrates by metalorganic chemical vapor deposition were studied using transmission electron microscopy. By increasing the Mg source flow rate, the reduction of dislocation density occurred up to the Mg source flow rate of 0.103 mu mol/min. While the vertical type inversion domain boundaries (IDBs) were observed in the Al0.13Ga0.87N layers grown with the low Mg source flow rate, the IDBs in the Al0.13Ga0.87N layers grown with the high Mg source flow rate have horizontally multifaceted shapes. The change of polarity by the IDBs of horizontal type also resulted in the 180 degrees rotation of pyramidal defects within the same AlGaN layer. (C) 2001 American Institute of Physics.