From 606c3c8604acebe4291c21a8fd2a1bf1dddcd9d8 Mon Sep 17 00:00:00 2001
From: Jehan-Guillaume de Rorthais <jgdr@dalibo.com>
Date: Tue, 4 Apr 2023 16:24:40 +0200
Subject: [PATCH 3/4] Add some debug and metrics

---
 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

