On 29/11/2026 02:56, Tender Wang wrote:
> Hi,
> 
> Chengpeng Yan <[email protected]> 于2026年11月25日周三 21:09写道:
>> Thanks for working on this. I reviewed v1 and it looks good to me. I
>> wonder if it's worth adding a negative test with an index on (x, y, w
>> DESC, z DESC) and a query with ORDER BY y, x, z DESC, w DESC. If the
>> plan uses that index, it should still sort before Unique to pick the
>> right row.

The feature looks promising.

This patch adds to the search scope the possibility of performing a UNIQUE
operation before the sort, which is good for a couple of reasons:
1. UNIQUE will probably remove some fraction of duplicated rows.
2. Top-N bounded sort may be applied more frequently.

So, let's discuss regressions. At first glance, executor-side regression looks
well-contained. I can imagine only one case: when Unique -> Sort -> SeqScan is
still effective, but the patched version already picks Sort -> Unique -> Index
Scan because of a cost misestimation.

But with planning-time regressions, it might be more difficult to identify a
specific case. For example, in [1], a trivial optimisation that added one more
sort to pathlist caused huge planning overhead (more than 100x in production
with 100+ joins), which forced me to limit its scope.

So, after reviewing this feature, I found no non-linear growth in planning
effort for now. But it might still add ~40% growth in memory and planning time
(see repro in the attachment). It may be important, because the feature itself
covers a quite rare case. Am I wrong?

[1]
https://www.postgresql.org/message-id/[email protected]

-- 
regards, Andrei Lepikhov,
pgEdge
BEGIN;

DROP TABLE IF EXISTS f, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13;

CREATE TABLE f (
  x int, y int, z int, c int,
  k1 int, k2 int, k3 int, k4 int, k5 int, k6 int, k7 int, k8 int, k9 int, k10 
int, k11 int, k12 int, k13 int
);
CREATE INDEX f_x_y_c ON f (x, y, c);
CREATE INDEX f_x_y_z ON f (x, y, z);

CREATE TABLE d1 (a int PRIMARY KEY);
CREATE TABLE d2 (a int PRIMARY KEY);
CREATE TABLE d3 (a int PRIMARY KEY);
CREATE TABLE d4 (a int PRIMARY KEY);
CREATE TABLE d5 (a int PRIMARY KEY);
CREATE TABLE d6 (a int PRIMARY KEY);
CREATE TABLE d7 (a int PRIMARY KEY);
CREATE TABLE d8 (a int PRIMARY KEY);
CREATE TABLE d9 (a int PRIMARY KEY);
CREATE TABLE d10 (a int PRIMARY KEY);
CREATE TABLE d11 (a int PRIMARY KEY);
CREATE TABLE d12 (a int PRIMARY KEY);
CREATE TABLE d13 (a int PRIMARY KEY);

SET join_collapse_limit = 20;
SET from_collapse_limit = 20;
SET geqo_threshold = 18;

SET min_parallel_table_scan_size = 0;
SET min_parallel_index_scan_size = 0;

EXPLAIN (COSTS OFF, SUMMARY ON, MEMORY ON)
SELECT DISTINCT ON (f.y, f.x) f.x, f.y, f.z
FROM f
  JOIN d1 ON f.k1 = d1.a
  JOIN d2 ON f.k2 = d2.a
  JOIN d3 ON f.k3 = d3.a
  JOIN d4 ON f.k4 = d4.a
  JOIN d5 ON f.k5 = d5.a
  JOIN d6 ON f.k6 = d6.a
  JOIN d7 ON f.k7 = d7.a
  JOIN d8 ON f.k8 = d8.a
  JOIN d9 ON f.k9 = d9.a
  JOIN d10 ON f.k10 = d10.a
  JOIN d11 ON f.k11 = d11.a
  JOIN d12 ON f.k12 = d12.a
  JOIN d13 ON f.k13 = d13.a
WHERE f.c = 1
ORDER BY f.y, f.x, f.z LIMIT 1;

ROLLBACK;

/*
-- Master:

 Planning:
   Memory: used=1417332kB  allocated=1417865kB
 Planning Time: 3301.229 ms

-- Patched:
 Planning:
   Memory: used=1970672kB  allocated=1974921kB
 Planning Time: 5274.001 ms
*/

Reply via email to