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.

In a down market, “not only is the state getting less money from the way the tax structure is comprised, but it also forces them to increase their payments for their unfunded pension liability,” said Howard Cure, head of municipal research in New York at Evercore Wealth Management, which oversees $6.2 billion of investments. “It compounds the vulnerability of their tax structure.”

There is a limit to how high the state’s contributions could rise: 0.5 percent annually. That could slow the state’s progress toward eliminating the pension shortfall if the markets reverse course after a period of good years that allowed California to cut its contributions, said Ryan Miller, principal fiscal and policy analyst at the legislative office.

”We have a very volatile revenue system,” Miller said. “It’s possible that we could lose tens of billions of dollars over a multi-year period following a recession and that might cause these sorts of constraints in a budget where we couldn’t address this problem.”

As state leaders deliberate on the budget for the fiscal year that starts in July, Brown has advocated fiscal restraint. The governor “is not going to support ongoing higher levels of state spending that broadly speaking could be susceptible to a downturn in revenues,” Palmer said.

The state should act now, while revenue is surging, to make its teacher contributions simpler and less susceptible to market volatility, Miller said.

“Its role here is going to be far less certain than the districts’ role,” Miller said. “In some scenarios, the state’s share of this problem could be very, very big.”