﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32825	Django unit tests with postgres can take an exceeding long time	Rich Rauenzahn		"I've had this happen with two different projects now and both I was able to resolve with the same ""hack"".

I don't completely understand the actual mechanism for the degenerate case (I do have some explain plans I can share, but I'm not sure is relevant given my solution). 

This was reproducible in my environment repeatedly: some queries in postgres during unit tests hit a degenerate case (with relatively small amounts of data!) where they take 100 of times longer than normal (production seems fine.)  I'm talking about 2.5 minute queries that takes seconds normally.

postgresql11-server-11.12-1PGDG.rhel7.x86_64

The solution in both projects is the following mixin I add to my test case classes:

{{{
def _vacuum():
    # Some unit test queries seem to take a much longer time.
    # Let's try vacuuming.
    # https://stackoverflow.com/a/13955271/2077386
    with connection.cursor() as cursor:
        logging.info(""Vacuum: begin"")
        cursor.execute(""VACUUM ANALYZE"")
        logging.info(""Vacuum: complete"")

class VacuumMixin:
    @classmethod
    def setUpClass(cls):
        _vacuum()
        return super().setUpClass()

    @classmethod
    def tearDownClass(cls):
        ret = super().tearDownClass()
        _vacuum()
        return ret
}}}

I realize this is more of a postgres problem than a Django problem, but I wanted to raise awareness of it in case people have similar problem.  I'm not sure its practical to run vacuum in the underlying Django code given that there may be more than one database, etc.?

"	Bug	closed	contrib.postgres	2.2	Normal	needsinfo			Unreviewed	0	0	0	0	0	0
