Format function for safe formatting in PostgreSQL - Time & Space Complexity
We want to understand how the time to run a query using the format function changes as the input grows.
Specifically, how does formatting many values safely affect execution time?
Analyze the time complexity of the following code snippet.
DO $$
DECLARE
n integer := 1000;
i integer;
formatted_text text;
BEGIN
FOR i IN 1..n LOOP
formatted_text := format('Value: %s', i);
END LOOP;
END $$;
This code formats a string with a number inside a loop that runs n times.
- Primary operation: The format function call inside the loop.
- How many times: It runs once for each number from 1 to n, so n times.
Each time we add one more number to format, the loop runs one more time, doing one more format operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 format calls |
| 100 | 100 format calls |
| 1000 | 1000 format calls |
Pattern observation: The number of operations grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as the input size increases.
[X] Wrong: "The format function runs in constant time no matter how many times it is called."
[OK] Correct: Each call to format does some work, so calling it more times means more total work.
Understanding how repeated formatting affects performance helps you write efficient queries and scripts that handle many inputs safely.
"What if we replaced the loop with a single format call that formats all values at once? How would the time complexity change?"
Practice
format() function in PostgreSQL?Solution
Step 1: Understand the function's role
Theformat()function is designed to build text strings safely by inserting values into placeholders.Step 2: Compare with other options
Options A, B, and C describe other SQL operations, not string formatting.Final Answer:
To safely build text strings with placeholders for values -> Option AQuick Check:
format() = safe string building [OK]
- Thinking format() executes queries
- Confusing format() with table operations
- Ignoring the safety aspect of format()
format() to safely insert a table name (identifier)?Solution
Step 1: Identify placeholder types
Informat(),%Iis used for identifiers like table or column names.Step 2: Eliminate other placeholders
%sis for strings,%Lfor literals, and%dis not a valid placeholder in PostgreSQL's format.Final Answer:
%I -> Option CQuick Check:
Identifier placeholder = %I [OK]
- Using %s for identifiers
- Confusing %L with %I
- Using invalid placeholders like %d
SELECT format('SELECT * FROM %I WHERE name = %L', 'users', 'Alice');Solution
Step 1: Understand placeholders used
%Iinserts the identifier without quotes, so 'users' becomes users.%Linserts the literal with quotes, so 'Alice' becomes 'Alice'.Step 2: Construct the output string
The output is: SELECT * FROM users WHERE name = 'Alice'Final Answer:
SELECT * FROM users WHERE name = 'Alice' -> Option BQuick Check:
Identifier no quotes, literal with quotes = SELECT * FROM users WHERE name = 'Alice' [OK]
- Adding quotes around identifiers
- Missing quotes around literals
- Confusing %s with %I or %L
format():SELECT format('INSERT INTO %I VALUES (%s)', 'products', 100);Solution
Step 1: Check placeholder for numeric value
%sinserts the value as a string without quotes, which can cause errors or injection risks.Step 2: Use %L for safe literal insertion
Using%Lsafely quotes and escapes the numeric literal, preventing errors.Final Answer:
Should use %L for the numeric value to be safe -> Option DQuick Check:
Numeric literals need %L for safety [OK]
- Using %s for literals without quotes
- Assuming %s works for all types
- Ignoring injection risks with %s
format() call is correct?1) format('UPDATE %I SET email = %s WHERE id = %L', 'users', 'new@example.com', 42)
2) format('UPDATE %I SET email = %L WHERE id = %L', 'users', 'new@example.com', 42)
3) format('UPDATE %s SET email = %L WHERE id = %s', 'users', 'new@example.com', 42)
4) format('UPDATE %I SET email = %I WHERE id = %L', 'users', 'new@example.com', 42)Solution
Step 1: Check placeholders for each part
Table name is an identifier, so %I is correct. Email is a string literal, so %L is needed. ID is a literal number, so %L is safe.Step 2: Evaluate each option
Option 2 uses %I for table, %L for email and id, which is correct. Option 1 uses %s for email (unsafe). Option 3 uses %s for table and id (wrong). Option 4 uses %I for email (wrong).Final Answer:
Option 2 -> Option AQuick Check:
Identifiers %I, literals %L = Option 2 [OK]
- Using %s for literals
- Using %I for string literals
- Mixing placeholders incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
