A thought from the holy Reb Tzadok of Lublin, in his work Tzidkat HaTzadik:[1]
The internal pain (agmat nefesh) that an individual receives from the sins that he did, is literally the punishment of purgatory for the sin. It is for this reason that the Talmud (Berachot 12a) states: “If one commits a sin and is ashamed of it, all his sins are forgiven for him.”
An individual who is meritorious is reminded from Heaven about his sin—when he reaches the amount of pain that he must suffer, his sin is forgiven. This is the propitiation for sin that the righteous receive in this world—for the punishment of this world is in an entirely different measure than the punishment of the World to Come.
It is for this reason that King David stated (Tehillim 23:4): “Even when I walk in the valley of darkness, I will fear no evil, for You are with me.” He surely did not think that he would receive punishment in death, since he referred to himself as righteous, as he stated (Tehillim 86:2), “Watch my soul for I am a pious man.”
Rather, King David was referring to Hell on earth. For, when a person is constantly depressed and embittered, it is a veritable hell. David did not fear this depression. For, although true that depression causes the Divine Presence to leave a person[2] and can be a catalyst for evil, he knew that even there—in the darkness—the level of G-d referred to as “You” was with him.
[1] #57.
[2] Talmud, Shabbat 30b.