The format function helps you build text safely by inserting values into a string without mistakes or errors.
Format function for safe formatting in PostgreSQL
format(format_string, value1, value2, ...)
The format_string uses placeholders like %s for strings, %I for identifiers (like table or column names), and %L for literals (values).
Values are safely inserted in place of placeholders, avoiding errors or injection risks.
SELECT format('Hello, %s!', 'Alice');
SELECT format('SELECT * FROM %I WHERE id = %L', 'users', 42);
SELECT format('Price: %s, Quantity: %s', 9.99, 5);
This query creates a message showing a user's name and points safely formatted.
SELECT format('User %s has %s points.', 'Bob', 150);
Use %I for identifiers to avoid SQL injection when inserting table or column names.
Use %L for literal values to safely quote strings or numbers.
The format function returns text, so you can use it anywhere text is expected.
The format function helps build text safely with placeholders.
Use %s for strings, %I for identifiers, and %L for literals.
It prevents errors and injection by inserting values correctly.
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
