Protein O-GlcNAcylation, a monosaccharide posttranslational modification maintained by two evolutionarily conserved enzymes, O-GlcNAc transferase (OGT) and O-GlcNAcase (OGA), is a major nutrient sensor integrating key metabolic pathways. While mutations in OGT have recently been associated with neurodevelopmental disorders, the dynamics and function of protein O-GlcNAcylation during early embryogenesis remain elusive. Here we develop a new fluorescent probe to visualize O-GlcNAcylation levels in live Drosophila early embryos. Our study shows that protein O-GlcNAcylation declines as the embryos develop to the mid-blastula transition when the facultative heterochromatin makes its first appearance. Lowering O-GlcNAcylation levels by exogenous OGA activity promotes the polycomb group O-GlcNAc protein Polyhomeotic (Ph) to form nuclear foci and K27 trimethylation of histone H3. This enhanced facultative heterochromatin formation fine-tunes the expression of several neurodevelopmental genes including short of gastrulation (sog). We provide evidence that perturbation of O-GlcNAcylation during early embryogenesis affects learning ability in adulthood, highlighting the importance of O-GlcNAcylation homeostasis for the development of the nervous system.