Search code examples
c#.netpostgresqlnpgsql

Why am I getting an "out of range for bigint" error when deleting records through a function in Postgresql?


I'm working on writing a postgresql stored procedure to delete records from a table based on a certain criteria. If I find a match, then I delete the record.

Here is my stored procedure:

CREATE OR REPLACE FUNCTION DeleteSomeRecords(keys_param character varying)
RETURNS void AS
$$
BEGIN
    DELETE FROM public.SomeTable
    WHERE keyvalue = ANY(keys_param::bigint[]);
END;
$$ LANGUAGE plpgsql;

The field keyvalue is defined as bigint.

Here is how I call it from PgAdmin 4:

SELECT * FROM DeleteSomeRecords('{9226086781768122240,9506724121199784302}')

I get the following error.

ERROR: value "9226086781768122240" is out of range for type bigint CONTEXT: SQL statement "DELETE FROM public.SomeTable WHERE keyvalue= ANY(keys_param::bigint[])" PL/pgSQL function DeleteSomeRecords(character varying) line 3 at SQL statement

What am I doing wrong here?

Is there a better way to write this function?

I'm calling this function through a C#/.Net application using NpgSql. I have a List values which I convert to a comma separated string:

string sKeyValues = "{" + string.Join(",", keyValue) + "}";

Thanks


Solution

  • Cast it to numeric:

    WHERE keyvalue = ANY(keys_param::numeric[])