[erlang-questions] swapping tables in mnesia

Chandru <>
Wed Nov 28 23:19:32 CET 2012

-record(my_data, {a, b, c}).

-record(sys_state, {key, value}).

mnesia:create_table(my_data_1, [{record_info(fields, my_data),
{record_name, my_data}, blah, blah]).

mnesia:create_table(my_data_2, [{record_info(fields, my_data),
{record_name, my_data}, blah, blah]).

mnesia:create_table(sys_state, [{record_info(fields, sys_state), blah,

mnesia:dirty_write(#sys_state{key = active_table, value = my_data_1}).

active_table() ->
    case mnesia:dirty_read(sys_state, active_table) of
       [] -> my_data_1;
       [#sys_state{value= V}] -> V

standby_table() ->
     case active_table() of
          my_data_1 -> my_data_2;
          my_data_2 -> my_data_1

swap_tables() ->
      mnesia:dirty_write(#sys_state{key = active_table, value =

upload_new_data() ->

read_data_from_table() ->
     mnesia:dirty_read(active_table(), blah).


On 28 November 2012 14:06, Philip Clarke <> wrote:

> Hi,
> Is it possible to swap or replace tables in mnesia ?
> For example, say I have a file which is read and parsed by some erlang
> process and then stored in a mnesia table.  When it is ready, the table
> data is used by some other processes.
> Then at some later time, I get a new version of the file.  I don't want to
> delete the existing table and load it up from the new file (the system
> would not be able to function during the load time).
> I would like for example to load up to a table called e.g. temp and when
> that has finished, replace my existing table with the temp table.
> One option I considered is to just keep loading up to new tables e.g.
> version1, version2, version3 etc. and then have another table which keeps
> the name of the table to use.  I'm interested to hear if anyone has a
> better solution.
> Thanks
> Philip
> _______________________________________________
> erlang-questions mailing list
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121128/bac23fc1/attachment.html>

More information about the erlang-questions mailing list