Re: BUG #16624: Query Optimizer - Performance bug related to predicate simplification - Mailing list pgsql-bugs

From Xinyu Liu
Subject Re: BUG #16624: Query Optimizer - Performance bug related to predicate simplification
Date
Msg-id CAMQyWW20j6ex59U6B7NeBi4dB1o6EmphtPi0C1oyn4PVt3gfJw@mail.gmail.com
Whole thread Raw
In response to Re: BUG #16624: Query Optimizer - Performance bug related to predicate simplification  (Peter Geoghegan <pg@bowt.ie>)
Responses Re: BUG #16624: Query Optimizer - Performance bug related to predicate simplification  (Peter Geoghegan <pg@bowt.ie>)
List pgsql-bugs
Thank you so much for the helpful discussion!

On Wed, Sep 23, 2020 at 4:49 PM Peter Geoghegan <pg@bowt.ie> wrote:
On Wed, Sep 23, 2020 at 12:48 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> If you were really intent on making this happen, you could imagine
> for instance attaching a SupportRequestSimplify hook to "int4eq",
> which could transform "x - c1 = c2" into "x = c1 + c2" after verifying
> that the constant values wouldn't produce overflow.  But this idea
> doesn't scale very well, since you'd really be talking about looking
> for a whole bunch of different possible rearrangements not just one.

Yeah. Apparently even sophisticated C compilers can only perform
simpler algebraic reductions on integers, even though it probably
matters a lot more than it would during database query optimization
[1]. It's not as universal as you might think at first. Algebraic
reductions are unsafe with floats because you risk losing precision. I
bet that there are various subtle behaviors that get broken once you
think it all through for each datatype.

It's not that hard to create algebraic expressions involving integers
that production quality C compilers cannot perform relatively obvious
algebraic reductions on. I'm not an expert by any means, but I suspect
that it's a surprisingly hard problem.

As you said, the fix for this problem is don't do that. People that
write SQL for a living all seem to figure that out on their own. It's
not something we hear any complaints about.

[1] https://www.agner.org/optimize/optimizing_cpp.pdf
--
Peter Geoghegan


--
-Xinyu

pgsql-bugs by date:

Previous
From: David Rowley
Date:
Subject: Re: BUG #15383: Join Filter cost estimation problem in 10.5
Next
From: PG Bug reporting form
Date:
Subject: BUG #16635: Query Optimizer - Performance optimization for the UNION function