From 43fc438cc697de2217eed70ac76fc904f6178814 Mon Sep 17 00:00:00 2001
From: Evdokimov Ilia <ilya.evdokimov@tantorlabs.com>
Date: Thu, 23 Jul 2026 13:36:25 +0300
Subject: [PATCH v2 5/9] Register the JoinDomain built in
 find_ec_position_matching_expr()

Every other JoinDomain construction site in the tree immediately
appends the new domain to root->join_domains after makeNode(). The
one built on the fly here to back a freshly-created EquivalenceClass
was the sole exception.

No concrete failing query found yet -- the top-level JoinDomain's
empty jd_relids currently makes find_join_domain() succeed via that
fallback before it would ever need this one -- but it's a real
invariant violation that any future code walking root->join_domains
exhaustively would trip over silently.
---
 src/backend/optimizer/path/uniquekey.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/backend/optimizer/path/uniquekey.c b/src/backend/optimizer/path/uniquekey.c
index 063fc15530c..f9d5ddeaf0f 100644
--- a/src/backend/optimizer/path/uniquekey.c
+++ b/src/backend/optimizer/path/uniquekey.c
@@ -288,6 +288,7 @@ find_ec_position_matching_expr(PlannerInfo *root, RelOptInfo *baserel,
 	/* Create the EC. */
 	jdomain = makeNode(JoinDomain);
 	jdomain->jd_relids = pull_varnos(root, (Node *) expr);
+	root->join_domains = lappend(root->join_domains, jdomain);
 	ec = get_eclass_for_sort_expr(root, expr,
 								  opfamilies,
 								  exprType((Node *) expr),
-- 
2.34.1

