Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLxFO-0005tl-Vm for pgsql-committers@arkaria.postgresql.org; Mon, 04 Dec 2017 20:21:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLxFO-00024h-Hb for pgsql-committers@arkaria.postgresql.org; Mon, 04 Dec 2017 20:21:50 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eLxFO-00024U-4y for pgsql-committers@lists.postgresql.org; Mon, 04 Dec 2017 20:21:50 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eLxFL-0004FO-3I for pgsql-committers@postgresql.org; Mon, 04 Dec 2017 20:21:49 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 0729420DDC; Mon, 4 Dec 2017 15:21:44 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute5.internal (MEProxy); Mon, 04 Dec 2017 15:21:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=znFQ7wPKWC4Msgv6dcLpGtUYIAKqU D5boOU//GbpNNU=; b=G7RxRCF6m3aTsYjjVwOVpcbFDRTptHKpgBmptZTOPnPXX ZoSv8r7LP0SJ4ZyO4RtQikmh8s88cj0Ka9xsViOqctYoPE+ku8rxiJliGAD9NyPX 5P736IRo4cJiNGWAZUVs088sGcW/UzJSprQBcqFqTwuail9NkXIyyFxRlsOHo+bg lvgLM6sjPQFmiNkKswg6ok40TVvSJNAfxHJbO1Pln8JgL5wqGEHkMRFYNRZiOfFr U6UA/l1T5BGXKdkbJc0e4M+zJBnDXHPTvFAwrgK7ZmG13aZvwMPKuSS0mqow5q6z rfONhgWmax6e5HSLhPsKi72xBC/WdRhJi+9BZZiNw== X-ME-Sender: Received: from intern.anarazel.de (unknown [172.58.32.248]) by mail.messagingengine.com (Postfix) with ESMTPA id 85D4725016; Mon, 4 Dec 2017 15:21:43 -0500 (EST) Date: Mon, 4 Dec 2017 12:21:42 -0800 From: Andres Freund To: Thomas Munro Cc: Tom Lane , pgsql-committers Subject: Re: pgsql: Add some regression tests that exercise hash join code. Message-ID: <20171204202142.spwkwc545vvxmed7@alap3.anarazel.de> References: <84690.1512011601@sss.pgh.pa.us> <20171130160644.6oh5jcaipfjxmze2@alap3.anarazel.de> <20171203183928.pbfygs26kyhroo2r@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2017-12-04 12:28:56 +1300, Thomas Munro wrote: > From 43eeea0bc35204440d262224b56efca958b33428 Mon Sep 17 00:00:00 2001 > From: Thomas Munro > Date: Mon, 4 Dec 2017 11:52:11 +1300 > Subject: [PATCH] Fix EXPLAIN ANALYZE of hash join when the leader doesn't > execute. > > If a hash join appears in a parallel query, there may be no hash table > available for explain.c to inspect even though a hash table may have been > built in other processes. This could happen either because > parallel_leader_participation was set to off or because the leader happened to > hit the end of the outer relation immediately (even though the complete > relation is not empty) and decided not to build the hash table. > > Commit bf11e7ee introduced a way for workers to exchange instrumentation via > the DSM segment for Sort nodes even though they are not parallel-aware. This > commit does the same for Hash nodes, so that explain.c has a way to find > instrumentation data from an arbitrary participant that actually built the > hash table. Makes sense. Seems ok to only fix this inv11, even if present earlier. > diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c > index 447f69d044e..1ffe635d66c 100644 > --- a/src/backend/commands/explain.c > +++ b/src/backend/commands/explain.c > @@ -2379,34 +2379,62 @@ show_sort_info(SortState *sortstate, ExplainState *es) > static void > show_hash_info(HashState *hashstate, ExplainState *es) > { > - HashJoinTable hashtable; > + HashInstrumentation *hinstrument = NULL; > > - hashtable = hashstate->hashtable; > + /* > + * In a parallel query, the leader process may or may not have run the > + * hash join, and even if it did it may not have built a hash table due to > + * timing (if it started late it might have seen no tuples in the outer > + * relation and skipped building the hash table). Therefore we have to be > + * prepared to get instrumentation data from a worker if there is no hash > + * table. > + */ > + if (hashstate->hashtable) > + { > + hinstrument = (HashInstrumentation *) > + palloc(sizeof(HashInstrumentation)); > + ExecHashGetInstrumentation(hinstrument, hashstate->hashtable); > + } > + else if (hashstate->shared_info) > + { > + SharedHashInfo *shared_info = hashstate->shared_info; > + int i; > + > + /* Find the first worker that built a hash table. */ > + for (i = 0; i < shared_info->num_workers; ++i) > + { > + if (shared_info->hinstrument[i].nbatch > 0) > + { > + hinstrument = &shared_info->hinstrument[i]; > + break; > + } > + } > + } I can't think of a way that, before the parallel HJ patch, results in a difference between the size of the hashtable between workers. Short of some participant not having a hashtable at all, that is. Wwe also don't currently display lookup information etc. So just more or less randomly choosing one worker's seems ok. > + case T_HashState: > + /* even when not parallel-aware */ "..., for stats" or such maybe? > + ExecHashEstimate((HashState *) planstate, e->pcxt); > + break; > case T_SortState: > /* even when not parallel-aware */ I'd just update that comment as well. > #endif /* NODEHASH_H */ > diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h > index e05bc04f525..6c322e57c00 100644 > --- a/src/include/nodes/execnodes.h > +++ b/src/include/nodes/execnodes.h > @@ -1980,6 +1980,29 @@ typedef struct GatherMergeState > struct binaryheap *gm_heap; /* binary heap of slot indices */ > } GatherMergeState; > > +/* ---------------- > + * Values displayed by EXPLAIN ANALYZE > + * ---------------- > + */ > +typedef struct HashInstrumentation > +{ > + int nbuckets; > + int nbuckets_original; > + int nbatch; > + int nbatch_original; > + size_t space_peak; > +} HashInstrumentation; Maybe I'm being pedantic here, but I think it'd not hurt to explain what these mean. It's obviously pretty clear what nbuckets/batch mean, but the difference between original and not is far less clea.r Looks reasonable these nitpicks aside. Greetings, Andres Freund