Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap rebind in dummy offset 0 #289

Merged
merged 1 commit into from
Oct 29, 2023
Merged

Wrap rebind in dummy offset 0 #289

merged 1 commit into from
Oct 29, 2023

Conversation

shane-circuithub
Copy link
Contributor

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:

```haskell
SELECT
  x * y + x * y
FROM (
  SELECT
    a + b + c AS x
    d + e + f AS y
  FROM
    foo
) _
```

into:

```haskell
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.

```haskell
SELECT
  x * y + x * y
FROM (
  SELECT
    a + b + c AS x
    d + e + f AS y
  FROM
    foo
  OFFSET
    0
) _
```
@shane-circuithub shane-circuithub merged commit 4a77272 into master Oct 29, 2023
2 checks passed
@shane-circuithub shane-circuithub deleted the rebind-offset-0 branch October 29, 2023 19:40
shane-circuithub added a commit that referenced this pull request Oct 29, 2023
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:

```haskell
SELECT
  x * y + x * y
FROM (
  SELECT
    a + b + c AS x
    d + e + f AS y
  FROM
    foo
) _
```

into:

```haskell
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.

```haskell
SELECT
  x * y + x * y
FROM (
  SELECT
    a + b + c AS x
    d + e + f AS y
  FROM
    foo
  OFFSET
    0
) _
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant