Challenge - 5 Problems
Postgres Functions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediateWhat is the output of this Postgres function call?
Consider the following Postgres function that returns the sum of two integers. What will be the result of calling
SELECT add_numbers(3, 5);?Supabase
CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
Think about what the function does with the two input numbers.
✗ Incorrect
The function adds the two integer inputs and returns their sum. So 3 + 5 equals 8.
🧠 Conceptual
intermediateWhat error occurs when calling a Postgres function with wrong argument types?
Given a Postgres function defined as
CREATE FUNCTION greet(name text) RETURNS text, what error will occur if you call SELECT greet(123);?Supabase
CREATE OR REPLACE FUNCTION greet(name text) RETURNS text AS $$
BEGIN
RETURN 'Hello, ' || name;
END;
$$ LANGUAGE plpgsql;Attempts:
2 left
💡 Hint
Check the argument type expected versus the type provided.
✗ Incorrect
Postgres functions are strict about argument types. Passing an integer where text is expected causes a 'function does not exist' error.
❓ Architecture
advancedWhich option correctly creates a Postgres function that returns a table?
You want to create a Postgres function that returns a table with columns
id integer and name text. Which option correctly defines this function?Attempts:
2 left
💡 Hint
Look for the correct use of RETURN QUERY and RETURNS TABLE syntax.
✗ Incorrect
Option A correctly uses RETURNS TABLE and RETURN QUERY to return a set of rows with specified columns.
❓ security
advancedWhich option correctly restricts a Postgres function to run with the caller's permissions?
You want a Postgres function to execute with the permissions of the user calling it, not the function owner. Which option achieves this?
Attempts:
2 left
💡 Hint
Check the SECURITY clause that controls execution rights.
✗ Incorrect
SECURITY INVOKER makes the function run with the caller's rights, while SECURITY DEFINER runs with the function owner's rights.
✅ Best Practice
expertWhat is the best practice to handle exceptions inside a Postgres function?
You want to catch errors inside a Postgres function and return a custom message instead of failing. Which code snippet correctly implements this?
Attempts:
2 left
💡 Hint
Postgres uses EXCEPTION blocks for error handling in PL/pgSQL.
✗ Incorrect
Option C uses the correct EXCEPTION WHEN OTHERS THEN syntax to catch all exceptions and handle them.
