Tucked inside this year’s New York State budget was legislation allowing Riverhead officials to save the town’s deeply indebted land-preservation program.

Three East End lawmakers attached language to the 2016-17 state budget that passed April 1 allowing Riverhead officials to restructure $46 million in debt and $12 million in interest racked up by town’s Community Preservation Fund.

Officials in Riverhead, home to much of Long Island’s remaining farmland, haven’t tapped the fund in six years because they say that, since the national recession, the fund’s annual debt payments have far outstripped its revenue.

Since 2010, Riverhead’s preservation fund has brought in an average of $2.7 million a year through a special tax on real-estate transfers, but has owed an average of $5.6 million a year in debt payments.

Town Supervisor Sean Walter said his predecessors borrowed heavily to preserve thousands of acres of land before the recession tanked Riverhead’s real-estate market after 2008.

Refinancing the debt over a period of 30 to 35 years, the supervisor said, could bring annual debt payments in line with revenue — and eventually allow the town to start preserving land again.

advertisement | advertise on newsday

Walter, in a statement, thanked state lawmakers for legislation he said “has helped us avoid a looming and real budget crisis.” State Sen. Kenneth LaValle (R-Port Jefferson) and Assembs. Fred Thiele Jr. (I-Sag Harbor) and Anthony Palumbo (R-New Suffolk) sponsored the legislation.

Deputy Town Attorney Anne Marie Prudenti, who said she has worked for more than a year to find a way to refinance the debt, said the legislation may have saved the town from one day having to dip into general funds to make payments.

“It’s huge,” she said.

Town Financial Administrator Bill Rothaar said the town’s Community Preservation Fund had about $12 million in it at the end of 2015.

The Community Preservation Fund program was created in 1998, when state lawmakers passed a bill allowing Long Island’s five easternmost towns to impose a 2 percent tax on most real-estate transfers to preserve farmland, natural spaces and historic sites.

While the funds have raised more than $1 billion and preserved more than 10,000 acres altogether, their success has varied by town. East Hampton’s and Southampton’s funds are flush with money; Riverhead’s has struggled to raise revenue since the recession.

The Community Preservation Fund program was scheduled to expire in 2030, but a 20-year extension of the program was signed into law in December. The extension is subject to a referendum in each town.

Walter said Riverhead residents must approve the extension in a November referendum in order for town officials to refinance the debt over a longer term.