String length and position functions in PostgreSQL - Time & Space Complexity
We want to understand how long it takes to find the length of a string or the position of a substring inside it.
How does the time needed change when the string gets longer?
Analyze the time complexity of the following code snippet.
SELECT LENGTH(text_column) AS str_length,
POSITION('abc' IN text_column) AS pos_abc
FROM example_table;
This code finds the length of each string and the position of 'abc' inside it for every row.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning characters in the string to find substring (POSITION). LENGTH is O(1) using stored length.
- How many times: Up to O(n) for POSITION; O(1) for LENGTH.
As the string gets longer, the time to search for substring grows roughly in direct proportion. (LENGTH is O(1))
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: The work grows steadily as the string length increases.
Time Complexity: O(n)
This means the time for POSITION grows linearly with the string size (LENGTH is O(1)).
[X] Wrong: "Finding the position is instant no matter how long the string is."
[OK] Correct: The database must scan characters to search, so longer strings take more time. (LENGTH is O(1))
Knowing how string functions scale helps you write efficient queries and understand performance when working with text data.
"What if we searched for a substring that appears near the end of the string? How would the time complexity change?"
Practice
LENGTH function do in PostgreSQL?Solution
Step 1: Understand LENGTH function purpose
The LENGTH function counts how many characters are in a given string.Step 2: Compare with other options
POSITION finds substring location, UPPER changes case, SPLIT divides string. Only LENGTH counts characters.Final Answer:
Returns the number of characters in a string -> Option DQuick Check:
LENGTH = character count [OK]
- Confusing LENGTH with POSITION
- Thinking LENGTH changes string case
- Assuming LENGTH splits strings
Solution
Step 1: Recall POSITION syntax
POSITION(substring IN string) finds where substring starts in string.Step 2: Check each option
POSITION('cat' IN 'concatenate') matches syntax. POSITION('concatenate' IN 'cat') reverses arguments. LENGTH('cat' IN 'concatenate') uses LENGTH wrongly. FIND('cat', 'concatenate') uses non-existent FIND function.Final Answer:
POSITION('cat' IN 'concatenate') -> Option CQuick Check:
POSITION(substring IN string) correct syntax [OK]
- Swapping substring and string arguments
- Using LENGTH instead of POSITION
- Using non-PostgreSQL functions like FIND
SELECT POSITION('day' IN 'holiday') AS pos, LENGTH('holiday') AS len;Solution
Step 1: Find POSITION of 'day' in 'holiday'
'holiday' positions: 1:'h', 2:'o', 3:'l', 4:'i', 5:'d', 6:'a', 7:'y'. 'day' matches positions 5('d')-6('a')-7('y'), so starts at 5 (1-based).Step 2: Calculate LENGTH of 'holiday'
'holiday' has 7 characters.Final Answer:
pos = 5, len = 7 -> Option BQuick Check:
POSITION('day' IN 'holiday') = 5, LENGTH('holiday') = 7 [OK]
- Counting POSITION starting at 0
- Miscounting substring start
- Confusing LENGTH with POSITION
SELECT POSITION('test', 'contest') AS pos;Solution
Step 1: Check POSITION function syntax
POSITION requires syntax POSITION(substring IN string), not POSITION(substring, string).Step 2: Identify error in query
The query uses comma instead of IN keyword, causing syntax error.Final Answer:
Uses comma instead of IN keyword in POSITION function -> Option AQuick Check:
POSITION(substring IN string) correct syntax [OK]
- Using comma instead of IN in POSITION
- Assuming alias AS is mandatory (it's optional)
- Thinking POSITION can't use string literals
Solution
Step 1: Identify correct function usage for substring position
POSITION(substring IN string) finds where substring starts. We want the space character ' '.Step 2: Evaluate each option
SELECT POSITION(' ' IN 'Find the position'); uses correct syntax and substring. SELECT LENGTH(' ' IN 'Find the position'); uses LENGTH wrongly. SELECT POSITION('space' IN 'Find the position'); searches for word 'space' which is not present. SELECT POSITION(' ' FROM 'Find the position'); uses FROM instead of IN, which is invalid syntax.Final Answer:
SELECT POSITION(' ' IN 'Find the position'); -> Option AQuick Check:
POSITION(' ' IN string) finds first space [OK]
- Using LENGTH instead of POSITION
- Searching for word 'space' instead of character
- Using FROM instead of IN in POSITION
Ready to prove it?
Pass the test and this pattern moves unknown
