protect against insertions/removals while iterating

Also make sure that all iterators associated with a table have been deleted
when a table is deleted.