[YSQL] DISTINCT over UNION ALL or partitioned table fails with “could not find pathkey item to sort”
Author: egladyshCreated Jul 16, 2026Updated Sep 18, 2026
Labelskind/bugarea/ysqlpriority/medium2024.2 Backport Required2025.2 Backport Required2026.1 Backport Required
Jira Link: DB-22416
Description
Description
A DISTINCT query over a UNION ALL subquery fails during planning when distinct index scans are available.
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT);
CREATE INDEX t1_a_idx ON t1 (a ASC);
CREATE INDEX t2_a_idx ON t2 (a ASC);
INSERT INTO t1 SELECT i % 2 FROM generate_series(1, 1000) i;
INSERT INTO t2 SELECT i % 3 FROM generate_series(1, 1000) i;
ANALYZE t1;
ANALYZE t2;
SELECT DISTINCT x
FROM (
SELECT a FROM t1
UNION ALL
SELECT a FROM t2
) s(x)
ORDER BY 1;
Actual result
ERROR: could not find pathkey item to sort
Expected result
0
1
2Similar error occurs with partitioned tables too,
yugabyte=# CREATE TABLE pt(k INT, v INT) PARTITION BY RANGE (k);
CREATE TABLE
yugabyte=# CREATE TABLE pt1 PARTITION OF pt FOR VALUES FROM (0) TO (5);
CREATE TABLE
yugabyte=# CREATE TABLE pt2 PARTITION OF pt FOR VALUES FROM (5) TO (10);
CREATE TABLE
yugabyte=# CREATE INDEX pt_v_idx ON pt (v ASC);
CREATE INDEX
yugabyte=# INSERT INTO pt (SELECT i%10, i%3 FROM GENERATE_SERIES(1, 100) AS i);
INSERT 0 100
yugabyte=#
yugabyte=# EXPLAIN (COSTS OFF) SELECT DISTINCT v FROM pt ORDER BY 1;
ERROR: could not find pathkey item to sortIssue Type
kind/bug
Warning: Please confirm that this issue does not contain any sensitive information
- I confirm this issue does not contain any sensitive information.
Source: yugabyte/yugabyte-db