But the tradition of prayer for the dead had already been firmly established. So Purgatory is added to the picture, so that we can still do good for Uncle Joe after he died. Purgatory is a logical conclusion based on the ideas that reward or punishment follows immediately upon death, and yet prayer for the dead is effective.
There is an old saying (Latin): "lex orandi, lex credendi" -- "The law of prayer [is] the law of belief." Much Christian doctrine is the result of how we pray. The doctrine of Purgatory arose from the far more ancient practice of prayer for the dead.