It should seem David penned this psalm upon occasion of his deliverance, by the power and goodness of God, from some great and pressing trouble, by which he was in danger of being overwhelmed; probably it was some trouble of mind arising from a sense of sin and of God’s displeasure against him for it; whatever it was, the same Spirit that indited his praises for that deliverance was in him, at the same time, a Spirit of prophecy, testifying of the sufferings of Christ and the glory that should follow; or, ere he was aware, he was led to speak of his undertaking, and the discharge of his undertaking, in words that must be applied to Christ only; and therefore how far the praises that here go before that illustrious prophecy, and the prayers that follow, may safely and profitably be applied to him it will be worth while to consider. In this psalm, I. David records God’s favour to him in delivering him out of his deep distress, with thankfulness to his praise, Ps. 40:1-5. II. Thence he takes occasion to speak of the work of our redemption by Christ, Ps. 40:6-10. III. That gives him encouragement to pray to God for mercy and grace both for himself and for his friends, Ps. 40:11-17. If, in singing this psalm, we mix faith with the prophecy of Christ, and join in sincerity with the praises and prayers here offered up, we make melody wit our hearts to the Lord.