From c319efc2282a582f86fbd3ee4950af2fe99c2e75 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 25 Sep 2015 23:56:38 -0500 Subject: [PATCH] Add Integers In Postgres as a postgres til. --- README.md | 1 + postgres/integers-in-postgres.md | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 postgres/integers-in-postgres.md diff --git a/README.md b/README.md index 3262b16..6d65c6c 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md) - [Fizzbuzz With Common Table Expressions](postgres/fizzbuzz-with-common-table-expressions.md) - [Generate Series Of Numbers](postgres/generate-series-of-numbers.md) +- [Integers In Postgres](postgres/integers-in-postgres.md) - [Intervals Of Time By Week](postgres/intervals-of-time-by-week.md) - [Limit Execution Time Of Statements](postgres/limit-execution-time-of-statements.md) - [List All Columns Of A Specific Type](postgres/list-all-columns-of-a-specific-type.md) diff --git a/postgres/integers-in-postgres.md b/postgres/integers-in-postgres.md new file mode 100644 index 0000000..610f664 --- /dev/null +++ b/postgres/integers-in-postgres.md @@ -0,0 +1,32 @@ +# Integers In Postgres + +Postgres has three kinds of integers. Or rather three sizes of integers. +There are `smallint` (`int2`), `integer` (`int4`), and `bigint` (`int8`) +integers. As you might expect, they are 2 byte, 4 byte, and 8 byte integers +respectively. They are also signed integers. All of this has implications +for what ranges of integers can be represented by each type. + +The `smallint` integers have 2 bytes to use, so they can be used to +represent integers from -32768 to +32767. + +The `integer` integers have 4 bytes to use, so they can be used to represent +integers from -2147483648 to +2147483647. + +The `bigint` integers have 8 bytes to use, so they can be used to represent +integers from -9223372036854775808 to +9223372036854775807. + +Though columns can be restricted to use a particular-sized integer, postgres +is smart enough to default to `integer` and only use `bigint` as necessary +when working with integers on the fly. + +```sql +> select pg_typeof(55); + pg_typeof +----------- + integer + +> select pg_typeof(99999999999999999); + pg_typeof +----------- + bigint +```