CURRENT_DATE, CURRENT_TIMESTAMP, NOW() in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to get the current date or time changes as the database grows.
Does asking for the current date or time take longer if the database has more data?
Analyze the time complexity of these PostgreSQL functions.
SELECT CURRENT_DATE;
SELECT CURRENT_TIMESTAMP;
SELECT NOW();
These commands return the current date or timestamp from the database server.
These commands do not repeat any operations like loops or scans.
- Primary operation: Fetching the system clock value once.
- How many times: Exactly once per query execution.
Getting the current date or time does not depend on how much data is in the database.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays the same no matter the input size.
Time Complexity: O(1)
This means the time to get the current date or time stays constant, no matter how big the database is.
[X] Wrong: "Getting the current date or time takes longer if the database has more rows."
[OK] Correct: These functions just read the system clock once and do not scan or process any data in the database.
Knowing that some queries run in constant time helps you understand which operations are fast and predictable, a useful skill in database work.
"What if we used CURRENT_TIMESTAMP inside a query that processes thousands of rows? How would the time complexity change?"
Practice
Solution
Step 1: Understand the function outputs
CURRENT_DATEreturns only the date part (year-month-day) without time.Step 2: Compare with other functions
CURRENT_TIMESTAMPandNOW()return both date and time, whileDATE_PART()extracts parts but is not a current date function.Final Answer:
CURRENT_DATE -> Option AQuick Check:
Only date, no time = CURRENT_DATE [OK]
- Confusing CURRENT_TIMESTAMP with CURRENT_DATE
- Using NOW() when only date is needed
- Thinking DATE_PART() returns current date
Solution
Step 1: Check function syntax
CURRENT_TIMESTAMPis a special function and does not require parentheses.Step 2: Validate other options
SELECT CURRENT_DATE();uses parentheses incorrectly with CURRENT_DATE.SELECT CURRENT_TIME();returns only the time part (time type), not full timestamp.SELECT NOW;misses required parentheses for NOW().SELECT CURRENT_TIMESTAMP;is the correct and preferred standard syntax.Final Answer:
SELECT CURRENT_TIMESTAMP; -> Option BQuick Check:
Standard timestamp function = CURRENT_TIMESTAMP [OK]
- Adding parentheses to CURRENT_DATE
- Omitting parentheses for NOW()
- Confusing function syntax
SELECT CURRENT_DATE, CURRENT_TIMESTAMP, NOW();Solution
Step 1: Identify each function's return type
CURRENT_DATEreturns a date type (no time).CURRENT_TIMESTAMPandNOW()return timestamp with time zone.Step 2: Confirm output columns
The query returns three columns: one date and two timestamps with time zone.Final Answer:
Three columns: date, timestamp with time zone, timestamp with time zone -> Option DQuick Check:
Types: date, timestamptz, timestamptz = Three columns: date, timestamp with time zone, timestamp with time zone [OK]
- Assuming all return date only
- Thinking NOW() returns date
- Expecting syntax error
SELECT CURRENT_DATE();What is the problem?
Solution
Step 1: Check function usage
CURRENT_DATEis a special keyword, not a function, so it does not accept parentheses.Step 2: Understand error cause
Using parentheses causes syntax error because PostgreSQL expects no parentheses for CURRENT_DATE.Final Answer:
CURRENT_DATE is not a function and should not have parentheses -> Option CQuick Check:
No parentheses with CURRENT_DATE [OK]
- Adding () to CURRENT_DATE
- Thinking CURRENT_DATE needs parameters
- Confusing with NOW() syntax
created_date DATE and created_time TIMESTAMP. Which INSERT statement is correct?Solution
Step 1: Match data types with values
created_dateis DATE type, soCURRENT_DATEfits.created_timeis TIMESTAMP, soNOW()fits.Step 2: Check syntax correctness
INSERT INTO table_name (created_date, created_time) VALUES (CURRENT_DATE, NOW()); uses correct functions and order. INSERT INTO table_name (created_date, created_time) VALUES (NOW(), CURRENT_DATE); swaps types incorrectly. INSERT INTO table_name (created_date, created_time) VALUES (CURRENT_TIMESTAMP, CURRENT_DATE); uses timestamp for date column (wrong type). INSERT INTO table_name (created_date, created_time) VALUES (CURRENT_DATE(), CURRENT_TIMESTAMP()); uses parentheses incorrectly with CURRENT_DATE and CURRENT_TIMESTAMP.Final Answer:
INSERT INTO table_name (created_date, created_time) VALUES (CURRENT_DATE, NOW()); -> Option AQuick Check:
Date = CURRENT_DATE, Timestamp = NOW() [OK]
- Swapping date and timestamp values
- Using parentheses with CURRENT_DATE
- Using CURRENT_TIMESTAMP for DATE column
Ready to prove it?
Pass the test and this pattern moves unknown
