David is announcing a recent deliverance executed by the Lord his God. The deliverance story begins with him mentally recanting the past, other times when he waited for the Lord to hear his prayer for deliverance. When trouble befell he waited for the Lord’s answer. While doing so, just as in this time his memory resurged to times past when the Lord heard his request. David had formed a good habit when life became troublesome.
David remembered that once he either fell or was pushed into a slimy pit. He started walking or running in the mud. The mud turned to mire. The mire led to a slimy pit. He found himself trapped. He sank into the earth. The distance between the smelly muck and his distressed face shrank. He prayed as the likelihood of death increased with the decrease in distance between face and mud. Whether the pit was real or a poetic analogy of life events does not matter. For David, it was a slimmy pit that the Lord lifted him out of. The Lord placed him on the rock. He once again had a firm place to stand.
Remembering past deliverance encouraged David. His faith in the Lord was reassured because other times the Lord rescued him. As the Lord had done before, so he would do again.
When the Lord rescued him from the slimy pit and placed him on the rock David wrote a song to inspire others, perhaps Psalm 38 or 39. A new song was placed in his mouth. A hymn of praise was sung to the God of David and Israel. The song led people to see that the Lord saved and they feared. Others put their trust in the Lord. So David once again wrote a psalm, this psalm. Praise must follow deliverance as surely as prayer springs from need if God is to be truly honored.