Abdullah bin Arqam narrated that the Messenger of Allah, peace and blessings be upon him, said: "When the time for prayer comes and a person needs to relieve himself, he should go to the toilet first."
Brief Explanation
(Commentary on Hadith 1464)
That is, one should first relieve oneself and then proceed to prayer, because in such a situation there will not be concentration in the prayer.
There is an authentic hadith: «لَا صَلَاةَ بِحَضْرَةِ الطَّعَامِ، وَلَا هُوَ يُدَافِعُهُ الأَخْبَثَانِ» "One should not perform prayer when food is presented or when there is an urge to relieve oneself of stool or urine."
[صحيح مسلم : 560].