Skip to content

Conversation

rafiss
Copy link
Contributor

@rafiss rafiss commented Feb 14, 2023

PostgreSQL allows you to label a function as immutable even if it is not. But this can lead to non-deterministic results.

See:
https://www.postgresql.org/docs/current/sql-createfunction.html

IMMUTABLE indicates that the function cannot modify the database and
always returns the same result when given the same argument values;
that is, it does not do database lookups or otherwise use information
not directly present in its argument list. If this option is given,
any call of the function with all-constant arguments can be immediately
replaced with the function value.

And:
https://www.postgresql.org/docs/current/xfunc-volatility.html

Labeling a function IMMUTABLE when it really isn't might allow it
to be prematurely folded to a constant during planning, resulting
in a stale value being re-used during subsequent uses of the plan.
This is a hazard when using prepared statements or when using function
languages that cache plans (such as PL/pgSQL).
...
It is generally unwise to select from database tables within an IMMUTABLE
function at all, since the immutability will be broken if the table contents
ever change. However, PostgreSQL does not enforce that you do not do that.

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. Does your submission pass tests?
  2. Does ./gradlew autostyleCheck checkstyleAll pass ?
  3. Have you added your new test classes to an existing test suite in alphabetical order?

Changes to Existing Features:

  • Does this break existing behaviour? If so please explain.
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully run tests with your changes locally?

PostgreSQL allows you to label a function as immutable even if it is
not. But this can lead to non-deterministic results. See:

https://www.postgresql.org/docs/current/sql-createfunction.html
```
IMMUTABLE indicates that the function cannot modify the database and
always returns the same result when given the same argument values;
that is, it does not do database lookups or otherwise use information
not directly present in its argument list. If this option is given,
any call of the function with all-constant arguments can be immediately
replaced with the function value.
```

And:
https://www.postgresql.org/docs/current/xfunc-volatility.html
```
Labeling a function IMMUTABLE when it really isn't might allow it
to be prematurely folded to a constant during planning, resulting
in a stale value being re-used during subsequent uses of the plan.
This is a hazard when using prepared statements or when using function
languages that cache plans (such as PL/pgSQL).
...
It is generally unwise to select from database tables within an IMMUTABLE
function at all, since the immutability will be broken if the table contents
ever change. However, PostgreSQL does not enforce that you do not do that.
```
@davecramer davecramer merged commit 248b2ee into pgjdbc:master Feb 14, 2023
@vlsi vlsi added the chore label Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants