A goldfish swimming upside down in an aquarium is widely consider to have a problem with its swim bladder. This is almost never the case. The real reason is gas that has inflated the intestinal tract. I’ll explain.
Goldfish do not have a stomach – yes, that’s right no stomach where food is traditionally digested. Instead they have a very long intestinal tract where their food is slowly digested. What happens is that the normal fish food they are given (flakes and pellets) are full of starch (from the grains, wheat and fillers used in the food) which fish cannot process. This starch slowly moves through the goldfish and basically becomes stuck with bacteria processing it and the bacteria produce a gas which fills up the intestine. As the intestine inflates, like a blimp, it eventually causes the fish to roll over upside down.
The condition can be fixed by feeding brine shrimp and peas that act as a laxative and clear out the blockage. Long term the fix is to feed a diet low in starch and high in vegetable protein. Vary the diet and if you must feed flakes or pellets get one specifically made for goldfish which should have little animal protein and wheat. Under no circumstances should you attempt to deflate the intestine. Anyhing like this should be done by a trained veterinarian.