This is God’s care for His righteous. We know well that David went through many afflictions when he was young, since he shared this with his men while they were in a cave, being chased by King Saul.
God helps those who do His will and always walk in righteousness. He grants them victory in all circumstances. The greatest victory that the psalmist David could have had was when God allowed him to have King Saul in his hands, but even so, David did not act against him, rather, he allowed God to act.