Bird
0
0

Identify the issue in this function:

medium📝 Debug Q7 of 15
PostgreSQL - Advanced PL/pgSQL

Identify the issue in this function:

CREATE FUNCTION get_colors() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT color FROM palette; RETURN NEXT 'extra'; END; $$ LANGUAGE plpgsql;

AThe function must return a single value, not SETOF.
BRETURN QUERY cannot be used with text data types.
CRETURN NEXT must be used before RETURN QUERY.
DThe function has unreachable code after RETURN QUERY.
Step-by-Step Solution
Solution:
  1. Step 1: Understand RETURN QUERY

    RETURN QUERY returns all rows and continues execution; it does not exit the function immediately.
  2. Step 2: Check code after RETURN QUERY

    RETURN NEXT 'extra' is reachable and will add an extra row after the query results.
  3. Step 3: Identify problem

    There is no unreachable code; the function returns all rows from SELECT plus one extra row.
  4. Final Answer:

    The function has no unreachable code; the RETURN NEXT after RETURN QUERY adds an extra row. -> Option D is incorrect
Quick Trick: RETURN QUERY does not exit function; code after it runs [OK]
Common Mistakes:
  • Placing RETURN NEXT after RETURN QUERY
  • Misunderstanding RETURN QUERY behavior
  • Thinking RETURN QUERY only returns one row

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes