Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCjKX-0007Vz-QH for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Sep 2019 11:50:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iCjKW-0001sR-Hl for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Sep 2019 11:50:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCjKW-0001sK-9W for pgsql-hackers@lists.postgresql.org; Tue, 24 Sep 2019 11:50:04 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCjKP-0007FU-Kz for pgsql-hackers@postgresql.org; Tue, 24 Sep 2019 11:50:03 +0000 Received: from [192.168.127.1] (port=45416 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.92.2 (FreeBSD)) (envelope-from ) id 1iCjKN-0003aV-3m for pgsql-hackers@postgresql.org; Tue, 24 Sep 2019 11:49:55 +0000 Received: from localhost ([127.0.0.1]:25567 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.92.1 (FreeBSD)) (envelope-from ) id 1iCjKM-0007Iq-V2 for pgsql-hackers@postgresql.org; Tue, 24 Sep 2019 11:49:55 +0000 From: Andrew Gierth To: pgsql-hackers@postgresql.org Subject: Excessive disk usage in WindowAgg Message-ID: <87a7ator8n.fsf@news-spur.riddles.org.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (berkeley-unix) Date: Tue, 24 Sep 2019 12:49:54 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This one just came up on IRC: create table tltest(a integer, b text, c text, d text); insert into tltest select i, repeat('foo',100), repeat('foo',100), repeat('foo',100) from generate_series(1,100000) i; set log_temp_files=0; set client_min_messages=log; select count(a+c) from (select a, count(*) over () as c from tltest s1) s; LOG: temporary file: path "base/pgsql_tmp/pgsql_tmp82513.3", size 92600000 Using 92MB of disk for one integer seems excessive; the reason is clear from the explain: QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=16250.00..16250.01 rows=1 width=8) (actual time=1236.260..1236.260 rows=1 loops=1) Output: count((tltest.a + (count(*) OVER (?)))) -> WindowAgg (cost=0.00..14750.00 rows=100000 width=12) (actual time=1193.846..1231.216 rows=100000 loops=1) Output: tltest.a, count(*) OVER (?) -> Seq Scan on public.tltest (cost=0.00..13500.00 rows=100000 width=4) (actual time=0.006..14.361 rows=100000 loops=1) Output: tltest.a, tltest.b, tltest.c, tltest.d so the whole width of the table is being stored in the tuplestore used by the windowagg. In create_windowagg_plan, we have: /* * WindowAgg can project, so no need to be terribly picky about child * tlist, but we do need grouping columns to be available */ subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST); Obviously we _do_ need to be more picky about this; it seems clear that using CP_SMALL_TLIST | CP_LABEL_TLIST would be a win in many cases. Opinions? -- Andrew (irc:RhodiumToad)