AGE function for differences in PostgreSQL - Time & Space Complexity
We want to understand how the time to calculate age differences grows as we use the AGE function more times.
How does the work change when we ask for many age calculations?
Analyze the time complexity of the following code snippet.
SELECT id, AGE(current_date, birth_date) AS age_diff
FROM users;
This query calculates the age difference between today and each user's birth date.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: AGE function calculation for each row in the users table.
- How many times: Once per user row, so as many times as there are users.
Each additional user means one more AGE calculation. The work grows steadily as the number of users grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 AGE calculations |
| 100 | 100 AGE calculations |
| 1000 | 1000 AGE calculations |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the number of rows increases.
[X] Wrong: "The AGE function runs once and applies to all rows at the same time."
[OK] Correct: Each row needs its own AGE calculation, so the function runs repeatedly, not just once.
Understanding how functions like AGE scale with data size helps you write efficient queries and explain their behavior clearly.
"What if we added a WHERE clause to filter only recent users? How would the time complexity change?"
Practice
AGE function return when given two timestamps?Solution
Step 1: Understand the AGE function purpose
The AGE function calculates the time difference between two timestamps.Step 2: Identify the output format
It returns the difference as an interval showing years, months, and days.Final Answer:
The difference between the two timestamps as an interval showing years, months, and days -> Option BQuick Check:
AGE returns interval difference [OK]
- Thinking AGE returns seconds or integer
- Confusing AGE with addition or subtraction of timestamps
- Assuming AGE returns a timestamp
'2023-01-01' and '2024-06-01' in PostgreSQL?Solution
Step 1: Check AGE function parameter order
AGE calculates the difference as first timestamp minus second timestamp, so the later date should come first.Step 2: Identify correct syntax with proper order
Using AGE('2024-06-01', '2023-01-01') returns the positive interval difference.Final Answer:
SELECT AGE('2024-06-01', '2023-01-01'); -> Option AQuick Check:
Later date first in AGE [OK]
- Reversing the order of timestamps
- Not casting strings to timestamps when needed
- Using AGE with only one parameter incorrectly
SELECT AGE('2024-06-01'::date, '2023-01-01'::date);Solution
Step 1: Calculate difference between dates
From 2023-01-01 to 2024-06-01 is 1 year and 5 months exactly.Step 2: Understand AGE output format
AGE returns interval as years, months, and days; days are zero here.Final Answer:
1 year 5 mons 0 days -> Option DQuick Check:
AGE returns 1 year 5 mons 0 days [OK]
- Counting months incorrectly as 6 instead of 5
- Confusing days count
- Assuming AGE returns total days only
SELECT AGE('2023-01-01');Solution
Step 1: Check AGE function argument requirements
AGE can accept one or two arguments; with one argument it returns difference from current date.Step 2: Analyze the query
The query uses one argument but does not cast it; however, AGE accepts one argument as timestamp or date.Step 3: Identify actual error
The query is valid; PostgreSQL implicitly handles the date string and computes age to current timestamp. No error.Final Answer:
No error; AGE accepts a single argument -> Option CQuick Check:
AGE needs two timestamps or one with current date [OK]
- Assuming AGE always needs two arguments
- Thinking date format is wrong
- Believing casting is mandatory
'1990-04-15' and today, but only in years ignoring months and days. Which query correctly uses AGE and extracts only the years part?Solution
Step 1: Use AGE with current date and birthdate
AGE(CURRENT_DATE, '1990-04-15') returns interval from birthdate to today.Step 2: Extract only the year part
Use EXTRACT(YEAR FROM interval) to get full years ignoring months and days.Final Answer:
SELECT EXTRACT(YEAR FROM AGE(CURRENT_DATE, '1990-04-15')); -> Option AQuick Check:
EXTRACT YEAR from AGE with later date first [OK]
- Reversing AGE argument order
- Using wrong function like DATE_PART with reversed arguments
- Trying to cast interval directly to year
Ready to prove it?
Pass the test and this pattern moves unknown
