Integer types (smallint, integer, bigint) in PostgreSQL - Time & Space Complexity
When working with integer types in PostgreSQL, it's helpful to understand how operations on these types scale as data grows.
We want to see how the time to process integers changes when using smallint, integer, or bigint types.
Analyze the time complexity of inserting and querying integer values of different sizes.
-- Create a table with an integer column
CREATE TABLE numbers (
id SERIAL PRIMARY KEY,
value INTEGER
);
-- Insert n integer values
INSERT INTO numbers (value)
SELECT generate_series(1, n);
-- Query all values
SELECT * FROM numbers WHERE value > 0;
This code inserts n integer values and then queries all rows with a simple condition.
Look for repeated actions that affect performance.
- Primary operation: Inserting and scanning n rows in the table.
- How many times: Each row is inserted once and scanned once during the query.
As the number of rows n increases, the work to insert and query grows roughly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 insert and 10 scan operations |
| 100 | About 100 insert and 100 scan operations |
| 1000 | About 1000 insert and 1000 scan operations |
Pattern observation: The operations increase linearly as n grows.
Time Complexity: O(n)
This means the time to insert and query grows directly with the number of rows.
[X] Wrong: "Using bigint instead of integer will slow down queries significantly because bigger numbers are harder to process."
[OK] Correct: The difference in processing time between smallint, integer, and bigint is very small and usually does not affect query time noticeably. The main factor is the number of rows, not the integer size.
Understanding how data size affects query time helps you write efficient database code and explain your reasoning clearly in interviews.
"What if we added an index on the integer column? How would the time complexity of queries change?"
Practice
Solution
Step 1: Understand integer types and their sizes
smallintstores small numbers,integerstores medium-sized numbers, andbigintstores very large numbers.Step 2: Identify the type for very large whole numbers
Since the question asks for very large whole numbers,bigintis the correct choice.Final Answer:
bigint -> Option AQuick Check:
Very large whole numbers = bigint [OK]
- Confusing smallint with bigint
- Choosing decimal which is for decimals, not integers
- Picking integer which is medium size
age as a 2-byte integer in PostgreSQL?Solution
Step 1: Recall PostgreSQL integer type syntax
PostgreSQL usesSMALLINTfor 2-byte integers. The syntax is simplycolumn_name SMALLINT;.Step 2: Evaluate each option
age SMALLINT;uses correct syntax. The other options incorrectly use size in parentheses (like INT2(2), INTEGER(2), BIGINT(2)), which is invalid for integer types in PostgreSQL.Final Answer:
age SMALLINT; -> Option DQuick Check:
2-byte integer = SMALLINT syntax [OK]
- Adding size in parentheses like INTEGER(2)
- Using BIGINT for small numbers
- Specifying size like INT2(2) for integer types
CREATE TABLE test_numbers (num SMALLINT); INSERT INTO test_numbers VALUES (40000); SELECT * FROM test_numbers;
Solution
Step 1: Understand SMALLINT range
SMALLINTcan store values from -32768 to 32767 only.Step 2: Check inserted value against range
40000 is greater than 32767, so inserting it causes an error.Final Answer:
Error: integer out of range -> Option BQuick Check:
40000 > SMALLINT max 32767 = error [OK]
- Assuming value truncates silently
- Expecting 0 or NULL instead of error
- Confusing SMALLINT range with INTEGER
CREATE TABLE sales (id INTEGER, amount SMALLINT);
But when inserting a large amount like 40000, you get an error. How do you fix it?
Solution
Step 1: Identify cause of error
Amount column isSMALLINTwhich maxes at 32767, so 40000 is too large.Step 2: Choose appropriate type to hold 40000
INTEGERcan hold values up to 2,147,483,647, so changingamounttoINTEGERfixes the error.Final Answer:
Change amount column type to INTEGER -> Option AQuick Check:
40000 fits INTEGER but not SMALLINT [OK]
- Changing unrelated column type
- Trying to insert smaller values only
- Choosing
tinyintwhich is a MySQL type, not PostgreSQL
Solution
Step 1: Understand size limits of integer types
smallintmax is 32767,integermax is about 2,147,483,647,bigintsupports much larger numbers.Step 2: Match requirement to type
User IDs may exceed 2 billion, sointegeris insufficient.bigintis needed to store larger values.Final Answer:
bigint because it supports numbers larger than 2 billion -> Option CQuick Check:
IDs > 2 billion need bigint [OK]
- Choosing smallint for large IDs
- Assuming integer supports >2 billion
- Confusing serial with integer size
Ready to prove it?
Pass the test and this pattern moves unknown
