Now I decided to create a small module that will help you manage the Extensions and Ids.
Besides what I showed in the first post, I added to the module an extra feature: now you have the possibility to get the free Ids: if for example you have 2 table extensions with Ids 50251 and 50254, the free Ids are considered the ones not used within the ones used, so in this example: 50252 and 50253. You can do this by specifying and extra parameter to the function.