pgx icon indicating copy to clipboard operation
pgx copied to clipboard

Cann't save uint64 , error: "value is greater than maximum value for Int4"

Open mahsandr opened this issue 4 years ago • 10 comments

Hi, I want to save the value of a uint64 variable in a table, the target column type is "BIGINT", but I get the error "868957043361742 is greater than maximum value for Int4" I have no error when I save this value directly in the table. I used pgxpool.Pool to connect to the database

mahsandr avatar Jul 25 '21 14:07 mahsandr

Only reason that should happen is if the driver thinks the column is an int4. Maybe double check the underlying type. Maybe try with a test table with bigint to confirm.

jackc avatar Jul 25 '21 15:07 jackc

In my case it was #1248

stokito avatar Oct 26 '22 22:10 stokito

Same problem:

SELECT NULL
FROM hgv.companies
WHERE id = $1

$1 is 2551565154

failed to encode args[0]: unable to encode 2551565154 into binary format for int4 (OID 23): 2551565154 is greater than maximum value for int4

column type is bigint, actually the following works:

SELECT NULL
FROM hgv.companies
WHERE id = $1::bigint

emaborsa avatar Mar 01 '23 14:03 emaborsa

@emaborsa What type is hgv.companies.id? cc @jackc

stokito avatar Mar 01 '23 14:03 stokito

@emaborsa What type is hgv.companies.id? cc @jackc

it is bigint

emaborsa avatar Mar 01 '23 15:03 emaborsa

Can you produce a standalone case that reproduces this?

jackc avatar Mar 01 '23 15:03 jackc

Yes, need some time...

emaborsa avatar Mar 01 '23 15:03 emaborsa

Sorry, I was wrong, hgv.company.id is int, not bigint. However, I am wondering why it works if I cast the parameter to bigint.

emaborsa avatar Mar 02 '23 07:03 emaborsa

pgx sends the SQL text to the PostgreSQL server to make a prepared statement. PostgreSQL responds with the data type of the parameters. When all you have is id = $1 PostgreSQL infers that $1 is the same type as id. But when you have $1::bigint you are telling it the type of $1 is bigint.

jackc avatar Mar 02 '23 14:03 jackc

Has this issue been resolved?

Mrmanbanpai avatar Jul 04 '24 03:07 Mrmanbanpai