Re: SLOPE - Planner optimizations on monotonic expressions.

From: Alexandre Felipe <o(dot)alexandre(dot)felipe(at)gmail(dot)com>
To: Kirill Reshke <reshkekirill(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-25 07:54:56
Message-ID: CAE8JnxOjKxyEUra6nXyVmtc158AyHUR=qj8gwoVJyWYjabKieg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Nov 24, 2026 at 5:22 AM Kirill Reshke <reshkekirill(at)gmail(dot)com>
wrote:

>
> Hi,
>
> I checked v16.0 and looks like there is a case where SLOPE produces
> an incorrect ordering and consequently a wrong LIMIT result.
>

Thank you for reviewing.

> SELECT x, y FROM t ORDER BY x / 10, y LIMIT 1;
> -- SLOPE on - returns (1, 100) - wrong

Agree, the term I coined for that was pinning, when a column
if a prefix can be constant without the underlying variable being constant
so in this case x / 10 doesn't pin the column. And since we are not
declaring injective functions x * 2 wouldn't pin it either.

select ts::date, ts, row_number() over (partition by ts::date order by ts)

Cover the cases where both f(x) and x are in the pathkeys and it works,
but the regression was missing one test where f(x) blocks prevents
using the index to order the suffix.

> This case would also be useful as a regression test.
>

Definitely,

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
>

It was :) got there independently, reading your hint as I reply.

> pathkey_emitted can be true after matching x / 10, which could be
> wrong (and is wrong in repro test).
>

The fix went like.

if (!column_pinned)
{
...
/*
* A slope match is not a unique prefix: f(x) can tie for distinct
* x, so the next index column does not order those ties unless x
* itself appeared in the ORDER BY (column_pinned above).
*/
if (pathkey_emitted)
break;
}
(trying to paste formatted code for the first time, let's see how it looks
after the list's sanitisation)

We need to harden extending the index pathkeys here. The remaining
> ordering could then be supplied by a Sort or Incremental Sort.
>

Please, see 0004 expected/slope.out
+-- ties on multi-column path keys

Regards,
Alexandre

Attachment Content-Type Size
v16.1-0005-SLOPE-Constant-sort-keys.patch application/octet-stream 9.4 KB
v16.1-0001-benchmark.patch application/octet-stream 4.8 KB
v16.1-0004-SLOPE-Planner-support.patch application/octet-stream 84.1 KB
v16.1-0002-Optimized-reverse-pathkeys.patch application/octet-stream 7.0 KB
v16.1-0003-SLOPE-catalog-changes.patch application/octet-stream 94.6 KB
v16.1-0008-SLOPE-timezone-errsave.patch application/octet-stream 12.0 KB
v16.1-0007-SLOPE-Timezone.patch application/octet-stream 54.0 KB
v16.1-0009-SLOPE-plan-cache-guards.patch application/octet-stream 12.9 KB
v16.1-0006-SLOPE-documentation.patch application/octet-stream 8.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Álvaro Herrera 2026-11-25 08:08:36 Re: remove unused shared WAL flush request
Previous Message Hayato Kuroda (Fujitsu) 2026-11-25 07:45:17 RE: Proposal: Conflict log history table for Logical Replication