WITH Queries (Common Table Expressions)

The WITH clause of the SELECT command provides a way to write subqueries for use in a larger SELECT query.

The subqueries, which are often referred to as Common Table Expressions or CTEs, can be thought of as defining temporary tables that exist just for the query. One use of this feature is to break down complicated queries into simpler parts. This example query displays per-product sales totals in only the top sales regions:

  1. WITH regional_sales AS (
  2. SELECT region, SUM(amount) AS total_sales
  3. FROM orders
  4. GROUP BY region
  5. ), top_regions AS (
  6. SELECT region
  7. FROM regional_sales
  8. WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)
  9. )
  10. SELECT region,
  11. product,
  12. SUM(quantity) AS product_units,
  13. SUM(amount) AS product_sales
  14. FROM orders
  15. WHERE region IN (SELECT region FROM top_regions)
  16. GROUP BY region, product;

The query could have been written without the WITH clause, but would have required two levels of nested sub-SELECTs. It is easier to follow with the WITH clause.

The RECURSIVE keyword can be enabled by setting the server configuration parameter gp_recursive_cte_prototype to on.

Note: The RECURSIVE keyword is a Beta feature.

The optional RECURSIVE keyword changes WITH accomplishes things not otherwise possible in standard SQL. Using RECURSIVE, a query in the WITH clause can refer to its own output. This is a simple example that computes the sum the integers from 1 through 100:

  1. WITH RECURSIVE t(n) AS (
  2. VALUES (1)
  3. UNION ALL
  4. SELECT n+1 FROM t WHERE n < 100
  5. )
  6. SELECT sum(n) FROM t;

The required form of a recursive WITH clause is a non-recursive term, followed by a UNION (or UNION ALL), and then a recursive term, where only the recursive term can contain a reference to the query output.

  1. <non_recursive_term> UNION [ ALL ] <recursive_term>

A recursive WITH query is executed as follows:

  1. Evaluate the non-recursive term. For UNION (but not UNION ALL), discard duplicate rows. Include all remaining rows in the result of the recursive query, and also place them in a temporary working table.
  2. As long as the working table is not empty, repeat these steps:
    1. Evaluate the recursive term, substituting the current contents of the working table for the recursive self-reference. For UNION (but not UNION ALL), discard duplicate rows and rows that duplicate any previous result row. Include all remaining rows in the result of the recursive query, and also place them in a temporary intermediate table.
    2. Replace the contents of the working table with the contents of the intermediate table, then empty the intermediate table.

Note: Strictly speaking, the process is iteration not recursion, but RECURSIVE is the terminology chosen by the SQL standards committee.

Recursive WITH queries are typically used to deal with hierarchical or tree-structured data. An example is this query to find all the direct and indirect sub-parts of a product, given only a table that shows immediate inclusions:

  1. WITH RECURSIVE included_parts(sub_part, part, quantity) AS (
  2. SELECT sub_part, part, quantity FROM parts WHERE part = 'our_product'
  3. UNION ALL
  4. SELECT p.sub_part, p.part, p.quantity
  5. FROM included_parts pr, parts p
  6. WHERE p.part = pr.sub_part
  7. )
  8. SELECT sub_part, SUM(quantity) as total_quantity
  9. FROM included_parts
  10. GROUP BY sub_part

When working with recursive WITH queries, you must ensure that the recursive part of the query eventually returns no tuples, or else the query loops indefinitely. In the example that computes the sum the integers, the working table contains a single row in each step, and it takes on the values from 1 through 100 in successive steps. In the 100th step, there is no output because of the WHERE clause, and the query terminates.

For some queries, using UNION instead of UNION ALL can ensure that the recursive part of the query eventually returns no tuples by discarding rows that duplicate previous output rows. However, often a cycle does not involve output rows that are complete duplicates: it might sufficient to check just one or a few fields to see if the same point has been reached before. The standard method for handling such situations is to compute an array of the visited values. For example, consider the following query that searches a table graph using a link field:

  1. WITH RECURSIVE search_graph(id, link, data, depth) AS (
  2. SELECT g.id, g.link, g.data, 1
  3. FROM graph g
  4. UNION ALL
  5. SELECT g.id, g.link, g.data, sg.depth + 1
  6. FROM graph g, search_graph sg
  7. WHERE g.id = sg.link
  8. )
  9. SELECT * FROM search_graph;

This query loops if the link relationships contain cycles. Because the query requires a depth output, changing UNION ALL to UNION does not eliminate the looping. Instead the query needs to recognize whether it has reached the same row again while following a particular path of links. This modified query adds two columns path and cycle to the loop-prone query:

  1. WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (
  2. SELECT g.id, g.link, g.data, 1,
  3. ARRAY[g.id],
  4. false
  5. FROM graph g
  6. UNION ALL
  7. SELECT g.id, g.link, g.data, sg.depth + 1,
  8. path || g.id,
  9. g.id = ANY(path)
  10. FROM graph g, search_graph sg
  11. WHERE g.id = sg.link AND NOT cycle
  12. )
  13. SELECT * FROM search_graph;

Aside from detecting cycles, the array value of path is useful in its own right since it represents the path taken to reach any particular row.

In the general case where more than one field needs to be checked to recognize a cycle, an array of rows can be used. For example, if we needed to compare fields f1 and f2:

  1. WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (
  2. SELECT g.id, g.link, g.data, 1,
  3. ARRAY[ROW(g.f1, g.f2)],
  4. false
  5. FROM graph g
  6. UNION ALL
  7. SELECT g.id, g.link, g.data, sg.depth + 1,
  8. path || ROW(g.f1, g.f2),
  9. ROW(g.f1, g.f2) = ANY(path)
  10. FROM graph g, search_graph sg
  11. WHERE g.id = sg.link AND NOT cycle
  12. )
  13. SELECT * FROM search_graph;

Tip: Omit the ROW() syntax in the case where only one field needs to be checked to recognize a cycle. This uses a simple array rather than a composite-type array to be used, gaining efficiency.

Tip: The recursive query evaluation algorithm produces its output in breadth-first search order. You can display the results in depth-first search order by making the outer query ORDER BY a path column constructed in this way.

A helpful technique for testing a query when you are not certain if it might loop indefinitely is to place a LIMIT in the parent query. For example, this query would loop forever without the LIMIT clause:

  1. WITH RECURSIVE t(n) AS (
  2. SELECT 1
  3. UNION ALL
  4. SELECT n+1 FROM t
  5. )
  6. SELECT n FROM t LIMIT 100;

The technique works because the recursive WITH implementation evaluates only as many rows of a WITH query as are actually fetched by the parent query. Using this technique in production is not recommended, because other systems might work differently. Also, the technique might not work if the outer query sorts the recursive WITH results or join the results to another table.

Parent topic: Querying Data