It can be a sign of pain, discomfort or irritation.
One of the only time a horse naturally swings t's tail during riding is in a changement.
– But It can come from many different things, such as pain, too high use of spurs or a poorly adjusted saddle.
If he does it really often, you should call an osteopath. He can have some vertebras stuck in the back, which is painful!
I agree, it can be a sign of pain! But I have heard it can also be a sign of joy! Horses are easy to understand, right? :p
It can have many significations actually! It can be a pain in the back especially, or in the teeth, in the limbs... Maybe you acts too much with your hands or legs? Or maybe you ride with spurs or a whip?