mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Integers In Postgres as a postgres til.
This commit is contained in:
@@ -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)
|
||||
|
||||
32
postgres/integers-in-postgres.md
Normal file
32
postgres/integers-in-postgres.md
Normal file
@@ -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
|
||||
```
|
||||
Reference in New Issue
Block a user