So...
Based on the quality of the soil, availability of water, crop lost to pest or pestilence, and diet of the people in question...
A family of 4 can grow everything they need to survive on about an acre of land.
This is particularly true in River Culture regions, where plentiful fishing and hunting were also easily accessible since humans controlled the water source other animals needed to survive. If you live at a River Delta (Particularly a -large- river delta, like the Mississippi or Nile) you'll wind up with incredibly fertile soil in abundance, lots of non-agrarian food options, and minimal need for Livestocking to maintain a comparatively easy life to, say, a farmer in Iowa.
Add in a long growing season based on relation to the equator and you get massive and plentiful yields from a fairly small square footage.
This is why Mesopotamia existed. It's why the Mississippi River Valley Cultures and Egypt as we know it existed. Indus River Valley Culture? Same deal.
Because when you scale production -beyond- what you need to survive in those places, you get massive quantities of excess that you can then use for trade.
And throughout history: Farmlands -were- protected. Not just by fences, though fences were freaking MANDATORY to minimize pest-loss, but also by things like 3-5 foot high stone walls and low stockades meant to make it harder for larger herbivores or attackers to get in. Not impossible, mind you, many a viking likely vaulted over a low stone wall and passed through fields toward churches or farmhouses... but harder.
And in places where these farmlands were communally held, they often were protected by more drastic measures, not just earthworks, but full sized stockades. And that's in -our- world.
In a world with Dragons and Ankhegs and Orcs it's perfectly reasonable to redesign society to protect their fields with high stone curtain walls, or to place the fields in the center of town with a "Donut" of city around it to form a protective barrier.
You're not limited to a medieval Europe stand-in with D&D. If something agrarian doesn't make sense to you, change how it works.
Though I would also like to note that most European kingdoms in history kept reserves of long-lasting foodstuffs like grain within the city walls so that even during years-long sieges they could maintain their populace. And let's not forget Heraklion which endured 21 years of siege. 16 of which had fairly continual shelling of the city in the 1640s and 50s.
Ever see the Baron Munchausen movie. It's the inspiration for the opening scene.
Between France and Smugglers enough goods were snuck into the city and enough was grown -within- the city to keep it going during a 21 year long siege. ON AN ISLAND. No escape but the sea.