WITH Queries (Common Table Expressions)

    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:

    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;

    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 (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. SELECT p.sub_part, p.part, p.quantity
    4. FROM included_parts pr, parts p
    5. WHERE p.part = pr.sub_part
    6. )
    7. SELECT sub_part, SUM(quantity) as total_quantity
    8. FROM included_parts
    9. 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, path, cycle) AS (
    2. SELECT g.id, g.link, g.data, 1,
    3. false
    4. FROM graph g
    5. SELECT g.id, g.link, g.data, sg.depth + 1,
    6. path || g.id,
    7. g.id = ANY(path)
    8. FROM graph g, search_graph sg
    9. WHERE g.id = sg.link AND NOT cycle
    10. )
    11. 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:

    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. )

    Parent topic: Querying Data