pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexey Bashtanov <bashtanov@imap.cc>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Subject: don't allocate HashAgg hash tables when running explain only
Date: Fri, 13 Nov 2020 16:10:30 +0000
Message-ID: <36823f65-050d-ae24-aa4d-a37726998240@imap.cc> (raw)

Hi,

I got somewhat scared when my explain took a few seconds to complete and 
used a few gigs of RAM.
To reproduce try the following:

discard temp;
create temp table a as select to_timestamp(generate_series(1, 7000)) i;
analyze a;
set work_mem to '3GB';
explain select distinct a1.i - a2.i from a a1, a a2;

I would appreciate if someone could have a look at the patch attached, 
which makes executor skip initializing hash tables when doing explain only.

Best, Alex

Attachments:

  [text/x-patch] dont-build-agg-hashtable-on-explain-only-v1.diff (651B, ../36823f65-050d-ae24-aa4d-a37726998240@imap.cc/2-dont-build-agg-hashtable-on-explain-only-v1.diff)
  download | inline diff:
diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index d87677d659..196fe09c52 100644
--- a/src/backend/executor/nodeAgg.c
+++ b/src/backend/executor/nodeAgg.c
@@ -3665,7 +3665,11 @@ ExecInitAgg(Agg *node, EState *estate, int eflags)
 							&aggstate->hash_ngroups_limit,
 							&aggstate->hash_planned_partitions);
 		find_hash_columns(aggstate);
-		build_hash_tables(aggstate);
+
+		/* Skip massive memory allocation when running only explain */
+		if (!(eflags & EXEC_FLAG_EXPLAIN_ONLY))
+			build_hash_tables(aggstate);
+
 		aggstate->table_filled = false;
 
 		/* Initialize this to 1, meaning nothing spilled, yet */

view thread (6+ messages)  latest in thread

Message-ID: <36823f65-050d-ae24-aa4d-a37726998240@imap.cc>
Permalink:  ../36823f65-050d-ae24-aa4d-a37726998240@imap.cc/
Also on:    postgresql.org/message-id/36823f65-050d-ae24-aa4d-a37726998240@imap.cc

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: bashtanov@imap.cc
  Subject: Re: don't allocate HashAgg hash tables when running explain only
  In-Reply-To: <36823f65-050d-ae24-aa4d-a37726998240@imap.cc>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox