It’s hard for states to fix pensions – just look at New Jersey and Illinois. And even when legislators in California agreed two years ago on a way to keep the teachers’ retirement fund from going broke, it left the state’s finances more exposed to financial-market swings.

Under the plan, California, school districts and teachers will gradually increase their contributions to make the second-biggest U.S. public pension fully funded in 30 years. The catch: the state’s share depends on whether the fund hits its 7.5 percent annual investment target, meaning it could jump in bad years or plummet in good ones, according to the Legislative Analyst’s Office. Three decades from now, the difference between market winning and losing streaks could mean as much as $5 billion.

The uncertainty over the state’s future payments to the California State Teachers’ Retirement System, which oversees benefits for about 879,000 workers, underscores the risks in betting on a continual surge in California, where the government’s fortunes have tracked the booms and busts of the equity market. For now, California’s still reaping a revenue windfall from the post-recession bull run that crested in the middle of last year.

“This could certainly add to fiscal pressure on the state in the event of a down market,” Gabriel Petek, a credit analyst in San Francisco for Standard & Poor’s, said of the pension overhaul. In July, S&P boosted the state’s rating to AA-, the fourth-highest rank, marking the third upgrade in as many years.

While the investment returns don’t start affecting the state’s contribution until July 2017, the lackluster performance in 2015 highlights the potential pitfalls. After stocks tumbled from record highs, state and city pensions eked out median returns of 0.36 percent, according to the Wilshire Trust Universe Comparison Service. It was the smallest increase since 2008 and far short of what they expected.

Nationwide, state and local governments have about $1.7 trillion less than needed to cover all the benefits that have been promised, according to Federal Reserve Board data. The need to boost contributions has led to downgrades for Illinois and New Jersey, where Democrats and Republicans have clashed over how to meet the growing obligations.

Before California legislators acted, the teachers’ system was facing a $74 billion shortfall. Unlike most public pensions, the board needs lawmakers’ approval to require school districts, the state and employees to increase what they pay into the fund. Because of the 2014 changes, the payments from school districts will more than double to 19.1 percent of payroll by 2020 from 8.25 percent. By July 2016, employees would kick in 10.25 percent of their checks, up from 8 percent, and the state’s portion grows to 6.3 percent of payroll.

Beginning in mid-2017, California would pay less if the system bests its earnings assumption and more if it falls short, due to a formula that divides the responsibility for the unfunded liability between the state and districts, according to the Legislative Analyst’s Office, a nonpartisan agency that conducts research for lawmakers.

Any such impact from the pension-overhaul law can be mitigated because it allows for the governor and legislators to review the system’s funding every five years to see if any corrections should be made, said H.D. Palmer, a spokesman for Governor Jerry Brown’s finance department. Another buffer: a successful ballot measure that Brown championed requires the state to save a portion of excess capital-gains revenue to cushion the impact of swings that dogged the state in the past.

Those fluctuations can be dramatic. In the fiscal year that ended in June 2010, its capital-gains taxes tumbled to $3 billion, one third of what they were two years earlier, only to rebound when share prices jumped. In the current budget, California’s expecting $13.4 billion from such levies, about 11 percent of its revenue. Some will be socked away to help cover the next round of deficits.