Re: Convert ALL SubLinks to ANY SubLinks

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: Rui Zhao <zhaorui126(at)gmail(dot)com>
Cc: Richard Guo <guofenglinux(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Convert ALL SubLinks to ANY SubLinks
Date: 2026-12-05 13:24:38
Message-ID: CALdSSPjvpBMK9pQkGiLVgxomoBk8eg2oUAZjM7cHbp7Cvi5+=Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, 9 Nov 2026 at 12:50, Rui Zhao <zhaorui126(at)gmail(dot)com> wrote:
>
> Hi Richard,

Hi all!

> Thanks for working on this optimization.
>
> I found a valid custom operator class for which v2 turns a working
> row-comparison ALL query into an error. Using the existing int4_tbl
> regression table:
>
> CREATE OPERATOR #< (LEFTARG = integer, RIGHTARG = integer,
> FUNCTION = int4lt, NEGATOR = #>=);
> CREATE OPERATOR #>= (LEFTARG = integer, RIGHTARG = integer,
> FUNCTION = int4ge, NEGATOR = #<);
> CREATE OPERATOR CLASS regress_all_int_ops
> FOR TYPE integer USING btree AS
> OPERATOR 1 #<,
> OPERATOR 2 <=,
> OPERATOR 3 =,
> OPERATOR 4 >=,
> OPERATOR 5 >,
> FUNCTION 1 btint4cmp(integer, integer);
>
> SELECT f1 FROM int4_tbl
> WHERE (f1, f1) OPERATOR(#<) ALL
> (SELECT f1, f1 FROM int4_tbl WHERE f1 = 123456)
> ORDER BY f1;
>
> Master returns -2147483647, -123456 and 0. On v2 I get:
>
> ERROR: operator 16417 is not a member of opfamily 16419
>
> NOT ALL fails too. The operator class passes amvalidate(). Its strategy
> 4 operator is the built-in >=, while the negator of #< is #>=. Both >=
> and #>= call int4ge, but only >= belongs to this family.

+1 I can reproduce this.

I found a performance counterexample for v2 when the rewritten ANY
uses a hashed SubPlan.

CREATE TEMP TABLE outer_t(k int NOT NULL);
INSERT INTO outer_t VALUES (42);

CREATE TEMP TABLE inner_t(id int PRIMARY KEY, k int);
INSERT INTO inner_t
SELECT g, CASE WHEN g = 1 THEN 42 ELSE 0 END
FROM generate_series(1, 100000) AS g;

ANALYZE inner_t;

EXPLAIN (ANALYZE, BUFFERS, TIMING OFF)
SELECT count(*)
FROM outer_t o
WHERE o.k <> ALL (
SELECT i.k
FROM inner_t i
ORDER BY i.id
LIMIT 100000
);

Master:

QUERY
PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=4839945.36..4839945.37 rows=1 width=8) (actual
rows=1.00 loops=1)
Buffers: local hit=5 dirtied=1 written=1
-> Seq Scan on outer_t o (cost=0.29..4839942.17 rows=1275
width=0) (actual rows=0.00 loops=1)
Filter: (ALL (k <> (SubPlan all_1).col1))
Rows Removed by Filter: 1
Buffers: local hit=5 dirtied=1 written=1
SubPlan all_1
-> Materialize (cost=0.29..3546.29 rows=100000 width=8)
(actual rows=1.00 loops=1)
Storage: Memory Maximum Storage: 17kB
Buffers: local hit=4 dirtied=1 written=1
-> Limit (cost=0.29..3046.29 rows=100000 width=8)
(actual rows=1.00 loops=1)
Buffers: local hit=4 dirtied=1 written=1
-> Index Scan using inner_t_pkey on inner_t i
(cost=0.29..3046.29 rows=100000 width=8) (actual rows=1.00 loops=1)
Index Searches: 1
Buffers: local hit=4 dirtied=1 written=1
Planning:
Buffers: shared hit=28 read=4, local hit=1
Planning Time: 0.160 ms
Execution Time: 0.126 ms
(19 rows)

v2:

QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=3341.36..3341.37 rows=1 width=8) (actual rows=1.00 loops=1)
Buffers: local hit=1161 dirtied=1 written=1
-> Seq Scan on outer_t o (cost=3296.29..3338.17 rows=1275
width=0) (actual rows=0.00 loops=1)
Filter: (NOT (ANY (k = (hashed SubPlan any_1).col1)))
Rows Removed by Filter: 1
Buffers: local hit=1161 dirtied=1 written=1
SubPlan any_1
-> Limit (cost=0.29..3046.29 rows=100000 width=8) (actual
rows=100000.00 loops=1)
Buffers: local hit=1160 dirtied=1 written=1
-> Index Scan using inner_t_pkey on inner_t i
(cost=0.29..3046.29 rows=100000 width=8) (actual rows=100000.00
loops=1)
Index Searches: 1
Buffers: local hit=1160 dirtied=1 written=1
Planning:
Buffers: shared hit=28 read=4, local hit=1
Planning Time: 0.173 ms
Execution Time: 8.029 ms
(16 rows)

With one outer row and a nullable 100,000-row RHS, master reads one
RHS row and finishes fast. V2 builds the hash table from all 100,000
rows and takes much longer.

Also, the RHS is nullable to prevent anti-join pullup, with
non-nullable side OFFSET 0 gives same regression. So looks like we
need to somehow account for cases where the outer side is small and
ALL can stop early. Or maybe this regression is only for narrow cases
so we can leave as-is?

--
Best regards,
Kirill Reshke

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Álvaro Herrera 2026-12-05 10:31:39 Re: Avoid logging dropped TOAST values with REPLICA IDENTITY FULL