Brazil's Senate has passed the final amendments to a much-debated pension reform a day after the main text of the bill was approved.
The approval of the bill is a major victory for President Jair Bolsonaro after decades of failed attempts to reform Brazil's social security system.
Had it not been passed, public debt would have skyrocketed, official data suggest.
Mr Bolsonaro said the reform would allow the country "to take off".
The president had staked much of his political reputation on passing the bill, keen to show he possesses the negotiating skills to push it through Congress.
What will change?
The main change is the creation of a retirement age - 65 for men and 62 for women. Other changes include an increase to workers' pension contributions and the mechanism to calculate benefits.
Teachers, federal police, prison guards and rural workers are among those who will have special rules.
The changes are expected to result in savings of 800bn reais ($195bn; £150bn) over the next 10 years.
They will be phased in over 12 to 14 years. Nothing changes for those who are already retired.
Why is the reform key?
The high costs of the pension system are the main cause of Brazil's huge budget deficit. Brazil's public finances have been further hit by weak growth and falling tax revenues, making savings imperative.
President Bolsonaro proposed the reforms back in February saying he could deliver where many previous governments had failed.
An ageing population means that without change, Brazil was looking at spiralling costs.
Why has it been so tricky?
The reform requires a change to the constitution and in order to get that approved, two thirds of Brazil's Congress needed to give their approval.
Members of Congress often drive a hard bargain demanding that the government offer them something in return for their vote, such as changes to other policies or investment in their areas.
Pension reforms have been attempted by past governments over the course of the last 30 years and until now, they had all failed.
President Bolsonaro's proposed reform has been watered down along the way. The original bill had envisioned savings of 1.237tn reais over 10 years and that came down to 800bn reais.
But the fact that lawmakers threw their weight behind the bill even after unions called a general strike in June is a victory for President Bolsonaro.
What has been the reaction?
When the Senate approved the main body of the text on Tuesday, President Bolsonaro tweeted: "Congratulations Brazilian people! This victory, which paves the way for our country to finally take off, is yours! Brazil is ours! GREAT DAY!".
The president of the Senate, Davi Alcolumbre, said that "Brazil today, with this vote that will bring us results, shows greatness".
Brazilian markets rallied and stocks hit an all-time high.
But the country's main trade union, CUT, criticised the reform, saying the "cruel changes" will result in cuts to benefits and make access to pensions harder.