H.F.
Is it only when you are trying to potty train? In any event, I would give the pediatrician a call and let him/her know what is happening. Then try to get your son to relax. If it is only when he is potty training, he may be having some nervous issues that are affecting his stomache. Also, look for a book called "Every Body Poops". It helped with my cousin when she refused to poop when she was potty training. She would hold it as long as she possibly could which led to constipation. I remember her having to go so bad that she bit down on a picnic table to help herself work it out when we were camping when she was a little over 2 and still in diapers but starting to potty train. My sister bought her the book for her birthday and like I said, it helped a little but sometimes it is mostly patience and lots of talking through it with the child to really get through it.
If he will go in a diaper or pull up, I'd suggest putting one on him to poo in BUT have him sit on the potty while he does it. Then try once every other day or once a day to go without the pull-up/diaper. Maybe this will help him transition a little better to the potty without him being constipated all the time.
I wish I could give you some magical advice that would make it all happen overnight but I don't think there is any. Like I said, give the Pediatrician a call just to make sure there isn't any other problem contributing to it and ask him for a little advice. He might suggest giving him some Miralax (which a friend uses for her girls who have constipation issues).
Good luck.