David's prayers based on personal righteousness

Ever felt a little uncomfortable who understands the gospel of grace, that David often appeals to God on the basis of his own righteousness? Rescue me because I'm good sounds more like the Pharisee than the tax-collector!

Well I gotta test this out, but Calvin has an interesting take on this in his commentary on Psalm 4:

He did not boast of his own righteousness except in reference to his enemies, from whose calumnies he vindicated himself. He had the testimony of a good conscience that he had attempted nothing without the call and commandment of God, and therefore he does not speak rashly when he calls God the protector and defender of his right.