The economic recession cost states hundreds of billions of dollars in lost tax revenue and saddled them with more than half a trillion dollars in new debt over the last five years.

New figures from the U.S. Census Bureau show state and local governments added about $533 billion in new debt between 2007 and 2012, an increase of more than 20 percent. The debt came from a combination of lost tax revenue and higher spending as states tried to rebuild their teetering economies.

California and Texas, two states hit hard by the recession, added more debt than any other. California’s debt ballooned from $330 billion to $419 billion, while Texas’s obligations grew from $186 billion in 2007 to $270 billion in 2012.

Only two states — Alaska and Montana — managed to shrink their total state and local debt load during the five-year period. Both states benefited from an energy boom that gave them huge influxes of cash even as their neighbors struggled with falling revenues and rising debts.

Arkansas, Iowa, Nebraska, Nevada, South Carolina and North Carolina all cut their state debt loads, but localities within those states added enough debt to wipe out those positive steps.

Almost all of the $2.9 trillion in debt U.S. state and local governments carry is long-term debt. Municipal governments carry almost $700 billion in debt, while counties and school districts owe between $300 billion and $400 billion.

State and local governments collected $1.4 trillion in taxes in 2012, up 8.2 percent over the 2007 number. But taxes make up just 53.4 percent of state and local revenues, and other revenue sources declined over the five year period. Total revenue declined by about $100 billion over the five year period, to $3 trillion in 2012. Revenues generated by taxes on employee retirement fell by more than two-thirds, and earnings from interest fell by more than 40 percent.

“State and local government revenues continue to be impacted by capital market fluctuations, especially employee retirement revenue,” Kevin Deardorff, the Census Bureau’s Economy-Wide Statistics Division chief, said in an analysis of the new data.

Expenditures, on the other hand, continued to rise. State and local governments spent $3.2 trillion in 2012, half a trillion dollars higher than the $2.7 trillion states spent in 2007, before localities began feeling the brunt of the recession.

Education expenditures, which make up more than a quarter of state and local expenses, rose more than 12 percent, to $896 billion in 2012. Spending on hospitals rose more than 30 percent, and overall health spending rose 13.6 percent. Unemployment compensation grew more than fivefold in Colorado, Texas, Utah and Arizona, the survey found.

At the same time, localities have shed hundreds of thousands of employees. In March 2012, local governments reported 224,000 fewer employees than they had in 2007. Many of those employees who lost their jobs were lower-wage employees: State and local government payrolls increased by between 9.6 percent and 13.4 percent over the same period.