Bird
0
0

Consider this recursive CTE:

medium📝 Debug Q14 of 15
PostgreSQL - Common Table Expressions
Consider this recursive CTE:
WITH RECURSIVE path(node) AS (SELECT 1 UNION ALL SELECT node + 1 FROM path WHERE node < 3) SELECT * FROM path;

What is wrong if this query runs forever without stopping?
ANot selecting from the CTE in the final SELECT
BMissing a proper termination condition in the recursive part
CUsing UNION instead of UNION ALL
DIncorrect anchor query syntax
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the recursive condition

    The recursive part adds 1 to node while node < 3, but due to UNION ALL accumulating duplicates of rows satisfying the condition, recursion continues indefinitely.
  2. Step 2: Identify why it loops infinitely

    If the termination condition is incorrect or not reached, recursion never stops. Here, the condition is present but ineffective against duplicates, causing infinite loop.
  3. Final Answer:

    Missing a proper termination condition in the recursive part -> Option B
  4. Quick Check:

    Infinite recursion = no stop condition [OK]
Quick Trick: Check recursive WHERE clause stops recursion [OK]
Common Mistakes:
  • Confusing UNION and UNION ALL effects
  • Ignoring anchor query correctness
  • Forgetting to select from CTE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes