Duplicate results from qlc on ordered_set mnesia table with secondary index

Sam Bobroff samb-bulk@REDACTED
Tue Aug 11 08:54:58 CEST 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello Mighty Erlangers,

I've found a situation where a simple qlc query returns multiple records
with the same primary key from an mnesia table with the ordered_set
type. It seems to be triggered by doing the query just after updating a
record using mnesia:write that is within the same transaction as the
query. The duplicate entry does not appear in the results of mnesia:select.

Here's some code that reproduces the issue for me in Erlang R13B01:

- --- begin ---

%% Bug replication code, save to "bug.erl".
%% Compile with: erlc bug.erl
%% Run with: erl -noshell -run bug -run init stop

- -module(bug).
- -export([start/0]).

- -include_lib("stdlib/include/qlc.hrl").

- -record(rec, {key, val}).

start() ->
	ok = mnesia:start(),
	{atomic, ok} = mnesia:create_table(rec, [{attributes,
record_info(fields, rec)}, {type, ordered_set}]),
	[ ok = mnesia:dirty_write(R) || R <- [ {rec,1,1}, {rec,2,1} ] ],
	io:fwrite("Before add_table_index.\n"),
	{atomic, _} = mnesia:transaction(fun do_test/0, []),
	{atomic, ok} = mnesia:add_table_index(rec, val),
	io:fwrite("After add_table_index.\n"),
	{atomic, _} = mnesia:transaction(fun do_test/0, []).

do_test() ->
	Val = 1,
	Key = 1,
	[Rec] = mnesia:read(rec, Key),
	ok = mnesia:write(Rec),
	check(Val).

check(Val) ->
	io:fwrite("select(val ~p) = ~p\n", [
		Val, mnesia:select(rec, [{#rec{val = Val, key = '$1', _ = '_'}, [],
['$1']}])
	]),
	io:fwrite("qlc(val ~p)    = ~p\n", [
		Val, qlc:e(qlc:q([ Rec#rec.key || Rec <- mnesia:table(rec),
Rec#rec.val =:= Val ]))
	]).

- -- end ---

The output I get from running this code is:

$ erl -noshell -run bug -run init stop
Before add_table_index.
select(val 1) = [1,2]
qlc(val 1)    = [1,2]
After add_table_index.
select(val 1) = [1,2]
qlc(val 1)    = [1,2,1]

It seems like two record with the key of 1 must be incorrect for an
ordered_set table. Is this a bug?

Sam.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFKgRXCm97/UHSa/AQRAmc/AJ9lqXXFYw+qw8Q6eW29JFIUQO+DZQCdFFfq
a9aaH2VmbbAHkRibPYEPWPw=
=30qU
-----END PGP SIGNATURE-----


More information about the erlang-bugs mailing list