ALBANY — It will take many years and billions of dollars to fix aging municipal sewer systems that spill sewage into waterways after it rains, state Comptroller Tom DiNapoli warned Thursday.

Each year, so-called combined sewer systems (CSOs), which contain both storm water and municipal sewage, are overwhelmed by heavy rains, which causes billions of gallons of untreated sewage and rainwater to spill directly into rivers and streams.

Such CSO systems are in place in Albany, Troy, Cohoes, Rensselaer, Watervliet and the village of Green Island. The communities are in the midst of a $147 million rebuilding plan aimed at reducing such spills into the Hudson River by 85 percent.

State officials advise that people should avoid coming into contact with river water for several days after a heavy rain.

Recent federal studies have estimated it could cost more than $5 billion to fix the CSO systems spread across 46 municipalities in the state, including New York City, Buffalo and Rochester.

"The ultimate financial and operational responsibility rests with the local governments that have these systems," wrote DiNapoli. "Local officials must take a long-term approach. ... State and local governments must remain focused on addressing the challenges of aging infrastructure through continued funding for these priorities, thoughtful capital planning and more sustainable development."