Spain's socialist government has struck a deal with the country's trade unions to raise the age of retirement.
All parties have agreed that the compulsory retirement age will increase from 65 to 67, one of the highest in Europe.
A final, marathon discussion ran into the early hours of Friday morning.
The deal is a crucial step to convince investors the government is committed to structural reform to revive the economy and avoid a bail-out.
Aware it was under international scrutiny, the government had threatened to impose the changes by decree if necessary. Spain's trade unions had warned of another general strike.
Since early January, the two sides have been holding long weekends and late nights of talks.
With a low birth rate and increasing life expectancy, government figures suggest 32% of Spain's population will be aged 64 or over by 2050. The number of pensioners is expected to rise from eight million to 12 million over the next 3 decades.
Under the current system, pension payments account for around 9% of public spending. By 2040, that figure could rise to 14%, according to the Economy Ministry.
Unemployment is also straining the system. Figures released by the national statistics institute on Friday revealed that the number of people out of work had surged to a 13-year record high of 20.33% in 2010.
Last year, for the first time, the social security system was not in surplus.
A social security crisis is looming in Spain, as elsewhere. So despite disagreement over the details, there is broad consensus about the need for change.
"If the reform is finally approved, the government will have chalked-up a success and the unions will not have taken a step back," was the reflection of an El Pais newspaper editorial on Thursday, which calculates that the reform maintains the solvency of the system.
"The general acceptance of the reasons for the change, and the spirit of negotiation, allow us to be optimistic," the paper concludes.
Not everyone accepts the deal, though.
The first real protests erupted on Thursday as a demonstration against the reforms by far left-wing groups turned violent.
Riot police clashed with dozens of young protestors who overturned and burned rubbish containers, threw firecrackers and smashed the glass facades of several banks. El Pais reported that eight police officers were injured.
Last week, trades unionists staged sit-ins at social security offices.
By reaching agreement with Spain's most powerful unions, though, the government appears to have deflected the danger of major social unrest. It is particularly keen to avoid another general strike, with local elections looming in May.
But it has been forced to row back somewhat from its initial proposal for reform.
It had insisted that to retire on a full pension, a person would have to work and pay into the system for 41 years, up from 35. The unions pushed for 38. The final agreement is 38.5.
The age of retirement will also increase progressively, from 2013, so that those close to pension age now will not be affected.
Trade union leaders stress that they reserve the right to make further alterations to the reforms before their final approval by parliament.
But it has been agreed that pensions will now be calculated on the basis of the last 25 years of a worker's earnings, instead of the current figure of 15, a system that one economist had described as "not just generous, but silly".
The reform appears daunting to some of Spain's younger generation. Forty per cent of young people here are unemployed; many are approaching the age of 30 without work, never having paid into the social security system.
"For me, the outlook is pretty bleak. It's extremely difficult to find a proper contract," explains Zohartze Sagastagoya, 28, who is an intern at a Madrid radio station.
"I know people who are 30, or even 35, who are still not paying any social security because they don't have proper jobs," she told the BBC.
"In this crisis, I don't know when I can start contributing, or how I can possibly work 38.5 years to get a pension."
Others argue that maintaining the status quo would be far worse. The government would have to tax the future earnings of that same young generation very steeply in order to fund the pensions of their elders, who are now living far longer after they retire.
Although the impact on Spain's budget deficit will not be seen in the short-run, the reform is likely to be welcomed by investors, but some analysts believe it could have gone further.
"The government is carrying out minimalist reforms to survive," believes Michele Boldrin, research fellow with the economic think-tank, FEDEA.
He believes the same is true for pensions as for the government's earlier labour and banking reforms, that they were intended not to provoke too much unrest.
"They could have done more, done better, gone deeper. This way, they can make it to the general elections in 2012. But it's not enough to resurrect the economy and fly."