Closed raptr0n closed 2 months ago
This is not (directly) a problem with pgx but the implicit interaction with how postgres works. When executing PREPARE stmt AS SELECT NULLIF($1, 0)
(what pgx does internally), the statement becomes effectively PREPARE stmt AS SELECT NULLIF($1, 0)::int
due to how type inference with NULLIF
works. Writing either NULLIF($1::float, 0)
or NULLIF($1, 0::float)
will solve the problem (or in your case numeric).
Though pgx could do things differently with how statements are prepared, this might cause more problems than it solves.
Yes, that absolutely worked! Thanks for your assistance and insight.
Description:
When using the
NULLIF
function in an insert query with thepgx
library, I encountered an issue where afloat64
value that should be inserted into aNUMERIC(9,3)
column loses precision and is rounded off incorrectly.Steps to Reproduce:
Create a
test
table in PostgreSQL with the following schema:Write a Go function that inserts data into the
test
table using thepgx
library andNULLIF
for thenumber
column:Insert a value into the table with the following data:
Verify the inserted value by querying the database:
Expected Behavior:
The value
1000.170
should be inserted into thenumber
column with full precision, as the column type isNUMERIC(9,3)
.Actual Behavior:
When using
NULLIF($1, 0)
for thenumber
column, the value is inserted as1000.000
instead of1000.170
.However, when
NULLIF($1, 0)
is removed and$1
is passed directly, the value is inserted correctly as1000.170
.Additional Information:
1.23.1
v5.7.1
14.12