Bug 23624: Count rows in report without (potentially) consuming all memory
authorPaul Hoffman <paul@flo.org>
Tue, 17 Sep 2019 17:00:03 +0000 (13:00 -0400)
committerFridolin Somers <fridolin.somers@biblibre.com>
Tue, 8 Oct 2019 07:24:50 +0000 (09:24 +0200)
commitbe8bade672b279da24f15154c88a5c5faf840b19
tree22c9ddfb386d66e7f79b25e9f4e245105f9d898b
parent278cffa3f1c8f86b59537b12d3d9feac72bd554e
Bug 23624: Count rows in report without (potentially) consuming all memory

C4::Reports::Guided::nb_rows (called by get_prepped_report in reports/guided_reports.pl) uses DBI::fetchall_arrayref to retrieve all rows at once; counts them; and then discards the rows and returns the count.  This has the potential, if the number of rows is very large, to exhaust all available memory.

(Other code in guided_reports.pl has the same potential effect, but because the solution to that is much less straightforward it will be addressed in a separate bug report.)

This patch uses the second ($max_rows) parameter to DBI::fetchall_arrayref to retrieve a smaller number (1,000) of rows at a time, looping until all results have been retrieved.  This will only use as much memory as the maximum amount used by a single call to DBI::fetchall_arrayref.

Test Plan:
1) Create a report the will generate a huge number of results
2) Run the report, watch your memory usage spike
3) Apply this patch
4) Restart all the things!
5) Run the report again, note your memory usage is much lower

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
(cherry picked from commit 79e15278f718768fa082b7b3d7b48e6adde164da)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
C4/Reports/Guided.pm