Re: SLOPE - Planner optimizations on monotonic expressions.

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: Alexandre Felipe <o(dot)alexandre(dot)felipe(at)gmail(dot)com>
Cc: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: SLOPE - Planner optimizations on monotonic expressions.
Date: 2026-11-24 05:22:10
Message-ID: CALdSSPivuUNTgouyn0UGzcY5tjFzeMby_rT+ZEG6AwT_5dGr3A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 23 Sept 2026 at 13:27, Alexandre Felipe
<o(dot)alexandre(dot)felipe(at)gmail(dot)com> 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-11-24 05:22:33 Re: REINDEX CONCURRENTLY reports lockers_done > lockers_total
Previous Message Dilip Kumar 2026-11-24 05:17:03 Re: Bug in logical decoding with DDL and subtransactions