CodeGym /Courses /SQL SELF /Analyzing Common Mistakes with Control Structures and Log...

Analyzing Common Mistakes with Control Structures and Logging

SQL SELF
Level 52 , Lesson 4
Available

Before we dive into the main topic, let's pause and look at the most common mistakes and slip-ups you might run into. SQL errors are every developer's pain, and, honestly, they usually pop up at the worst possible time.

1. Syntax Errors: "Forgot to close IF"

Syntax errors are the most basic problems, but they happen way more often than you'd think. For example, if you forget to close an IF block with END IF;, the compiler will instantly call you out.

Example of an error:

CREATE OR REPLACE FUNCTION check_number(num INTEGER)
RETURNS TEXT AS $$
BEGIN
    IF num > 0 THEN
        RETURN 'Positive';
    ELSE
        RETURN 'Negative';
-- somewhere END IF; got lost
END;
$$ LANGUAGE plpgsql;

When you try to run this code, you'll get an error: ERROR: syntax error at or near "END". Why? Because the IF block is left open.

How to avoid these mistakes?

Always use a clear code structure. When you open a block (like IF), write its closing right away. Here's the fixed example:

CREATE OR REPLACE FUNCTION check_number(num INTEGER)
RETURNS TEXT AS $$
BEGIN
    IF num > 0 THEN
        RETURN 'Positive';
    ELSE
        RETURN 'Negative';
    END IF; -- don't forget to close the block
END;
$$ LANGUAGE plpgsql;

2. Forgot to Handle All CASE Conditions: "What if nothing matches?"

When you use CASE, always add an ELSE branch to handle unexpected stuff. If you skip it, you might get an unwanted NULL back.

Example of an error:

CREATE OR REPLACE FUNCTION grade_result(grade CHAR)
RETURNS TEXT AS $$
BEGIN
    RETURN CASE grade
        WHEN 'A' THEN 'Excellent'
        WHEN 'B' THEN 'Good'
        WHEN 'C' THEN 'Average'
        -- But what if grade = 'D' or something else?
    END;
END;
$$ LANGUAGE plpgsql;

If you pass in D, the function will return NULL, which can mess up your code.

Fixed version:

CREATE OR REPLACE FUNCTION grade_result(grade CHAR)
RETURNS TEXT AS $$
BEGIN
    RETURN CASE grade
        WHEN 'A' THEN 'Excellent'
        WHEN 'B' THEN 'Good'
        WHEN 'C' THEN 'Average'
        ELSE 'Unknown grade' -- Catch all other cases
    END;
END;
$$ LANGUAGE plpgsql;

3. Infinite Loop Problems: "Why did my server freeze?"

When using a LOOP, it's super easy to forget to add an exit condition. That can lead to an infinite loop:

Example of an error:

CREATE OR REPLACE FUNCTION infinite_loop_demo()
RETURNS VOID AS $$
DECLARE
    i INTEGER := 1;
BEGIN
    LOOP
        i := i + 1;
        -- No exit condition!
    END LOOP;
END;
$$ LANGUAGE plpgsql;

This code will freeze your server, because the loop never ends.

How to fix:

Add an exit condition using EXIT:

CREATE OR REPLACE FUNCTION finite_loop_demo()
RETURNS VOID AS $$
DECLARE
    i INTEGER := 1;
BEGIN
    LOOP
        i := i + 1;
        IF i > 10 THEN
            EXIT; -- Exit condition
        END IF;
    END LOOP;
END;
$$ LANGUAGE plpgsql;

4. Skipping Iterations in Loops: "What about missing data?"

When you use CONTINUE to skip iterations, you might mess up if you don't think through all the possible behaviors. For example:

Example of an error:

CREATE OR REPLACE FUNCTION skip_even()
RETURNS VOID AS $$
DECLARE
    i INTEGER := 0;
BEGIN
    WHILE i < 10 LOOP
        i := i + 1;
        IF i % 2 = 0 THEN
            CONTINUE; -- Just skipping even numbers
        END IF;
        RAISE NOTICE 'Odd number: %', i;
    END LOOP;
END;
$$ LANGUAGE plpgsql;

What if all the numbers are even? The server will run, but you won't see any result.

How to fix:

Make sure you handle all your data right, and add logs for control:

CREATE OR REPLACE FUNCTION skip_even_logging()
RETURNS VOID AS $$
DECLARE
    i INTEGER := 0;
BEGIN
    WHILE i < 10 LOOP
        i := i + 1;
        IF i % 2 = 0 THEN
            RAISE NOTICE 'Skipping even number: %', i;
            CONTINUE;
        END IF;
        RAISE NOTICE 'Odd number: %', i;
    END LOOP;
END;
$$ LANGUAGE plpgsql;

Now you can see which numbers got skipped.

5. Bad Error Handling: "Where's my RAISE EXCEPTION?"

Error handling with RAISE EXCEPTION is a powerful tool, but it's easy to mess up if you use it wrong.

Example of an error:

CREATE OR REPLACE FUNCTION calculate_square(num INTEGER)
RETURNS INTEGER AS $$
BEGIN
    IF num < 0 THEN
        RAISE 'Negative number is not allowed!';
    END IF;
    RETURN num * num;
END;
$$ LANGUAGE plpgsql;

This code will throw an error, because the RAISE syntax is wrong (missing the message level).

Fixed version:

CREATE OR REPLACE FUNCTION calculate_square(num INTEGER)
RETURNS INTEGER AS $$
BEGIN
    IF num < 0 THEN
        RAISE EXCEPTION 'Negative number is not allowed!';
    END IF;
    RETURN num * num;
END;
$$ LANGUAGE plpgsql;

6. Logging Mistakes: "Why aren't my errors going into error_log?"

Bad inserts into the error_log table can happen because of mistakes in your INSERT INTO queries.

Example of an error:

CREATE OR REPLACE FUNCTION log_error(err_msg TEXT)
RETURNS VOID AS $$
BEGIN
    INSERT INTO error_log (error_message, error_time)
    VALUES (err_msg, CURRENT_TIMESTAMP); -- What if there's a column name error?
END;
$$ LANGUAGE plpgsql;

If the error_log table had its column name changed (like to error_msg), this will throw an error.

How to avoid:

Always check your table structure or use a strict schema to manage your data.

7. General Carelessness and "Human Factor"

Mistakes aren't just technical—they can be about not paying attention. Forgotten debug code, unused variables, or no code formatting can turn your function into a mess.

Example:

DECLARE
    i INTEGER; -- Why, if the variable isn't used?

Fix: delete unnecessary code so it stays clean and readable.

Now you're ready to avoid the most common mistakes in PL/pgSQL and write code that brings more joy than pain. Don't forget to test, log, and fix bugs on time—this will save you a ton of nerves and client calls!

2
Task
SQL SELF, level 52, lesson 4
Locked
Handling All Conditions in `CASE`
Handling All Conditions in `CASE`
1
Survey/quiz
Error Handling, level 52, lesson 4
Unavailable
Error Handling
Error Handling
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION