Somewhere between PostgreSQL 11 and PostgreSQL 15, PostgreSQL's optimiser gained the ability to see "through" subqueries, and it seems to choose to do this even when we don't really want it to.
E.g., it started transforming the following:
SELECT
x * y + x * y
FROM (
SELECT
a + b + c AS x
d + e + f AS y
FROM
foo
) _
into:
SELECT
(a + b + c) * (d + e + f) + (a + b + c) * (d + e + f)
FROM
foo
before evaluating.
You can see how more complicated expressions nested several levels deep could get expanded into crazy big expressions. This seems to be what PostgreSQL actually does on Rel8 code that uses rebind. Compared to older versions of PostgreSQL, this increases the planning time and execution time dramatically.
Given that Rel8's rebind is intended to function as a "let binding", and the user needs to go out of their way to choose to use it (they could just use pure if they wanted the fully expanded expression), we want a way to force PostgreSQL to evaluate the a + b + c and the d + e + f first before worrying about trying to simplify x * y + x * y. Adding OFFSET 0 to the inner query seems to achieve that.
SELECT
x * y + x * y
FROM (
SELECT
a + b + c AS x
d + e + f AS y
FROM
foo
OFFSET
0
) _
Somewhere between PostgreSQL 11 and PostgreSQL 15, PostgreSQL's optimiser gained the ability to see "through" subqueries, and it seems to choose to do this even when we don't really want it to.
E.g., it started transforming the following:
into:
before evaluating.
You can see how more complicated expressions nested several levels deep could get expanded into crazy big expressions. This seems to be what PostgreSQL actually does on Rel8 code that uses
rebind
. Compared to older versions of PostgreSQL, this increases the planning time and execution time dramatically.Given that Rel8's
rebind
is intended to function as a "let binding", and the user needs to go out of their way to choose to use it (they could just usepure
if they wanted the fully expanded expression), we want a way to force PostgreSQL to evaluate thea + b + c
and thed + e + f
first before worrying about trying to simplifyx * y + x * y
. AddingOFFSET 0
to the inner query seems to achieve that.