From 1a6ccb87c4be7d0f15ff8d273f28929ec0fc9264 Mon Sep 17 00:00:00 2001
From: Antonin Houska <ah@cybertec.at>
Date: Thu, 18 Apr 2024 16:20:06 +0200
Subject: [PATCH 3/5] Do not use expression indexes to create unique keys.

The existing code does not rely on predOK when checking uniqueness, so don't
let the new code do that.
---
 src/backend/optimizer/path/uniquekey.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/backend/optimizer/path/uniquekey.c b/src/backend/optimizer/path/uniquekey.c
index d3f9aaaac8..0b65ff135f 100644
--- a/src/backend/optimizer/path/uniquekey.c
+++ b/src/backend/optimizer/path/uniquekey.c
@@ -98,8 +98,12 @@ populate_baserel_uniquekeys(PlannerInfo *root, RelOptInfo *rel)
 	{
 		IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
 
-		if (!index->unique || !index->immediate ||
-			(index->indpred != NIL && !index->predOK))
+		/*
+		 * Like in relation_has_unique_index_for(), we shouldn't use predOK
+		 * because its validity might depend on join clauses, however here we
+		 * test uniqueness of the join input.
+		 */
+		if (!index->unique || !index->immediate || index->indpred != NIL)
 			continue;
 
 		if (add_uniquekey_for_uniqueindex(root, index,
-- 
2.44.0

