0
0
PostgreSQLquery~15 mins

Integer types (smallint, integer, bigint) in PostgreSQL - Deep Dive

Choose your learning style9 modes available
Overview - Integer types (smallint, integer, bigint)
What is it?
Integer types in PostgreSQL are used to store whole numbers without decimals. There are three main sizes: smallint, integer, and bigint. Each type can hold numbers within a specific range, with smallint being the smallest and bigint the largest. These types help the database efficiently store and process numeric data.
Why it matters
Using the right integer type saves storage space and improves performance. If you use a type too large, you waste space; too small, and you risk errors from numbers that don't fit. Without these types, databases would be slower and less efficient, making applications sluggish and costly.
Where it fits
Before learning integer types, you should understand basic data types and how databases store data. After this, you can learn about numeric operations, indexing, and performance tuning related to data types.
Mental Model
Core Idea
Integer types are like containers of different sizes that hold whole numbers within fixed limits to balance storage space and range.
Think of it like...
Think of integer types as jars of different sizes: a small jar (smallint) holds fewer candies (numbers), a medium jar (integer) holds more, and a large jar (bigint) holds the most. Choosing the right jar means you don’t waste space or run out of room.
┌───────────────┐
│  smallint     │ 2 bytes │ Range: -32,768 to 32,767
├───────────────┤
│  integer      │ 4 bytes │ Range: -2,147,483,648 to 2,147,483,647
├───────────────┤
│  bigint       │ 8 bytes │ Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
└───────────────┘
Build-Up - 7 Steps
1
FoundationWhat are integer types?
🤔
Concept: Introduction to integer types as whole number storage in databases.
Integer types store numbers without fractions or decimals. PostgreSQL offers three sizes: smallint, integer, and bigint. Each uses a different number of bytes and supports different ranges of numbers.
Result
You understand that integer types are for whole numbers and that there are three sizes with different ranges.
Understanding that integers are whole numbers without decimals is the base for choosing the right type.
2
FoundationRanges and storage sizes
🤔
Concept: Each integer type has a fixed size in bytes and a specific range of values it can hold.
Smallint uses 2 bytes and stores numbers from -32,768 to 32,767. Integer uses 4 bytes and stores from -2,147,483,648 to 2,147,483,647. Bigint uses 8 bytes and stores very large numbers, up to about 9 quintillion.
Result
You know the exact storage size and range for each integer type.
Knowing the size and range helps prevent errors and optimize storage.
3
IntermediateChoosing the right integer type
🤔Before reading on: do you think using bigint for all numbers is better than choosing smaller types? Commit to your answer.
Concept: Selecting the appropriate integer type based on the expected range of values.
If your numbers never exceed 32,767, use smallint to save space. For most common cases, integer is enough. Use bigint only when you expect very large numbers. Using bigger types than needed wastes storage and can slow down queries.
Result
You can pick the best integer type for your data needs.
Understanding the trade-off between range and storage size improves database efficiency.
4
IntermediateInteger overflow and errors
🤔Before reading on: what happens if you insert a number too big for smallint? Will PostgreSQL store it, truncate it, or error? Commit to your answer.
Concept: What occurs when a number exceeds the range of the chosen integer type.
If you try to insert a number outside the allowed range, PostgreSQL throws an error and refuses the insert. It does not truncate or wrap the number. This protects data integrity but requires careful type choice.
Result
You understand that exceeding range causes errors, not silent data corruption.
Knowing this prevents bugs and data loss from unexpected number sizes.
5
IntermediateImplicit type casting and arithmetic
🤔
Concept: How PostgreSQL handles arithmetic and type conversions between integer types.
When you mix integer types in calculations, PostgreSQL promotes smaller types to larger ones to avoid overflow. For example, smallint plus integer results in integer. This automatic casting helps avoid errors but can affect performance.
Result
You know how PostgreSQL manages mixed integer operations safely.
Understanding implicit casting helps predict query behavior and optimize performance.
6
AdvancedStorage and performance impact
🤔Before reading on: do you think using bigint instead of integer always slows down queries? Commit to your answer.
Concept: How integer type size affects storage space and query speed.
Smaller integer types use less disk space and memory, which can speed up queries and reduce I/O. However, modern CPUs handle 4- and 8-byte integers efficiently, so the difference is often small. Still, for very large tables, choosing the smallest adequate type improves performance.
Result
You understand the practical performance trade-offs of integer types.
Knowing when size matters helps optimize large-scale databases.
7
ExpertPostgreSQL internal integer representation
🤔Before reading on: do you think PostgreSQL stores integers as text or binary internally? Commit to your answer.
Concept: How PostgreSQL stores integer types internally in binary form for efficiency.
PostgreSQL stores integers in fixed-length binary format: 2 bytes for smallint, 4 bytes for integer, and 8 bytes for bigint. This binary storage allows fast arithmetic and indexing. Internally, integers are signed and use two's complement representation.
Result
You know the internal binary storage and representation of integers in PostgreSQL.
Understanding internal storage explains why integer operations are fast and how range limits arise.
Under the Hood
PostgreSQL stores integer types as fixed-size binary values using two's complement encoding. This means each integer type uses a fixed number of bytes (2, 4, or 8) to represent signed whole numbers. Arithmetic operations and comparisons work directly on these binary values, making them very fast. When data is read or written, PostgreSQL converts between this binary form and human-readable text.
Why designed this way?
Fixed-size binary storage was chosen for speed and simplicity. Two's complement is a standard way to represent signed integers in computers, allowing easy arithmetic and comparison. Alternatives like variable-length storage would slow down operations and complicate indexing. The three sizes balance storage efficiency and range for common use cases.
┌─────────────┐
│ Input Text  │
└──────┬──────┘
       │ Parse to binary
┌──────▼──────┐
│ Binary Int  │ 2/4/8 bytes
│ (two's comp)│
└──────┬──────┘
       │ Arithmetic & Indexing
┌──────▼──────┐
│ Storage on  │
│ Disk & RAM  │
└─────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does using bigint always make your database faster? Commit yes or no.
Common Belief:Using bigint is always better because it can store bigger numbers and is faster.
Tap to reveal reality
Reality:Bigint uses more storage and can be slower than smaller integer types, especially on large datasets. It should only be used when needed.
Why it matters:Using bigint unnecessarily wastes space and can degrade performance, increasing costs and slowing queries.
Quick: Can you store decimal numbers in integer types? Commit yes or no.
Common Belief:Integer types can store decimal numbers by rounding or truncating.
Tap to reveal reality
Reality:Integer types only store whole numbers. Trying to insert decimals causes errors or truncation depending on context, but PostgreSQL does not automatically round decimals to integers.
Why it matters:Misusing integer types for decimals leads to data loss or errors, causing incorrect results.
Quick: If you insert a number too big for smallint, does PostgreSQL store it anyway? Commit yes or no.
Common Belief:PostgreSQL will store the number by truncating or wrapping around if it’s too big for smallint.
Tap to reveal reality
Reality:PostgreSQL throws an error and refuses to store numbers outside the allowed range for the type.
Why it matters:Assuming silent truncation leads to unexpected errors and data integrity problems.
Quick: Is integer the same as int in PostgreSQL? Commit yes or no.
Common Belief:Integer and int are different types with different sizes.
Tap to reveal reality
Reality:Integer and int are synonyms in PostgreSQL and mean the same 4-byte integer type.
Why it matters:Confusing these can cause misunderstandings when reading or writing SQL code.
Expert Zone
1
PostgreSQL uses two's complement binary representation internally, which affects how negative numbers and overflow behave.
2
When mixing integer types in expressions, PostgreSQL promotes smaller types to larger ones to avoid overflow, which can impact performance subtly.
3
Choosing integer types affects index size and query planning, influencing overall database performance beyond just storage.
When NOT to use
Avoid integer types when you need fractional numbers; use numeric or decimal types instead. Also, for very large numbers beyond bigint range, use numeric type. For boolean flags or small enumerations, consider using boolean or enum types instead of smallint.
Production Patterns
In production, use smallint for small ranges like status codes, integer for most IDs and counts, and bigint for large counters or IDs in distributed systems. Monitor data growth to adjust types before overflow errors occur. Use explicit casting to control arithmetic behavior and avoid surprises.
Connections
Data Types
Integer types are a subset of data types used to define how data is stored and interpreted.
Understanding integer types helps grasp the broader concept of data types and their role in database design.
Computer Architecture
Integer types in databases reflect how computers represent numbers in binary using fixed bytes.
Knowing computer number representation clarifies why integer ranges and sizes exist as they do.
Memory Management
Choosing integer sizes affects memory and storage allocation in databases.
Understanding memory management principles helps optimize database performance by selecting appropriate integer types.
Common Pitfalls
#1Using smallint for a column that stores values larger than 32,767.
Wrong approach:CREATE TABLE sales (amount smallint); INSERT INTO sales VALUES (40000);
Correct approach:CREATE TABLE sales (amount integer); INSERT INTO sales VALUES (40000);
Root cause:Misunderstanding the range limits of smallint leads to overflow errors.
#2Using bigint for all integer columns without need.
Wrong approach:CREATE TABLE users (id bigint, age bigint);
Correct approach:CREATE TABLE users (id bigint, age smallint);
Root cause:Not considering storage size and performance trade-offs causes inefficient database design.
#3Trying to store decimal numbers in integer columns.
Wrong approach:CREATE TABLE products (price integer); INSERT INTO products VALUES (19.99);
Correct approach:CREATE TABLE products (price numeric(5,2)); INSERT INTO products VALUES (19.99);
Root cause:Confusing integer types with decimal types causes data loss or errors.
Key Takeaways
Integer types store whole numbers in fixed-size containers with specific ranges.
Choosing the right integer type balances storage efficiency and the range of values needed.
Exceeding an integer type’s range causes errors, not silent data corruption.
PostgreSQL stores integers in binary using two's complement for fast arithmetic.
Using unnecessarily large integer types wastes space and can reduce performance.