I remember reading that Tzaddikim will get up by Techiat Hameitim right when Moshiach comes and everyone else 40 years later. What is the source for this?
According to Rav Uriel Eitam (footnote 5), Rav Saadia Gaon in his Emunoth V'Deoth, treatise 7, seems to interpret that there will be two separate revivals of the dead, a first one to reward Israel, and a second one to punish the wicked.
According to an article on the topic in Chabadpedia, the Zohar (1:139:1), says that the ingathering of the exiles will precede the revival of the dead by 40 years. The Chabadpedia article likewise claims that a revival of the righteous at the beginning of the messianic era is derivable from Yoma 5b.