Can't run mnesia:first on empty fragmented table

Magnus Henoch <>
Fri Mar 4 15:39:15 CET 2011

Hi all,

When I run mnesia:first on an empty fragmented table, it tries to
access the fragment with the number one beyond the maximum.  In the
sample code below, I create a table with two fragments, 'foo' and
'foo_frag2', but mnesia tries to access 'foo_frag3':



foo() ->
    net_kernel:start([foo, shortnames]),
    {atomic, ok} = mnesia:create_table(foo, []),
    %% activate fragmentation
    {atomic, ok} = mnesia:change_table_frag(foo, {activate, []}),
    %% add a second fragment on this node
    {atomic, ok} = mnesia:change_table_frag(foo, {add_frag, [node()]}),
    io:format("Our table is fragmented:~n~p~n", [mnesia:table_info(foo, all)]),
    io:format("Now let's run mnesia:first.  We expect to get ~p.~n~p~n",
               %% but we get {'EXIT',{aborted,{no_exists,[foo_frag3]}}}
               catch mnesia:activity(sync_dirty,
                                 fun() -> mnesia:first(foo) end,

It looks like a simple off-by-one error in mnesia_frag:search_first.
Changing the guard from '=<' to '<' as in the patch below fixes my
test case (and the real system I distilled it from), but I'd
appreciate a second opinion.


diff --git a/lib/mnesia/src/mnesia_frag.erl b/lib/mnesia/src/mnesia_frag.erl
index a2958ab..d33dafe 100644
--- a/lib/mnesia/src/mnesia_frag.erl
+++ b/lib/mnesia/src/mnesia_frag.erl
@@ -209,7 +209,7 @@ first(ActivityId, Opaque, Tab) ->
-search_first(ActivityId, Opaque, Tab, N, FH) when N =< FH#frag_state.n_fragments ->
+search_first(ActivityId, Opaque, Tab, N, FH) when N < FH#frag_state.n_fragments ->
     NextN = N + 1,
     NextFrag = n_to_frag_name(Tab, NextN),
     case mnesia:first(ActivityId, Opaque, NextFrag) of

More information about the erlang-bugs mailing list