1
votes

I have a requirement where i need to create a mnesia table for every ejabberd user. Since the users are large and their username is not known in advance, the mnesia tables with the username has to be created at runtime. Currently this is done by creating the atoms dynamically for the table-name as the mnesia:create_table methods takes the table name as an atom using the following code.

-record(schedule_msg, {schedule_hash, from, to, packet, pid}).

mnesia:create_table(list_to_atom(lists:concat(["schedule_msg_", From#jid.user])),
        [{disc_only_copies, [node()]}, {type, set},
         {attributes, record_info(fields, schedule_msg)}]),

In the http://learnyousomeerlang.com/starting-out-for-real#atoms it is recommended not to create atoms dynamically as it is not garbage collected and the atom lookup table is of finite size.

So how to create mnesia tables without using atom's ?

or

There should not be as many tables as the number of users in the first place. Is it a bad design from the view of performance?

1
The recommendation against dynamically creating atoms is there really to make you think carefully if it is a good idea. If you KNOW there won't be too many then you could consider breaking the recommendation, but obviously if you do run out it totally breaks the run time. There is also a limit to the number of ETS tables, but you can tune it at least. Without knowing the details of your case it's tricky to say, but if you are worried about exhausting even half the 1M atom limit at all, then maybe a table per user also isn't ideal. I'd certainly want to try and find another way myself.Michael

1 Answers

2
votes

So how to create mnesia tables without using atom's ?

You cannot

There should not be as many tables as the number of users in the first place. Is it a bad design from the view of performance?

Yes, it is a bad idea. The Mnesia tables is a limited resource you do not want to create thousands of them without a very very good reason.