Like Psalm 34, this Psalm deals with the seeming prosperity of the wicked and the suffering of the righteous. The Psalmist admits, I almost slipped in my faith when I thought about this (2-3). But when he went into the sanctuary (the presence of God), God showed him the eternal destiny of the wicked.
This revelation put the problem in the perspective of eternity (17-20) and reveals the blessing enjoyed by the righteous which is greater than any suffering (25-28).
The Psalmist almost slipped when his faith faltered but, in the end, it is the wicked who will slip and fall (2, 18) because their faith is not in God. As for the righteous, whatever suffering may come, God is his portion (26) and that is everything.