This is the most common problem I have seen with newbies & first time Glock owners. This includes LEOs. I had the same problem myself when I was a newbie a long time ago. I even had brass in the face every once and awhile. I sold my Glock 17 and bought a Colt 1911. I also did not have any problems when the G22s came into the market later on. I need to see your grip and how you fire the gun to be sure. Some ideas:
1. Shoot with the tip of your finger. The more the finger you use, the more left the muzzle will go when you pull the trigger.
2. Keep your left finger off the front of trigger guard. This will pull gun down and to your left.
3. Keep your thumbs straight and pointed at the target.
4. Do not have a death grip on the gun,it will not fly out of your hands.
5. Some students flinch when pulling the trigger because of the low weight of the polymer gun, coupled with the snappy 9mm. Put some dry fire rounds randomly in the magazine. See if you flinch low and to left when the gun does not fire. You said you do not have this problem with the .40. The .40 is not as snappy. Try a 10mm or .45 from Glock, I bet you will be surprised.
Well, these are some of the issues I have helped others with. Some or none may apply to you. Like I said, I would have to see you fire the weapon. Good luck!