public inbox for [email protected]  
help / color / mirror / Atom feed
From: Jehan-Guillaume de Rorthais <[email protected]>
Subject: [PATCH 3/4] Add some debug and metrics
Date: Tue, 4 Apr 2023 16:24:40 +0200

---
 src/backend/executor/nodeHash.c | 35 +++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 4544296391..ec6b80121b 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -81,6 +81,35 @@ static bool ExecParallelHashTuplePrealloc(HashJoinTable hashtable,
 static void ExecParallelHashMergeCounters(HashJoinTable hashtable);
 static void ExecParallelHashCloseBatchAccessors(HashJoinTable hashtable);
 
+static void debugIncreaseBatches(HashJoinTable hashtable, char * label)
+{
+	int allocInnerBufFiles = 0;
+	int allocOuterBufFiles = 0;
+
+	for (int i=0; i<hashtable->nbatch; i++) {
+		if (hashtable->innerBatchFile != NULL &&
+			hashtable->innerBatchFile[i] != NULL)
+			allocInnerBufFiles++;
+		if (hashtable->outerBatchFile != NULL &&
+			hashtable->outerBatchFile[i] != NULL)
+			allocOuterBufFiles++;
+	}
+
+	elog(WARNING, "%25s: %d,\t%ld,\t%ld,\t%ld,\t%ld,\t%d,\t%d,\t%d,\t%d",
+		 label,
+		 hashtable->nbatch,
+		 hashtable->spaceAllowed,
+		 hashtable->spaceUsed,
+		 hashtable->spacePeak,
+		 hashtable->fileCxt->mem_allocated,
+		 allocInnerBufFiles,
+		 allocOuterBufFiles,
+		 hashtable->nbuckets,
+		 hashtable->growEnabled);
+
+	elog(LOG, "%s ======= context stats =======", label);
+	MemoryContextStats(TopMemoryContext);
+}
 
 /* ----------------------------------------------------------------
  *		ExecHash
@@ -889,6 +918,8 @@ ExecHashTableDestroy(HashJoinTable hashtable)
 {
 	int			i;
 
+	debugIncreaseBatches(hashtable, "destroying the hash table");
+
 	/*
 	 * Make sure all the temp files are closed.  We skip batch 0, since it
 	 * can't have any temp files (and the arrays might not even exist if
@@ -1685,7 +1716,11 @@ ExecHashTableInsert(HashJoinTable hashtable,
 		if (hashtable->spaceUsed +
 			hashtable->nbuckets_optimal * sizeof(HashJoinTuple)
 			> hashtable->spaceAllowed)
+		{
+			debugIncreaseBatches(hashtable, "trying to save memory");
 			ExecHashIncreaseNumBatches(hashtable);
+			debugIncreaseBatches(hashtable, "memory rescue done");
+		}
 	}
 	else
 	{
-- 
2.39.2


--MP_/xtmrMo8rz+yGL.lLMmD=jly
Content-Type: text/x-patch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=0004-v4-Limit-BufFile-memory-explosion-with-bad-HashJoin.patch



view thread (7+ messages)  latest in thread

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: [email protected]
  Cc: [email protected]
  Subject: Re: [PATCH 3/4] Add some debug and metrics
  In-Reply-To: <no-message-id-1857386@localhost>

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

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