Postgres explain plan is not correctly optimezed if the data is a "double" but the collumn is a "numeric" #3285
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In Postgres, if we have a collumn stored as a "numeric", but store "double in it", the execution plan will not be optimized.
Driver Version
postgresql-42.7.3
Java Version
JDK 21
To Reproduce
select id from invoice i join contract c on c.id = i.contract_id where i.amount between ? and ?
When we use a
PreparedStatement
, the system should use an implicite cast as a numeric to calculate the execution plan, but it used('the value')::double precision
, so the execution plan could be false leading to performance issue (in my case, we have 8 million + invoice in the database, and the prepared request take 3 sec to execute)The problème is generate by this change : 06abfb7
Now, the executed request for data
-10
and3.4
will beselect id from invoice i join contract c on c.id = i.contract_id where i.amount between ('-10'::double precision) and ('3.4'::double precision)
.As I understand, this modification is due to a security problème with thing like
-?
that could generate son SQL injection if the value is a negative number.Correction proposition
Instead of using a cast, only put the data between parenthesis for number, so we have
-?
becomming-(-2.548)
istead of-('-2.548'::double precision)
Fixes #3284