This could be "Critical Threshold" by Brian Stableford, part of the Daedalus series, the premise of which is a ship sent to reestablish contact with colonies and, if necessary, find out what problems they're having and help them fix it.
A review covers many of the initial points, a colony that's descended to primitive levels and a fence/wall around it:
But even before the Daedelus lands, it becomes very clear that Dendra is a world that has fallen below said critical threshold. The colonists number fewer than a hundred after a century and a half, and not only are they gaunt and sickly-looking and appear even a bit mad, they're still wearing the tattered rags of the 150-year-old uniforms the original settlers wore! No one on the Daedelus crew can find any trace of the original settlers' databanks, and, most curiously, there is a wall seven miles long that surrounds the puny area where the survivors live. But is the wall to keep something frightening in the forests out, or the colonists in? Biologist Alexis Alexander theorizes the latter. Something is rotten on Dendra. What could it be?
It leaves out the solution to the mystery itself, so I'll just present the scene where the biologist viewpoint character figures it out:
"What the hell happened?" moaned Karen.
"Springtime," I said. "The mating season."
She didn't get it. I wasn't really surprised.
"The butterflies," I said. "In the forest, there isn't any seasonal cycle. The insects don't have internal rhythms to regulate their life cycle. Instead, they spend the greater part of their existence in the innocent business of living, until a build-up happens in a particular place--the kind of place we passed through yesterday. When population density hits a certain level it triggers the release of pheromones, which attract more insects from outside, which builds up the density, which results in the release of more pheromones, and so on. Result--one hell of a crowd. Or perhaps I should say cloud. Millions of the things filling the air with their pungent odor--sex hormones, to attract mates by the million. An olfactory explosion. Instant orgy. The mating dance of the whole damn generation, packed into one small area, one brief moment bang!"
"The savages...," she began.
I interrupted. "They triggered it. Deliberately. Those things they carried were cages with butterflies in them. They brought a crowd together... to critical mass. They weren't just doing it to promote a forest love affair. They get high on that drug. To them, what we experienced was a pleasant experience--a big kick. Any time they can pick up enough sex-starved butterflies...I don't know how often. Once a month, once a week."