Another favorite from childhood - I always loved this Psalm which we used to sing to Spohr: later it enthralled, during messianic studies, when I noticed it in Hebrews.
It helps to remember, as we try to grasp its meaning, that it reflects David's own experience, in union with Messiah, as the Anointed of the LORD. As the title says:
Psalm 40: To the Choirmaster. A Psalm of David.
So what experience does the king recollect and proceed to celebrate? I seems a snug fit with the son of Jesse's twin experiences of rescue from Saul and, later, Absalom. Alternatively, the first rescue recounted could be the muddy slimy pit of adultery and murder, which led to the rebellion that cause his exile from the Kingdom, and leads to the second plea. Probably the reason no history is given is to broaden the application to the saints whom God drags out of every kind of ditch or dungeon.
Just a quick comment about the lay-out may be helpful: if you were to ask, I would divide it in five sections - the middle part is key and, significantly, the most tricky.
A - Rescue No 1 - Verses 1-3
B - Rejoicing - Verses 4-6
C - Resolution - Verses 7-11
B`- Reproach - Verses 12-13
A`- Rescue No 2 - Verses 14-18
Undivided devotion and total consecration to the will of God for David both stems from his first rescue and causes the reproach that necessitates final rescue.
Rescue No 1 - Verses 1-3
David had to wait for ages till God stooped down to answer - until, like Jeremiah or Joseph, he was hauled out of his pit, set on rock to sing and stir awe in the church.
I waited patiently for the LORD; he inclined to me and heard my cry. He drew me up from the pit of destruction ...set my feet upon a rock ...put a new song in my mouth ...many will see and fear.
Is there sin or suffering? Whatever pit we face, don't think he doesn't see, and if you pray he'll hear - child he'll lift you up, when cast down, for Jesus sake.
Rejoicing - Verses 4-6
God's miraculous acts - he delights in deliverance - rejoice sovereign and subjects - in an evangelistic, ecstatic, state, they can't enumerate all God's jaw-dropping work.
You have multiplied, O LORD my God, your wondrous deeds and your thoughts towards us: none can compare with you! I will proclaim and tell of them - yet they are more than can be told.
Has God granted grace? Then, dear friends, for conversion or consecration, for redemption or restoration, from hardship or heartache - give the LORD all glory!
Resolution - Verses 7-11
David knows well the LORD doesn't want mere rites - God's king must must be all ears, copy out Moses' Law, do it from the heart, which is just what this soul craves:
Then I said "Behold I have come; in the scroll of the book it is written of me: I desire to do your will, O my God, your law is within my heart."
That's why Heb. 10:5-7 says David spoke in Christ: as Jesus enters earth, he set his face like flint to do all prophets wrote of him and execute God's will to death.
Reproach - Verses 12-13
Radical obedience to God comes with a costly cross - if Jesus must be impaled for sin that isn't his, which God imputes to him, David admits this mess is all his own.
For evils have encompassed me beyond number; my iniquities have overtaken me, and I cannot see; they are more than the hairs of my head; my heart fails me.
Chastening brings growth if sinners cast themselves on God - yes, we should weep for what our sins cost Christ, but also run for mercy, for he drowned in guilt for us.
Rescue No 2 - Verses 14-18
Sentence served, David seeks aid: Recompense my foes (who mocked 'Aha! Aha!')! Rejoice the saints (who seek salvation in God)! Relieve your impoverished prince!
But may all who seek you rejoice and be glad in you; may those who seek your salvation say continually "Great is the LORD!" As for me, I am poor and needy, but the LORD takes thought for me. You are my help and my deliverer; do not delay, O my God.
Has Jesus paid sin's debt? Even those who mock us can have their sin white-washed - all seekers of salvation can find delight in God and be delivered when in need: such is blood-bought blessing for those in covenant with God. This is the fruit of Christ's plunging in our pit - this the miraculous rescue that all saints should shout about.
Did God stoop to save? Tell it in the Church! Did God pull from pits? Spread good news abroad! Are you feet on rock? Devote yourself to God! Were you in a mess? Give thanks you now confess! Was your suffering mocked? Pray God might forgive! Is your memory hazy? Muse on miraculous acts! Seek his heart in order to commit!