Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is PL/pgSQL in PostgreSQL?
PL/pgSQL is a procedural language in PostgreSQL that allows writing functions and triggers with control structures like loops and conditionals, making database operations more powerful and flexible.
Click to reveal answer
intermediate
Why use advanced PL/pgSQL features instead of simple SQL queries?
Advanced PL/pgSQL lets you handle complex logic inside the database, reduce data transfer between app and database, improve performance, and create reusable code blocks.
Click to reveal answer
beginner
Name one benefit of using control structures in PL/pgSQL.
Control structures like IF statements and loops let you make decisions and repeat actions inside the database, which is not possible with plain SQL.
Click to reveal answer
intermediate
How does advanced PL/pgSQL improve application performance?
By running complex logic inside the database, it reduces the need to send many queries or large data sets back and forth, speeding up the overall process.
Click to reveal answer
intermediate
What role do triggers play in advanced PL/pgSQL?
Triggers automatically run PL/pgSQL code in response to events like inserts or updates, helping automate tasks and maintain data integrity without extra application code.
Click to reveal answer
What is a key advantage of using PL/pgSQL over plain SQL?
AIt supports control flow like loops and conditionals
BIt stores data in tables
CIt replaces the need for indexes
DIt automatically creates backups
✗ Incorrect
PL/pgSQL allows control flow structures such as loops and conditionals, which plain SQL does not support.
How does advanced PL/pgSQL help improve database performance?
ABy running complex logic inside the database to reduce data transfer
BBy increasing the size of the database
CBy disabling indexes
DBy storing data in external files
✗ Incorrect
Running logic inside the database reduces the need to send large amounts of data back and forth, improving performance.
What is a trigger in PostgreSQL?
AA user permission setting
BA type of index
CA function that runs automatically on certain database events
DA backup tool
✗ Incorrect
Triggers are functions that automatically execute in response to events like inserts or updates.
Which of the following is NOT a feature of PL/pgSQL?
ALoops and conditionals
BAutomatic data replication
CException handling
DVariable declaration
✗ Incorrect
PL/pgSQL does not handle automatic data replication; it focuses on procedural logic inside the database.
Why is code reuse important in advanced PL/pgSQL?
AIt disables security features
BIt increases database size
CIt slows down queries
DIt saves time and reduces errors by using functions multiple times
✗ Incorrect
Reusing code with functions saves development time and helps avoid mistakes.
Explain why advanced PL/pgSQL matters for database applications.
Think about how running code inside the database can help your app.
You got /5 concepts.
Describe how triggers in PL/pgSQL can automate tasks.
Triggers respond to changes like inserts or updates.
You got /4 concepts.
Practice
(1/5)
1. What is one main benefit of using advanced PL/pgSQL in PostgreSQL?
easy
A. It replaces the need for any SQL queries.
B. It disables database transactions.
C. It automatically creates user interfaces.
D. It allows writing complex logic inside the database for better performance.
Solution
Step 1: Understand PL/pgSQL purpose
PL/pgSQL is designed to write procedural code inside PostgreSQL to handle complex logic.
Step 2: Identify the benefit
Writing logic inside the database improves performance by reducing data transfer and centralizing processing.
Final Answer:
It allows writing complex logic inside the database for better performance. -> Option D
Quick Check:
Advanced PL/pgSQL improves performance [OK]
Hint: Think about why logic inside DB helps speed [OK]
Common Mistakes:
Thinking PL/pgSQL replaces all SQL queries
Confusing PL/pgSQL with UI tools
Assuming it disables transactions
2. Which of the following is the correct way to declare a variable in PL/pgSQL?
Variables are declared inside a DECLARE block with type and optional initialization.
Step 2: Check each option
DECLARE myvar INTEGER := 10; correctly uses DECLARE, variable name, type, and initialization. Others have syntax errors.
Final Answer:
DECLARE myvar INTEGER := 10; -> Option A
Quick Check:
Variable declaration needs DECLARE and type [OK]
Hint: Remember DECLARE block is mandatory for variables [OK]
Common Mistakes:
Omitting DECLARE keyword
Placing type after initialization
Using VAR instead of DECLARE
3. What will be the output of this PL/pgSQL function?
CREATE OR REPLACE FUNCTION add_numbers(a INTEGER, b INTEGER) RETURNS INTEGER AS $$
BEGIN
RETURN a + b;
END;
$$ LANGUAGE plpgsql;
SELECT add_numbers(3, 5);
medium
A. 8
B. 35
C. Syntax error
D. NULL
Solution
Step 1: Understand function logic
The function takes two integers and returns their sum using RETURN a + b.
Step 2: Evaluate the SELECT call
Calling add_numbers(3, 5) returns 3 + 5 = 8.
Final Answer:
8 -> Option A
Quick Check:
3 + 5 = 8 [OK]
Hint: Add the two input numbers as the function returns sum [OK]
Common Mistakes:
Concatenating numbers as strings
Expecting syntax error due to missing semicolon
Assuming NULL return without explicit return
4. Identify the error in this PL/pgSQL block:
DO $$
DECLARE
counter INTEGER := 0
BEGIN
counter := counter + 1;
RAISE NOTICE 'Counter: %', counter;
END;
$$ LANGUAGE plpgsql;
medium
A. Variable counter cannot be initialized
B. RAISE NOTICE syntax is incorrect
C. Missing semicolon after variable declaration
D. LANGUAGE plpgsql is not allowed in DO blocks
Solution
Step 1: Check variable declaration syntax
In PL/pgSQL, each statement must end with a semicolon. The declaration line lacks a semicolon.
Step 2: Verify other parts
RAISE NOTICE syntax is correct, variable initialization is allowed, and LANGUAGE plpgsql is required.
Final Answer:
Missing semicolon after variable declaration -> Option C
Quick Check:
Statements must end with semicolon [OK]
Hint: Check semicolons after DECLARE lines [OK]
Common Mistakes:
Ignoring missing semicolon errors
Misreading RAISE NOTICE syntax
Thinking variable initialization is disallowed
5. You want to create a PL/pgSQL function that returns the factorial of a number using recursion. Which of these function definitions correctly implements this?
hard
A. CREATE FUNCTION factorial(n INTEGER) RETURNS INTEGER AS $$ BEGIN RETURN n * factorial(n - 1); END; $$ LANGUAGE plpgsql;
B. CREATE FUNCTION factorial(n INTEGER) RETURNS INTEGER AS $$ BEGIN IF n <= 1 THEN RETURN 1; ELSE RETURN n * factorial(n - 1); END IF; END; $$ LANGUAGE plpgsql;
C. CREATE FUNCTION factorial(n INTEGER) RETURNS INTEGER AS $$ BEGIN IF n = 0 THEN RETURN 0; ELSE RETURN n * factorial(n - 1); END IF; END; $$ LANGUAGE plpgsql;
D. CREATE FUNCTION factorial(n INTEGER) RETURNS INTEGER AS $$ BEGIN WHILE n > 1 LOOP RETURN n * factorial(n - 1); END LOOP; END; $$ LANGUAGE plpgsql;
Solution
Step 1: Understand factorial base case
Factorial of 0 or 1 is 1, so base case must return 1 when n <= 1.
Step 2: Check recursive call correctness
CREATE FUNCTION factorial(n INTEGER) RETURNS INTEGER AS $$ BEGIN IF n <= 1 THEN RETURN 1; ELSE RETURN n * factorial(n - 1); END IF; END; $$ LANGUAGE plpgsql; correctly returns 1 for base case and multiplies n by factorial(n-1) otherwise.
Step 3: Identify errors in other options
CREATE FUNCTION factorial(n INTEGER) RETURNS INTEGER AS $$ BEGIN RETURN n * factorial(n - 1); END; $$ LANGUAGE plpgsql; lacks base case, causing infinite recursion. CREATE FUNCTION factorial(n INTEGER) RETURNS INTEGER AS $$ BEGIN IF n = 0 THEN RETURN 0; ELSE RETURN n * factorial(n - 1); END IF; END; $$ LANGUAGE plpgsql; returns 0 for n=0, which is incorrect. CREATE FUNCTION factorial(n INTEGER) RETURNS INTEGER AS $$ BEGIN WHILE n > 1 LOOP RETURN n * factorial(n - 1); END LOOP; END; $$ LANGUAGE plpgsql; misuses WHILE loop and RETURN inside loop.
Final Answer:
Correctly implements recursive factorial with base case and recursion. -> Option B