Is it possible to get the maximum value for BIGINT
type without hardcoding it?
I know that the limit is well known, however I wouldn't like to hardcode it.
A big integer is a binary integer with a precision of 63 bits. The range of big integers is -9223372036854775808 to +9223372036854775807.
PostgreSQL allows a type of integer type namely BIGINT . It requires 8 bytes of storage size and can store integers in the range of -9, 223, 372, 036, 854, 775, 808 to +9, 223, 372, 036, 854, 775, 807.
BIGINT is a numeric data type in PostgreSQL that stores the integer type values. One of the following data types is used to store a whole number in PostgreSQL: SMALLINT, INTEGER, or BIGINT. All these data types differ in storage size and range. If we talk about the BIGINT data type, it is used to store large values.
If we talk about the storage size of INT and BI GINT, the storage size of INT is 4 bytes while the storage size of BIGINT is double of that i.e.: 8 bytes.
Just for fun: Building on alexpods' answer, we could define a "generic" function (assuming two's complement) for all postgres integers:
create or replace function
minint(a anyelement)
returns anyelement
immutable
language sql
as $$
select ((a - a) - 1) << (8 * pg_column_size(a) - 1);
$$
;
create or replace
function maxint(a anyelement)
returns anyelement
immutable
language sql
as $$
select ~minint(a)
$$
;
Usage:
select
minint(0::smallint)
, maxint(0::smallint)
, minint(0::int)
, maxint(0::int)
, minint(0::bigint)
, maxint(0::bigint)
;
Result:
minint | maxint | minint | maxint | minint | maxint
--------+--------+-------------+------------+----------------------+---------------------
-32768 | 32767 | -2147483648 | 2147483647 | -9223372036854775808 | 9223372036854775807
(1 row)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With