Of course. Thank you for providing the exact error message and database details. This makes the problem crystal clear.
The error [postgresql]: Specified relation(-s) does not exist combined with (LINE: 26) points to a specific requirement in PostgreSQL (and standard SQL) for recursive queries.
The Direct Fix
The issue is that your WITH clause defines a recursive Common Table Expression (CTE), but you haven't told PostgreSQL that it's recursive.
You are missing the RECURSIVE keyword.
In PostgreSQL, when a CTE refers to itself (as recursive_combinations does in the UNION ALL part), you must explicitly declare the WITH clause as WITH RECURSIVE.
The Explanation
Your query has this structure:
WITH
ordered AS (...),
recursive_combinations AS (
SELECT ... -- Base case
UNION ALL
SELECT ...
FROM recursive_combinations -- <--- Self-reference
...
)
SELECT ... FROM recursive_combinations;
When the PostgreSQL parser reads FROM recursive_combinations inside the definition of recursive_combinations, it doesn't know this is an intentional recursive call. It thinks you are trying to reference a regular table or a previously defined CTE named recursive_combinations, which doesn't exist yet. It throws the "relation does not exist" error.
By adding RECURSIVE, you are telling the parser: "Be prepared, one or more of the CTEs in this block will refer to themselves. This is intentional."
A Deeper Logic Flaw and a Better Solution
There is also a subtle logic error in your original query's final SELECT. The statement SELECT DISTINCT combination FROM recursive_combinations will return all intermediate combinations, not just the final, complete combination for each transaction.
For your example transaction 231574 with products 111, 222, 444, the recursive_combinations CTE would contain two rows:
'111, 222' (from the first recursion)
'111, 222, 444' (from the second recursion)
Your query would incorrectly output both of these as distinct combinations. You only want the final, complete one.
The fix is to select only the rows from the recursive CTE that represent the last product in each transaction. We can identify these because their row number (rn) will be equal to the total product count (cnt) for that transaction.
Final Corrected and Improved Query
This query fixes both the syntax error (RECURSIVE keyword) and the logic error (selecting only the final combination).
WITH RECURSIVE -- FIX 1: Added the RECURSIVE keyword
ordered AS (
-- For each transaction, order the products and get a total count
SELECT
transaction_id,
product_id,
ROW_NUMBER() OVER (PARTITION BY transaction_id ORDER BY product_id) AS rn,
COUNT(*) OVER (PARTITION BY transaction_id) AS cnt
FROM transactions
WHERE product_id IS NOT NULL
),
recursive_combinations AS (
-- Base case: Start with the first product of every multi-item transaction
SELECT
transaction_id,
CAST(product_id AS VARCHAR) AS combination, -- Use VARCHAR without a huge limit unless needed
rn,
cnt
FROM ordered
WHERE rn = 1 AND cnt > 1 -- Only start with transactions that have more than 1 item
UNION ALL
-- Recursive step: Append the next product to the combination string
SELECT
o.transaction_id,
CONCAT(rc.combination, ', ', CAST(o.product_id AS VARCHAR)) AS combination,
o.rn,
o.cnt
FROM recursive_combinations rc
JOIN ordered o
ON rc.transaction_id = o.transaction_id AND o.rn = rc.rn + 1
)
-- Select only the final, complete combinations
SELECT DISTINCT combination
FROM recursive_combinations
WHERE rn = cnt -- FIX 2: Only select rows where the last product has been added
ORDER BY combination;
Summary of Changes:
WITH RECURSIVE: Corrects the "relation does not exist" syntax error.
WHERE rn = cnt: Corrects the logic to ensure you only get the single, complete combination string for each transaction, not all the partial ones built along the way.
- Passing
cnt through: I've added cnt to the recursive CTE so it's available for the final WHERE clause.