Substring and overlay functions in PostgreSQL - Time & Space Complexity
We want to understand how the time to extract or replace parts of text grows as the text gets longer.
How does the work change when the input string size increases?
Analyze the time complexity of the following code snippet.
SELECT substring(text_column FROM 5 FOR 10) AS part,
overlay(text_column PLACING 'new' FROM 3 FOR 4) AS replaced
FROM example_table;
This code extracts a part of the text and replaces a part of the text in each row.
Look for repeated work done by the functions.
- Primary operation: Scanning the input string to find and extract or replace the substring.
- How many times: Once per row in the table, each operation processes a portion of the string.
As the input string gets longer, the functions need to look through more characters.
| 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 roughly in direct proportion to the string length.
Time Complexity: O(n)
This means the time to extract or replace grows linearly with the length of the input string.
[X] Wrong: "Substring and overlay run instantly no matter how long the string is."
[OK] Correct: The functions must check characters to find the right part, so longer strings take more time.
Understanding how string functions scale helps you write efficient queries and explain performance clearly.
"What if we used substring with a pattern match instead of fixed positions? How would the time complexity change?"
Practice
substring function do when given a string and a start position?Solution
Step 1: Understand substring function purpose
The substring function extracts a portion of a string starting at a specified position.Step 2: Analyze given parameters
When only a start position is given, substring returns from that position to the end of the string.Final Answer:
Extracts part of the string starting from the given position to the end -> Option BQuick Check:
substring extracts substring from position [OK]
- Confusing substring with overlay
- Thinking substring replaces text
- Assuming substring returns string length
overlay function in PostgreSQL?Solution
Step 1: Recall overlay syntax
The correct syntax is overlay(string placing new_substring from start for length).Step 2: Match options to syntax
overlay(string placing new_substring from start for length) matches the correct order: string placing new_substring from start for length.Final Answer:
overlay(string placing new_substring from start for length) -> Option AQuick Check:
Overlay syntax = string placing new_substring from start for length [OK]
- Mixing order of 'from' and 'placing'
- Omitting 'for length' keyword
- Swapping string and new_substring positions
SELECT overlay('abcdef' placing 'XYZ' from 2 for 3);Solution
Step 1: Understand overlay parameters
We replace 3 characters starting at position 2 in 'abcdef' with 'XYZ'. Positions start at 1.Step 2: Apply replacement
Characters at positions 2,3,4 ('b','c','d') are replaced by 'XYZ', resulting in 'aXYZef'.Final Answer:
'aXYZef' -> Option DQuick Check:
Overlay replaces substring from pos 2 length 3 with 'XYZ' [OK]
- Replacing wrong substring length
- Starting replacement at wrong position
- Appending instead of replacing
SELECT overlay('hello' placing 'y' for 2 from 3);Solution
Step 1: Check overlay syntax order
The correct syntax is overlay(string placing new_substring from start for length). The query uses 'placing' then 'for' then 'from', which is wrong order.Step 2: Identify error cause
The keywords 'for' and 'from' are swapped, causing syntax error.Final Answer:
Incorrect order of 'for' and 'from' keywords -> Option CQuick Check:
Overlay syntax order matters: from then for then placing [OK]
- Swapping 'for' and 'from' keywords
- Omitting quotes around strings
- Using overlay without required keywords
overlay and then extracts the substring starting at position 5 with length 4?Solution
Step 1: Replace 'gre' with 'XYZ' using overlay
'gre' starts at position 5 and has length 3. Correct overlay syntax is overlay(string placing new_substring from start for length). SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4); matches this.Step 2: Extract substring from position 5 length 4
Apply substring on the result starting at position 5 for length 4. SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4); applies substring correctly after overlay.Final Answer:
SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4); -> Option AQuick Check:
Overlay then substring with correct syntax = SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4); [OK]
- Mixing overlay keyword order
- Applying substring before overlay
- Wrong positions or lengths in functions
Ready to prove it?
Pass the test and this pattern moves unknown
