Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kSxC5-0003We-C2 for pgsql-performance@arkaria.postgresql.org; Thu, 15 Oct 2020 06:56:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kSxC4-0007GM-5D for pgsql-performance@arkaria.postgresql.org; Thu, 15 Oct 2020 06:56:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kSxC3-0007GD-R6 for pgsql-performance@lists.postgresql.org; Thu, 15 Oct 2020 06:56:55 +0000 Received: from mail-ej1-x641.google.com ([2a00:1450:4864:20::641]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kSxC1-00065c-2I for pgsql-performance@postgresql.org; Thu, 15 Oct 2020 06:56:54 +0000 Received: by mail-ej1-x641.google.com with SMTP id p5so2045777ejj.2 for ; Wed, 14 Oct 2020 23:56:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=DHaAHSwYRD89cXGpKOQvWZrBGBeAWLBe04MK/qREO5g=; b=otalNf2cHt5eOAGZQdDpiB9+zLWHO6f4FvXY7Vy0Af8MTikYV1j+gVSDORXLiLZagJ leMEulzeVNiywkZ9U5lc+oR9+oZuilhRCRiywHU9Ae6mGDeLy69GNXbcxrJjy9Tq6VXf gCrPTAS7wrhsRBAodBPED5VvT/OXAQxCZgpoUqadYI8AcMw4iEp7flC8dhQOQIY+LZ8d LpcxjLY53uWO1wRfv9oM/znc2LCtO/I/lIEUZXY5zSAm59XZPogKz8j7BePw5BiBGN0J PhwAEISEqvc4ms0vUXIklDQ2bspx59GyvtPQwi63lpTx+BpInRyZQIB/o6PSSBWcsZjS DVyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=DHaAHSwYRD89cXGpKOQvWZrBGBeAWLBe04MK/qREO5g=; b=a85YqVQMFe6fZw7Ev9DesLHeAGfThHkTMp83tDkm8mkzShtjaROlJqv4vW9PNWpjQ/ bmIYVMnl+KrIAwHW5bqXFd7VBM303VPXGC0SnPNIIeqhjqte9dtedaPW8HwSTwzlbwea mJyJW2J+qtBQx3lBCiKoboyR0yRQcSyIevb5HNEH2hoBQoV/wJR0egR2OfdZjp6WE0Dj OL9DSxcfbnhNNVFBynOezYC3dF1yh84k5SpUPNGUQpJjEF2uk/VWx7RhBGN9iWG62rPz 6/T5Zg+6z+4e0sl29XsC9RxQ+p5lR1PDozWlHpMXPM+yCPk0XWCO21XNzSvNfH5Rw9Ao uOAQ== X-Gm-Message-State: AOAM531812e7IyRcL8b5o1T0KgtMsMP9Oev83oy01HBghA7Fh0Ou3D/r OgyqLRe6+icMEFvR17qd35lDNA== X-Google-Smtp-Source: ABdhPJwWX4oKq7+MM2Guo/sWuLxTjOa0gTPGVUEU/cusYssJgpFSeGl+LTX0U533p2HEXZ0+wffzBA== X-Received: by 2002:a17:907:43c3:: with SMTP id i3mr2728427ejs.231.1602745011108; Wed, 14 Oct 2020 23:56:51 -0700 (PDT) Received: from dynamic-eo96g0i5kykc3cm91-pd01.res.v6.highway.a1.net (dynamic-eo96g0i5kykc3cm91-pd01.res.v6.highway.a1.net. [2001:871:60:99d8:a978:c0b9:4def:4ea5]) by smtp.gmail.com with ESMTPSA id n20sm886803eje.85.2020.10.14.23.56.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2020 23:56:50 -0700 (PDT) Message-ID: Subject: Re: Poor Performance running Django unit tests after upgrading from 10.6 From: Laurenz Albe To: Roger Hunwicks , pgsql-performance@postgresql.org Date: Thu, 15 Oct 2020 08:56:47 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-1.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, 2020-10-15 at 01:21 -0400, Roger Hunwicks wrote: > We have a large Django application running against a Postgresql database. > > When we run the test suite using Postgresql 10.7 in a Docker container > we consistently get: > > Ran 1166 tests in 1291.855s > > When we first started running the same test suite against Postgresql > 12.4 we got: > > Ran 1166 tests in 8502.030s > > I think that this reduction in performance is caused by the lack of > accurate statistics [...] > > We have since managed to get the performance of the test run using > 12.4 back to approximately the normal range by customizing the > Postgresql parameters. `seq_page_cost=0.1` and `random_page_cost=0.11` > seem to be key, but we are also setting `shared_buffers`, etc. and all > the other typical parameters. With Postgresql 10.7 we weren't setting > anything and performance was fine using just the defaults, given the > tiny data volumes. > > However, even though we have similar performance for 12.4 for most > test runs, it remains very variable. About 30% of the time we get > something like: > > I think we have narrowed down the problem to a single, very complex, > materialized view using CTEs; the unit tests create the test data and > then refresh the materialized view before executing the actual test > code. > > Database logging using autoexplain shows things like: > > db_1 | 2020-10-14 10:27:59.692 UTC [255] LOG: duration: > 4134.625 ms plan: > db_1 | Query Text: REFRESH MATERIALIZED VIEW > price_marketpricefacts_materialized > db_1 | Merge Join > (cost=14141048331504.30..9635143213364288.00 rows=116618175994107184 > width=3302) (actual time=4134.245..4134.403 rows=36 loops=1) > > For comparison, the equivalent query on 10.7 has: > > db_1 | 2020-10-15 03:28:58.382 UTC [163] LOG: duration: > 10.500 ms plan: > db_1 | Query Text: REFRESH MATERIALIZED VIEW > price_marketpricefacts_materialized > db_1 | Hash Left Join (cost=467650.55..508612.80 > rows=199494 width=3302) (actual time=10.281..10.341 rows=40 loops=1) > > I can get performance almost identical to 10.7 by altering the unit > tests so that in each test that refreshes the materialized view prior > to executing the query, we execute `ANALYZE;` prior to refreshing the > view. > > Is it worth us trying to debug the plan for situations with low row > counts and poor statistics? Or is this use case not really covered: > the general advice is obviously to make sure that statistics are up to > date before troubleshooting performance problems. On the other hand, > it is not easy for us to make sure that we run analyze inside the > transaction in each unit test; it also seems a bit wasteful. > > Opinions and advice gratefully received. Yes, the query plan for the query that defines the materialized view is the interesting data point. Run an EXPLAIN (ANALYZE, BUFFERS) on that query. If your statistics are off because the data have just been imported a second ago, run an explicit ANALYZE on the affected tables after import. If your statistics are off because they are not calculated often enough, consider lowering "autovacuum_analyze_scale_factor". Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com