You may also need to do some typecasts to get rid of the error due to operation overflows
I just wanted to illustrate that in some cases, the issue is not just that you need a BIGINT column, but that some intermediate operation is overflowing.
For example, the following gives ERROR: integer out of range, even though we made the column BIGINT:
CREATE TABLE tmp(i BIGINT);
INSERT INTO tmp SELECT 2147483647 + 1;
The problem is that 2147483647 = 2**31 - 1, the maximum integer that fits into INTEGER, so when we add 1 it overflows and we get the error.
The same issue happens if we just SELECT without any tables involved:
SELECT 2147483647 + 1;
To solve the issue, we could typecast either as:
SELECT 2147483647::BIGINT + 1;
or as:
SELECT 2147483647 + 1::BIGINT;
so we understand that so long one of the operators is BIGINT, the result gets implicitly typecast without error.
It is also worth noting that
SELECT 2147483648 + 1;
does not give any error because when we use a 2147483648 literal, that doesn't fit into INTEGER, so PostgreSQL assumes it is BIGINT by default.
generate_series typecasting
Another case where the issue might come up is when using generate_series to generate some large test data, this is what brought me here in the first place, e.g.:
SELECT i + 1 FROM generate_series(2147483647, 2147483647) AS s(i);
gives the error for similar reasons as above, because if the arguments of generate_series are INTEGER, then so are the returned values. One good clean solution in this case is to typecast the arguments of generate_series to BIGINT as in:
SELECT i + 1 FROM generate_series(2147483647::BIGINT, 2147483647::BIGINT) AS s(i);
Tested on PostgreSQL 16.6, Ubuntu 24.04.1.
1403184512.2283964and1403184662.118respectively, both are fine and does not affect the result in any way what so ever. Also they are placed at the beginning of both the insert clumn definitions and the value definitions. So the position is not the issue here.idgenerator has passed 2^31?select max(id) from raw. You also might try changing the type of ID from SERIAL (4 byte signed integer) to BIGSERIAL (8 byte signed integer). Share and enjoy.select currval('raw_id_seq')return? (The name of your sequence might be different; mine is PostgreSQL's default.)