In your faithfulness answer me, in your righteousness! Enter not into judgment with your servant, for no one living is righteous before you. Psalm 143:1-2
David called on God's righteousness to show him mercy. What a strange righteousness this is! In the very next verse David admits that he's not righteous, that he is in fact liable to God's righteous judgment. In other words, David doesn't deserve God's mercy at all. He deserves God's righteous condemnation.
You would think a sinner like David, conscious of his own unrighteousness, would stay as far away from God's righteousness as he could. But David knew better. He knew that because he had no righteousness of his own, God's own righteousness was his only hope. Only God can satisfy his own demands in the place of sinners, in other words justify us—the very thing he has done though his Son.
But there's something even deeper here, and here David shows us what a massive grasp he had on the gospel. Once you are justified in Christ, God's righteousness then calls out, not for the judgment you deserve, but for the mercy you don't, which Christ earned for you. If there's any doubt, I will call an Apostle to the witness stand: "If we confess our sins, he is faithful and just to forgive us our sins and to cleanse us from all unrighteousness." (1 John 1:9).
When sinners trust in Jesus, it's a matter of God's strict justice that he forgive them and cleanse them and count them righteous and keep them in his mercy forever. God's righteousness, the very thing that secured our condemnation, now secures his eternal faithfulness to us. In a manner of speaking, in the gospel his righteousness becomes his faithfulness to us. If you trust in Jesus, it would be unrighteous for God to deny his mercy to you. In other words, perish the thought!