Searching for bug: exponential growth of table, mdl_question. Currently examining the classes restore_controller and backup_controller
Hello,
which passes a course module to duplicate_module() of lib.php
lib.php then uses the course module as an argument for
- new backup_controller()
- new restore_controller()
- $DB->setfield()
So far, I have not found any loops.
Current assumption
The problem is NOT caused by DB->set_field(), otherwise users would complain of exponential growth on many other tables.
Symptom summary
The symptom appears in the table, mdl_question.
When a quiz is duplicated multiple times,
each question in the quiz is appended to the table a number of times equal to 2^(n-1) , where n equals the number of duplicates created.

I am currently looking for the cause of issue MDL-63260.
To do this, I am examining the two classes, restore_controller and backup_controller.
/backup/controller/_____.class.php
The symptoms of this bug are described below.
My request
If you know of other components which I should investigate, please point me toward them.Tracing the source
Selecting "Duplicate" on a quiz activates /course/mod.php,which passes a course module to duplicate_module() of lib.php
lib.php then uses the course module as an argument for
- new backup_controller()
- new restore_controller()
- $DB->setfield()
So far, I have not found any loops.
Current assumption
The problem is NOT caused by DB->set_field(), otherwise users would complain of exponential growth on many other tables.
Symptom summary
The symptom appears in the table, mdl_question.
When a quiz is duplicated multiple times,
each question in the quiz is appended to the table a number of times equal to 2^(n-1) , where n equals the number of duplicates created.
For example, a Moodle installation with only 1 quiz and 1 question will generate the following table after the quiz has been duplicated 4 times.

16 rows = (1 original quiz) + 2^0 + 2^1 + 2^2 + 2^3
= (1 original quiz) + 4 duplicates
(the ID of 16 in my example is irrelevant)
More details about the symptom are beautifully described here: MDL-63260
= (1 original quiz) + 4 duplicates
(the ID of 16 in my example is irrelevant)
More details about the symptom are beautifully described here: MDL-63260
Re: Searching for bug: exponential growth of table, mdl_question. Currently examining the classes restore_controller and backup_controller
The cause is clear, and already linked in the tracker. This is a 'regression' cauased by MDL-45851.
In fact, the MDL-45851 fix was right and necessary, it is just that it did not go far enough, and I have not yet thought about the best approach to solve it. Two possibilities:
- MDL-45851 change retore to duplicated all random questions, rather than ever risk reusing them. A hard but possibly more correct fix would be to change that logic to only duplicate questions if they are used.
- The alternative is to fix this somewhere other than in the backup/restore code. E.g. it would be easier, and would solve other problems too, to have a scheduled task to automatically delete random questions that are no longer used anywhere.
Re: Searching for bug: exponential growth of table, mdl_question. Currently examining the classes restore_controller and backup_controller
I have tested my solution by the instructions of MDL-63260.
Any feedback is appreciated.
A patch file is attached, or you can see the commits here:
https://github.com/moodle/moodle/compare/master...boGitsIT:MDL-63260-bO#diff-abca48266288361745e41f33e4884124