Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres maximum value for BIGINT

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.

like image 509
iggy Avatar asked Mar 10 '15 09:03

iggy


People also ask

What is the max value for BIGINT?

A big integer is a binary integer with a precision of 63 bits. The range of big integers is -9223372036854775808 to +9223372036854775807.

Does Postgres have BIGINT?

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.

What is BIGINT PostgreSQL?

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.

Does BIGINT take up more space?

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.


1 Answers

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)
like image 184
Adam Sosnowski Avatar answered Nov 16 '22 02:11

Adam Sosnowski