MIDRASH TEHILLIM
1.....(3.1)
"It is a joy for a righteous man to do judgment..." (Mishlei - Proverbs 21:15)
It may mean that when God executes judgment upon righteous men, it is a joy to them, because He cleanses them of their transgressions. As soon as the measure of God's judgment struck David, He began to rejoice and to compose a Psalm of David, when he fled from Absalom his son.
2.....(3.3)
When did David compose this Psalm?
When he went up by the ascent of the Mountain of Olives and wept as he went up (Shmuel II - II Samuel 15:30)
According to rabbi Yudan, this is what David said - Yakov fled, as it is said: "And Yakov fled to the field of Aram..." (Hoshea - Hosea 12:13), Moshe fled, as it is said: "...Moshe fled from before Pharaoh." (Shemot 2:15). Like them, I am fleeing. Upon this David recieved the verse: "I remembered Your judgments of old, O LORD, and I was consoled", as if to say - When I remember the measure of judgment which You did deal to those who lived before me, it comforts me. When did David speak thus? When he fled from Absolom, his son.