Q. A person who suffers from ‘shy bladder’ (an anxiety disorder) and is unable to relieve himself when other people are around (in the bathroom or even in the building) even though he feels the need to relieve himself – may such a person daven while feeling the need to relieve himself?

A. There are a number of more common conditions that may cause a person to feel frequently or constantly the need to relieve himself, such as urinary track or kidney infections, bladder stones or cancer, overactive bladder syndrome, age etc. Poskim permit davening when otherwise one would not be able to daven at all.
On a similar question (443) we wrote: “In regards to a patient that suffers from urinary incontinence and he constantly drips and would therefore be unable to pray or recite sh’ma forever, Mishna Berura (80: 4) quotes the Remoh (teshuvo 98). He writes that one should place a cloth or sponge around one’s organ, and then it is as if the urine is contained in a pocket, as he has no other choice it is permitted, if he could control himself at least at the beginning of the prayer. Melamed Lehoil (O.H. 7 seems to permit even when he is unable to stop at all, Chelkas Yaakov (O.H. 31) as he understands from Chaye Odom, agrees. However, Mishne Halochos (2: 61) prohibits. Horav Shlomo Miller’s Shlit”a opinion is that if the patient can feel when he discharges urine, he should at that time stop his praying or learning. If he is unaware, he is still permitted to pray and learn Torah.”
On your particular question, when the patient is actually dry and clean, yet he feels a need to relieve himself that he cannot easily attain, Horav Shlomo Miller’s Shlit’a opinion is that he should relieve himself prior to the starting of the tefila. If later on while already in the middle of the shema or amida he feels a need again, if by the time he exits the shul and returns, the tefila will be over and the hefsek is Halachic unacceptable, he may continue and end davening as is.

Rabbi A. Bartfeld as revised by Horav Shlomo Miller Shlit”a