From 8807ba1263c5898a96eba045e3fde9216e734171 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Fri, 28 Mar 2025 21:12:04 +0530
Subject: [PATCH 3/3] Use length of ec_derives clause list as initial hash
 table size estimation

Ashutosh Bapat
---
 src/backend/optimizer/path/equivclass.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/optimizer/path/equivclass.c
index b988db4d7b3..8229405e4f4 100644
--- a/src/backend/optimizer/path/equivclass.c
+++ b/src/backend/optimizer/path/equivclass.c
@@ -3457,8 +3457,10 @@ ec_build_derives_hash(PlannerInfo *root, EquivalenceClass *ec)
 {
 	Assert(!ec->ec_derives_hash);
 
-	/* Create the hash table */
-	ec->ec_derives_hash = derives_create(root->planner_cxt, 256, NULL);
+	/* Create the hash table. */
+	ec->ec_derives_hash = derives_create(root->planner_cxt,
+										 pg_nextpower2_32(list_length(ec->ec_derives_list)),
+										 NULL);
 
 	foreach_node(RestrictInfo, rinfo, ec->ec_derives_list)
 		ec_add_clause_to_derives_hash(ec, rinfo);
-- 
2.34.1

