Skip to content

Commit c319efc

Browse files
committed
Add Integers In Postgres as a postgres til.
1 parent 2637845 commit c319efc

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
7979
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
8080
- [Fizzbuzz With Common Table Expressions](postgres/fizzbuzz-with-common-table-expressions.md)
8181
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
82+
- [Integers In Postgres](postgres/integers-in-postgres.md)
8283
- [Intervals Of Time By Week](postgres/intervals-of-time-by-week.md)
8384
- [Limit Execution Time Of Statements](postgres/limit-execution-time-of-statements.md)
8485
- [List All Columns Of A Specific Type](postgres/list-all-columns-of-a-specific-type.md)

postgres/integers-in-postgres.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Integers In Postgres
2+
3+
Postgres has three kinds of integers. Or rather three sizes of integers.
4+
There are `smallint` (`int2`), `integer` (`int4`), and `bigint` (`int8`)
5+
integers. As you might expect, they are 2 byte, 4 byte, and 8 byte integers
6+
respectively. They are also signed integers. All of this has implications
7+
for what ranges of integers can be represented by each type.
8+
9+
The `smallint` integers have 2 bytes to use, so they can be used to
10+
represent integers from -32768 to +32767.
11+
12+
The `integer` integers have 4 bytes to use, so they can be used to represent
13+
integers from -2147483648 to +2147483647.
14+
15+
The `bigint` integers have 8 bytes to use, so they can be used to represent
16+
integers from -9223372036854775808 to +9223372036854775807.
17+
18+
Though columns can be restricted to use a particular-sized integer, postgres
19+
is smart enough to default to `integer` and only use `bigint` as necessary
20+
when working with integers on the fly.
21+
22+
```sql
23+
> select pg_typeof(55);
24+
pg_typeof
25+
-----------
26+
integer
27+
28+
> select pg_typeof(99999999999999999);
29+
pg_typeof
30+
-----------
31+
bigint
32+
```

0 commit comments

Comments
 (0)