diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 66352a4a624..bee916b833c 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -208,6 +208,7 @@ static void create_one_window_path(PlannerInfo *root,
 								   PathTarget *output_target,
 								   WindowFuncLists *wflists,
 								   List *activeWindows);
+static bool distinct_input_provably_unique(PlannerInfo *root);
 static RelOptInfo *create_distinct_paths(PlannerInfo *root,
 										 RelOptInfo *input_rel,
 										 PathTarget *target);
@@ -2132,6 +2133,14 @@ grouping_planner(PlannerInfo *root, double tuple_fraction,
 				 */
 				root->distinct_elided = true;
 			}
+			else if (distinct_input_provably_unique(root))
+			{
+				/*
+				 * The input is already distinct on the DISTINCT columns, so
+				 * enforcement would be a no-op; skip building it.
+				 */
+				root->distinct_elided = true;
+			}
 			else
 				current_rel = create_distinct_paths(root,
 													current_rel,
@@ -5067,6 +5076,79 @@ create_one_window_path(PlannerInfo *root,
 	add_path(window_rel, path);
 }
 
+/*
+ * distinct_input_provably_unique
+ *		Detect whether this query's DISTINCT clause is a no-op because its
+ *		input is provably distinct on the DISTINCT columns already.
+ *
+ * We handle only the simple but common shape where the query reads exactly
+ * one subquery RTE (no joins -- joining can multiply rows) and every
+ * DISTINCT column is a plain Var of that subquery, and the subquery's own
+ * query structure (DISTINCT, GROUP BY, aggregation, non-ALL set operation)
+ * proves distinctness via query_is_distinct_for().  WHERE quals are fine:
+ * filtering cannot introduce duplicates.  We derive distinctness only from
+ * the subquery's structure, never from catalog uniqueness metadata.
+ *
+ * We bail out on target SRFs (they multiply rows after the subquery scan),
+ * and conservatively on window functions, though those merely add columns.
+ * DISTINCT ON is out too: with more than one row per group it changes which
+ * rows survive, so proving groups of one would need all ON columns covered;
+ * we don't bother.  The caller must not modify parse->distinctClause; on a
+ * true return it merely refrains from building enforcement paths.
+ */
+static bool
+distinct_input_provably_unique(PlannerInfo *root)
+{
+	Query	   *parse = root->parse;
+	RangeTblRef *rtr;
+	RangeTblEntry *rte;
+	List	   *distinct_cols = NIL;
+	ListCell   *lc;
+
+	if (parse->hasDistinctOn)
+		return false;
+
+	/* bail out on anything that complicates the proof */
+	if (parse->hasTargetSRFs || parse->hasWindowFuncs || parse->hasAggs ||
+		parse->groupClause || parse->groupingSets || parse->havingQual)
+		return false;
+
+	/* the input must be exactly one subquery RTE */
+	if (list_length(parse->jointree->fromlist) != 1)
+		return false;
+	rtr = (RangeTblRef *) linitial(parse->jointree->fromlist);
+	if (!IsA(rtr, RangeTblRef))
+		return false;
+	rte = root->simple_rte_array[rtr->rtindex];
+	if (rte->rtekind != RTE_SUBQUERY)
+		return false;
+	if (!query_supports_distinctness(rte->subquery))
+		return false;
+
+	/* every DISTINCT column must be a plain Var of that subquery */
+	foreach(lc, root->processed_distinctClause)
+	{
+		SortGroupClause *sgc = lfirst_node(SortGroupClause, lc);
+		TargetEntry *tle = get_sortgroupclause_tle(sgc, root->processed_tlist);
+		Var		   *var = (Var *) tle->expr;
+		DistinctColInfo *info;
+
+		if (!IsA(var, Var) ||
+			var->varno != rtr->rtindex ||
+			var->varlevelsup != 0 ||
+			var->varattno <= 0)
+			return false;
+
+		info = palloc_object(DistinctColInfo);
+		info->colno = var->varattno;
+		info->opid = sgc->eqop;
+		info->collid = var->varcollid;
+		distinct_cols = lappend(distinct_cols, info);
+	}
+
+	return query_is_distinct_for(rte->subquery, distinct_cols);
+}
+
 /*
  * create_distinct_paths
  *
diff --git a/src/test/regress/expected/select_distinct.out b/src/test/regress/expected/select_distinct.out
index 741f7238742..c90f5a2f796 100644
--- a/src/test/regress/expected/select_distinct.out
+++ b/src/test/regress/expected/select_distinct.out
@@ -596,3 +596,110 @@ SELECT DISTINCT y, x FROM distinct_tbl ORDER BY y;
 
 RESET enable_hashagg;
 DROP TABLE distinct_tbl;
+--
+-- Skip DISTINCT enforcement when the input is provably distinct already
+-- (structural proof via the input subquery's own DISTINCT/GROUP BY/setop).
+--
+-- elided: grouped subquery proves distinctness
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT four FROM (SELECT four FROM tenk1 GROUP BY four) ss;
+       QUERY PLAN        
+-------------------------
+ HashAggregate
+   Group Key: tenk1.four
+   ->  Seq Scan on tenk1
+(3 rows)
+
+-- elided: superset of the unique columns is still unique
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT four, c FROM
+  (SELECT four, count(*) AS c FROM tenk1 GROUP BY four) ss;
+       QUERY PLAN        
+-------------------------
+ HashAggregate
+   Group Key: tenk1.four
+   ->  Seq Scan on tenk1
+(3 rows)
+
+-- elided: aggregate without GROUP BY returns one row
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT c FROM (SELECT count(*) AS c FROM tenk1) ss;
+                     QUERY PLAN                     
+----------------------------------------------------
+ Aggregate
+   ->  Index Only Scan using tenk1_hundred on tenk1
+(2 rows)
+
+-- elided: non-ALL setop output is unique; WHERE cannot break uniqueness
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT x FROM
+  (SELECT four AS x FROM tenk1 UNION SELECT ten FROM tenk1) ss WHERE x > 2;
+              QUERY PLAN               
+---------------------------------------
+ HashAggregate
+   Group Key: tenk1.four
+   ->  Append
+         ->  Seq Scan on tenk1
+               Filter: (four > 2)
+         ->  Seq Scan on tenk1 tenk1_1
+               Filter: (ten > 2)
+(7 rows)
+
+-- not elided: subset of output that isn't the unique key
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT c FROM
+  (SELECT four, count(*) AS c FROM tenk1 GROUP BY four) ss;
+                QUERY PLAN                 
+-------------------------------------------
+ Unique
+   ->  Sort
+         Sort Key: ss.c
+         ->  Subquery Scan on ss
+               ->  HashAggregate
+                     Group Key: tenk1.four
+                     ->  Seq Scan on tenk1
+(7 rows)
+
+-- not elided: joining two unique subqueries can duplicate rows
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT a.four FROM
+  (SELECT four FROM tenk1 GROUP BY four) a,
+  (SELECT ten FROM tenk1 GROUP BY ten) b;
+                       QUERY PLAN                        
+---------------------------------------------------------
+ Unique
+   ->  Sort
+         Sort Key: tenk1_1.four
+         ->  Nested Loop
+               ->  HashAggregate
+                     Group Key: tenk1.ten
+                     ->  Seq Scan on tenk1
+               ->  Materialize
+                     ->  HashAggregate
+                           Group Key: tenk1_1.four
+                           ->  Seq Scan on tenk1 tenk1_1
+(11 rows)
+
+-- not elided: UNION ALL proves nothing
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT x FROM
+  (SELECT four AS x FROM tenk1 UNION ALL SELECT ten FROM tenk1) ss;
+              QUERY PLAN               
+---------------------------------------
+ HashAggregate
+   Group Key: tenk1.four
+   ->  Append
+         ->  Seq Scan on tenk1
+         ->  Seq Scan on tenk1 tenk1_1
+(5 rows)
+
+-- results sanity
+SELECT DISTINCT four FROM (SELECT four FROM tenk1 GROUP BY four) ss ORDER BY 1;
+ four 
+------
+    0
+    1
+    2
+    3
+(4 rows)
+
diff --git a/src/test/regress/sql/select_distinct.sql b/src/test/regress/sql/select_distinct.sql
index 2ed1616b098..773b1695436 100644
--- a/src/test/regress/sql/select_distinct.sql
+++ b/src/test/regress/sql/select_distinct.sql
@@ -274,3 +274,37 @@ SELECT DISTINCT y, x FROM distinct_tbl ORDER BY y;
 RESET enable_hashagg;
 
 DROP TABLE distinct_tbl;
+
+--
+-- Skip DISTINCT enforcement when the input is provably distinct already
+-- (structural proof via the input subquery's own DISTINCT/GROUP BY/setop).
+--
+-- elided: grouped subquery proves distinctness
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT four FROM (SELECT four FROM tenk1 GROUP BY four) ss;
+-- elided: superset of the unique columns is still unique
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT four, c FROM
+  (SELECT four, count(*) AS c FROM tenk1 GROUP BY four) ss;
+-- elided: aggregate without GROUP BY returns one row
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT c FROM (SELECT count(*) AS c FROM tenk1) ss;
+-- elided: non-ALL setop output is unique; WHERE cannot break uniqueness
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT x FROM
+  (SELECT four AS x FROM tenk1 UNION SELECT ten FROM tenk1) ss WHERE x > 2;
+-- not elided: subset of output that isn't the unique key
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT c FROM
+  (SELECT four, count(*) AS c FROM tenk1 GROUP BY four) ss;
+-- not elided: joining two unique subqueries can duplicate rows
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT a.four FROM
+  (SELECT four FROM tenk1 GROUP BY four) a,
+  (SELECT ten FROM tenk1 GROUP BY ten) b;
+-- not elided: UNION ALL proves nothing
+EXPLAIN (COSTS OFF)
+SELECT DISTINCT x FROM
+  (SELECT four AS x FROM tenk1 UNION ALL SELECT ten FROM tenk1) ss;
+-- results sanity
+SELECT DISTINCT four FROM (SELECT four FROM tenk1 GROUP BY four) ss ORDER BY 1;
