Obviously the ability to simply dig a pit or place a wall to block all attacks is a bit overpowered. Some simple suggestions on how to have creatures that can get around them.
1. Burrow. Basically, pathfinding of burrowers will take into account the area without walls, or X deep into the soil and stone, and allow them to pass, like vermin, by 'teleporting' in. It could increase the pathfinding distance by some huge number, so that they only do this if necessary. They would have to stand next to the wall in question a long time to simulate time it takes to burrow. Finally, the burrow would be assumed to be destroyed behind them, thus in ground it would leave no trace (though it could destroy walls)... or you could have so the tile is marked as a 'burrow' so that creatures of that size or smaller can fit and use it like a hallway, but others find it useless.
2. Climb: similar dynamics to burrow, but for going 1 z-level up and 1 x- or y-level over, and no wall destroying.
3. Special classes attacking with others. Example: goblin siege including 3 miners. This would be hard to code for the AI to dig into the fort, but an easier suggestion would be things like 'Goblin Demolitionists', whose entire job is to walk up to a wall, and blow themselves up, along with anything 1-2 squares away. These would be deadly foes, capable of destroying walls, but also of killing or injuring any and all dwarves in melee range immediately...
4. Bringing along and assembling siege weapons on-site... and have catapults destroy walls... and have them bring a non-trade 'caravan' of stone to load into the catapult and fire. Would also make sieges more deadly because siege weapons would kill dwarves too, AND disallow people from just holeing up and waiting it out. I'm guilty of doing this far too often...