On Wed, 23 Sept 2026 at 13:27, Alexandre Felipe
<[email protected]> wrote:
>
> v16.0
>
> The previous version was producing sort in queries such as
> SELECT x, f(y) FROM tbl WHERE x = 1 ORDER BY f(y);
> Where the order of x is trivial as it is a constant for the query.
>
> Regards,
> Alexandre
>
Hi,
I checked v16.0 and looks like there is a case where SLOPE produces
an incorrect ordering and consequently a wrong LIMIT result.
Here is a repro:
CREATE TABLE t (x int, y int);
INSERT INTO t VALUES (1, 100), (2, 0);
CREATE INDEX ON t (x, y);
SET enable_seqscan = off;
SELECT x, y FROM t ORDER BY x / 10, y LIMIT 1;
-- SLOPE on - returns (1, 100) - wrong
SET enable_slope = off;
SELECT x, y FROM t ORDER BY x / 10, y LIMIT 1;
-- (2, 0) -- ok
This case would also be useful as a regression test.
I think here
@@ -1346,6 +1349,13 @@ build_index_pathkeys(PlannerInfo *root,
continue;
}
+ /*
+ * If the index column is pinned i.e. (constant EC) or it matched
+ * a previous pathkey, we can try the next index column.
+ */
+ if (pathkey_emitted || column_pinned)
+ break;
+
/*
* Index can't satisfy query pathkeys any further
*/
@@ -1365,7 +1375,7 @@ build_index_pathkeys(PlannerInfo *root,
column_pinned = true;
}
- if (!column_pinned)
+ if (!column_pinned && !pathkey_emitted)
{
/*
* Boolean index keys might be redundant even if they do not
pathkey_emitted can be true after matching x / 10, which could be
wrong (and is wrong in repro test).
We need to harden extending the index pathkeys here. The remaining
ordering could then be supplied by a Sort or Incremental Sort.
--
Best regards,
Kirill Reshke