From 2000e31c553a168fc93ca658e194e8a915916167 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Thu, 6 Feb 2025 22:43:05 +0100
Subject: [PATCH v20250206 1/2] add nbatch/nbucket to explain

---
 src/backend/commands/explain.c          | 18 ++++++++++++++++++
 src/backend/executor/nodeHash.c         |  3 +++
 src/backend/optimizer/path/costsize.c   |  1 +
 src/backend/optimizer/plan/createplan.c |  3 +++
 src/include/nodes/execnodes.h           |  4 ++++
 src/include/nodes/pathnodes.h           |  1 +
 src/include/nodes/plannodes.h           |  4 ++++
 7 files changed, 34 insertions(+)

diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index c24e66f82e1..a3333d763b6 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -3490,6 +3490,24 @@ show_hash_info(HashState *hashstate, ExplainState *es)
 							 spacePeakKb);
 		}
 	}
+	else
+	{
+		if (es->format != EXPLAIN_FORMAT_TEXT)
+		{
+			ExplainPropertyInteger("Hash Buckets", NULL,
+								   hashstate->num_buckets, es);
+			ExplainPropertyInteger("Hash Batches", NULL,
+								   hashstate->num_batches, es);
+		}
+		else
+		{
+			ExplainIndentText(es);
+			appendStringInfo(es->str,
+							 "Buckets: %d  Batches: %d\n",
+							 hashstate->num_buckets,
+							 hashstate->num_batches);
+		}
+	}
 }
 
 /*
diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index d34824097f8..f9b13285f5b 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -389,6 +389,9 @@ ExecInitHash(Hash *node, EState *estate, int eflags)
 	/* delay building hashtable until ExecHashTableCreate() in executor run */
 	hashstate->hashtable = NULL;
 
+	hashstate->num_batches = node->num_batches;
+	hashstate->num_buckets = node->num_buckets;
+
 	/*
 	 * Miscellaneous initialization
 	 *
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 73d78617009..f03c50a6e69 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -4315,6 +4315,7 @@ final_cost_hashjoin(PlannerInfo *root, HashPath *path,
 
 	/* mark the path with estimated # of batches */
 	path->num_batches = numbatches;
+	path->num_buckets = numbuckets;
 
 	/* store the total number of tuples (sum of partial row estimates) */
 	path->inner_rows_total = inner_path_rows_total;
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 816a2b2a576..b41c265686f 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -4929,6 +4929,9 @@ create_hashjoin_plan(PlannerInfo *root,
 						  skewColumn,
 						  skewInherit);
 
+	hash_plan->num_batches = best_path->num_batches;
+	hash_plan->num_buckets = best_path->num_buckets;
+
 	/*
 	 * Set Hash node's startup & total costs equal to total cost of input
 	 * plan; this only affects EXPLAIN display not decisions.
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index aca15f771a2..fbf64bfd42d 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -2799,6 +2799,10 @@ typedef struct HashState
 	FmgrInfo   *skew_hashfunction;	/* lookup data for skew hash function */
 	Oid			skew_collation; /* collation to call skew_hashfunction with */
 
+	/* expected number of batches and buckets (for explain) */
+	int			num_batches;
+	int			num_buckets;
+
 	/*
 	 * In a parallelized hash join, the leader retains a pointer to the
 	 * shared-memory stats area in its shared_info field, and then copies the
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
index 52d44f43021..9d639558971 100644
--- a/src/include/nodes/pathnodes.h
+++ b/src/include/nodes/pathnodes.h
@@ -2169,6 +2169,7 @@ typedef struct HashPath
 	JoinPath	jpath;
 	List	   *path_hashclauses;	/* join clauses used for hashing */
 	int			num_batches;	/* number of batches expected */
+	int			num_buckets;	/* number of buckets expected */
 	Cardinality inner_rows_total;	/* total inner rows expected */
 } HashPath;
 
diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 06d9559ebb9..eeb665981f0 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -1222,6 +1222,10 @@ typedef struct Hash
 	bool		skewInherit;	/* is outer join rel an inheritance tree? */
 	/* all other info is in the parent HashJoin node */
 	Cardinality rows_total;		/* estimate total rows if parallel_aware */
+
+	/* expected number of batches and buckets (for explain) */
+	int			num_batches;
+	int			num_buckets;
 } Hash;
 
 /* ----------------
-- 
2.47.1

