7.8. WITH
Queries (Common Table Expressions)#
The basic value of which displays per-product sales totals in only the top sales regions. The The optional The general form of a recursive Recursive Query Evaluation Evaluate the non-recursive term. For So long as the working table is not empty, repeat these steps: Evaluate the recursive term, substituting the current contents of the working table for the recursive self-reference. For Replace the contents of the working table with the contents of the intermediate table, then empty the intermediate table. While In the example above, the working table has just 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 Recursive queries are typically used to deal with hierarchical or tree-structured data. A useful example is this query to find all the direct and indirect sub-parts of a product, given only a table that shows immediate inclusions: When computing a tree traversal using a recursive query, you might want to order the results in either depth-first or breadth-first order. This can be done by computing an ordering column alongside the other data columns and using that to sort the results at the end. Note that this does not actually control in which order the query evaluation visits the rows; that is as always in SQL implementation-dependent. This approach merely provides a convenient way to order the results afterwards. To create a depth-first order, we compute for each result row an array of rows that we have visited so far. For example, consider the following query that searches a table To add depth-first ordering information, you can write this: In the general case where more than one field needs to be used to identify a row, use an array of rows. For example, if we needed to track fields Omit the To create a breadth-first order, you can add a column that tracks the depth of the search, for example: To get a stable sort, add data columns as secondary sorting columns. The recursive query evaluation algorithm produces its output in breadth-first search order. However, this is an implementation detail and it is perhaps unsound to rely on it. The order of the rows within each level is certainly undefined, so some explicit ordering might be desired in any case. There is built-in syntax to compute a depth- or breadth-first sort column. For example: This syntax is internally expanded to something similar to the above hand-written forms. The When working with recursive queries it is important to be sure that the recursive part of the query will eventually return no tuples, or else the query will loop indefinitely. Sometimes, using This query will loop if the Aside from preventing cycles, the array value is often useful in its own right as representing 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, use an array of rows. For example, if we needed to compare fields Omit the There is built-in syntax to simplify cycle detection. The above query can also be written like this: and it will be internally rewritten to the above form. The The cycle path column is computed in the same way as the depth-first ordering column show in the previous section. A query can have both a A helpful trick for testing queries when you are not certain if they might loop is to place a This works becausePostgreSQL's implementation evaluates only as many rows of a A useful property of However, if a A simple example of these rules is This In particular, if there's an index on the so that the parent query's restrictions can be applied directly to scans of An example where Here, materialization of the The examples above only show You can use data-modifying statements ( This query effectively moves rows from A fine point of the above example is that the Data-modifying statements in This example would remove all rows from tables Recursive self-references in data-modifying statements are not allowed. In some cases it is possible to work around this limitation by referring to the output of a recursive This query would remove all direct and indirect subparts of a product. Data-modifying statements in The sub-statements in the outer the outer Trying to update the same row twice in a single statement is not supported. Only one of the modifications takes place, but it is not easy (and sometimes not possible) to reliably predict which one. This also applies to deleting a row that was already updated in the same statement: only the update is performed. Therefore you should generally avoid trying to modify a single row twice in a single statement. In particular avoid writing At present, any table used as the target of a data-modifying statement inWITH
provides a way to write auxiliary statements for use in a larger query. These statements, which are often referred to as Common Table Expressions orCTEs, can be thought of as defining temporary tables that exist just for one query. Each auxiliary statement in aWITH
clause can be aSELECT
,INSERT
,UPDATE
,DELETE
, orMERGE
; and theWITH
clause itself is attached to a primary statement that can also be aSELECT
,INSERT
,UPDATE
,DELETE
, orMERGE
.7.8.1.
SELECT
inWITH
#SELECT
inWITH
is to break down complicated queries into simpler parts. An example is:WITH regional_sales AS ( SELECT region, SUM(amount) AS total_sales FROM orders GROUP BY region), top_regions AS ( SELECT region FROM regional_sales WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales))SELECT region, product, SUM(quantity) AS product_units, SUM(amount) AS product_salesFROM ordersWHERE region IN (SELECT region FROM top_regions)GROUP BY region, product;
WITH
clause defines two auxiliary statements namedregional_sales
andtop_regions
, where the output ofregional_sales
is used intop_regions
and the output oftop_regions
is used in the primarySELECT
query. This example could have been written withoutWITH
, but we'd have needed two levels of nested sub-SELECT
s. It's a bit easier to follow this way.7.8.2. Recursive Queries#
RECURSIVE
modifier changesWITH
from a mere syntactic convenience into a feature that accomplishes things not otherwise possible in standard SQL. UsingRECURSIVE
, aWITH
query can refer to its own output. A very simple example is this query to sum the integers from 1 through 100:WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100)SELECT sum(n) FROM t;
WITH
query is always anon-recursive term, thenUNION
(orUNION ALL
), then arecursive term, where only the recursive term can contain a reference to the query's own output. Such a query is executed as follows:UNION
(but notUNION ALL
), discard duplicate rows. Include all remaining rows in the result of the recursive query, and also place them in a temporaryworking table.UNION
(but notUNION 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 temporaryintermediate table.Note
RECURSIVE
allows queries to be specified recursively, internally such queries are evaluated iteratively.WHERE
clause, and so the query terminates.WITH RECURSIVE included_parts(sub_part, part, quantity) AS ( SELECT sub_part, part, quantity FROM parts WHERE part = 'our_product' UNION ALL SELECT p.sub_part, p.part, p.quantity * pr.quantity FROM included_parts pr, parts p WHERE p.part = pr.sub_part)SELECT sub_part, SUM(quantity) as total_quantityFROM included_partsGROUP BY sub_part
7.8.2.1. Search Order#
tree
using alink
field:WITH RECURSIVE search_tree(id, link, data) AS ( SELECT t.id, t.link, t.data FROM tree t UNION ALL SELECT t.id, t.link, t.data FROM tree t, search_tree st WHERE t.id = st.link)SELECT * FROM search_tree;
WITH RECURSIVE search_tree(id, link, data,path) AS ( SELECT t.id, t.link, t.data,ARRAY[t.id] FROM tree t UNION ALL SELECT t.id, t.link, t.data,path || t.id FROM tree t, search_tree st WHERE t.id = st.link)SELECT * FROM search_treeORDER BY path;
f1
andf2
:WITH RECURSIVE search_tree(id, link, data,path) AS ( SELECT t.id, t.link, t.data,ARRAY[ROW(t.f1, t.f2)] FROM tree t UNION ALL SELECT t.id, t.link, t.data,path || ROW(t.f1, t.f2) FROM tree t, search_tree st WHERE t.id = st.link)SELECT * FROM search_treeORDER BY path;
Tip
ROW()
syntax in the common case where only one field needs to be tracked. This allows a simple array rather than a composite-type array to be used, gaining efficiency.WITH RECURSIVE search_tree(id, link, data,depth) AS ( SELECT t.id, t.link, t.data,0 FROM tree t UNION ALL SELECT t.id, t.link, t.data,depth + 1 FROM tree t, search_tree st WHERE t.id = st.link)SELECT * FROM search_treeORDER BY depth;
Tip
WITH RECURSIVE search_tree(id, link, data) AS ( SELECT t.id, t.link, t.data FROM tree t UNION ALL SELECT t.id, t.link, t.data FROM tree t, search_tree st WHERE t.id = st.link)SEARCH DEPTH FIRST BY id SET ordercolSELECT * FROM search_tree ORDER BY ordercol;WITH RECURSIVE search_tree(id, link, data) AS ( SELECT t.id, t.link, t.data FROM tree t UNION ALL SELECT t.id, t.link, t.data FROM tree t, search_tree st WHERE t.id = st.link)SEARCH BREADTH FIRST BY id SET ordercolSELECT * FROM search_tree ORDER BY ordercol;
SEARCH
clause specifies whether depth- or breadth first search is wanted, the list of columns to track for sorting, and a column name that will contain the result data that can be used for sorting. That column will implicitly be added to the output rows of the CTE.7.8.2.2. Cycle Detection#
UNION
instead ofUNION ALL
can accomplish this by discarding rows that duplicate previous output rows. However, often a cycle does not involve output rows that are completely duplicate: it may be necessary 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 already-visited values. For example, consider again the following query that searches a tablegraph
using alink
field:WITH RECURSIVE search_graph(id, link, data, depth) AS ( SELECT g.id, g.link, g.data, 0 FROM graph g UNION ALL SELECT g.id, g.link, g.data, sg.depth + 1 FROM graph g, search_graph sg WHERE g.id = sg.link)SELECT * FROM search_graph;
link
relationships contain cycles. Because we require a“depth” output, just changingUNION ALL
toUNION
would not eliminate the looping. Instead we need to recognize whether we have reached the same row again while following a particular path of links. We add two columnsis_cycle
andpath
to the loop-prone query:WITH RECURSIVE search_graph(id, link, data, depth,is_cycle, path) AS ( SELECT g.id, g.link, g.data, 0,false, ARRAY[g.id] FROM graph g UNION ALL SELECT g.id, g.link, g.data, sg.depth + 1,g.id = ANY(path), path || g.id FROM graph g, search_graph sg WHERE g.id = sg.linkAND NOT is_cycle)SELECT * FROM search_graph;
f1
andf2
:WITH RECURSIVE search_graph(id, link, data, depth,is_cycle, path) AS ( SELECT g.id, g.link, g.data, 0,false, ARRAY[ROW(g.f1, g.f2)] FROM graph g UNION ALL SELECT g.id, g.link, g.data, sg.depth + 1,ROW(g.f1, g.f2) = ANY(path), path || ROW(g.f1, g.f2) FROM graph g, search_graph sg WHERE g.id = sg.linkAND NOT is_cycle)SELECT * FROM search_graph;
Tip
ROW()
syntax in the common case where only one field needs to be checked to recognize a cycle. This allows a simple array rather than a composite-type array to be used, gaining efficiency.WITH RECURSIVE search_graph(id, link, data, depth) AS ( SELECT g.id, g.link, g.data, 1 FROM graph g UNION ALL SELECT g.id, g.link, g.data, sg.depth + 1 FROM graph g, search_graph sg WHERE g.id = sg.link)CYCLE id SET is_cycle USING pathSELECT * FROM search_graph;
CYCLE
clause specifies first the list of columns to track for cycle detection, then a column name that will show whether a cycle has been detected, and finally the name of another column that will track the path. The cycle and path columns will implicitly be added to the output rows of the CTE.Tip
SEARCH
and aCYCLE
clause, but a depth-first search specification and a cycle detection specification would create redundant computations, so it's more efficient to just use theCYCLE
clause and order by the path column. If breadth-first ordering is wanted, then specifying bothSEARCH
andCYCLE
can be useful.LIMIT
in the parent query. For example, this query would loop forever without theLIMIT
:WITH RECURSIVE t(n) AS ( SELECT 1 UNION ALL SELECT n+1 FROM t)SELECT n FROM tLIMIT 100;
WITH
query as are actually fetched by the parent query. Using this trick in production is not recommended, because other systems might work differently. Also, it usually won't work if you make the outer query sort the recursive query's results or join them to some other table, because in such cases the outer query will usually try to fetch all of theWITH
query's output anyway.7.8.3. Common Table Expression Materialization#
WITH
queries is that they are normally evaluated only once per execution of the parent query, even if they are referred to more than once by the parent query or siblingWITH
queries. Thus, expensive calculations that are needed in multiple places can be placed within aWITH
query to avoid redundant work. Another possible application is to prevent unwanted multiple evaluations of functions with side-effects. However, the other side of this coin is that the optimizer is not able to push restrictions from the parent query down into a multiply-referencedWITH
query, since that might affect all uses of theWITH
query's output when it should affect only one. The multiply-referencedWITH
query will be evaluated as written, without suppression of rows that the parent query might discard afterwards. (But, as mentioned above, evaluation might stop early if the reference(s) to the query demand only a limited number of rows.)WITH
query is non-recursive and side-effect-free (that is, it is aSELECT
containing no volatile functions) then it can be folded into the parent query, allowing joint optimization of the two query levels. By default, this happens if the parent query references theWITH
query just once, but not if it references theWITH
query more than once. You can override that decision by specifyingMATERIALIZED
to force separate calculation of theWITH
query, or by specifyingNOT MATERIALIZED
to force it to be merged into the parent query. The latter choice risks duplicate computation of theWITH
query, but it can still give a net savings if each usage of theWITH
query needs only a small part of theWITH
query's full output.WITH w AS ( SELECT * FROM big_table)SELECT * FROM w WHERE key = 123;
WITH
query will be folded, producing the same execution plan asSELECT * FROM big_table WHERE key = 123;
key
, it will probably be used to fetch just the rows havingkey = 123
. On the other hand, inWITH w AS ( SELECT * FROM big_table)SELECT * FROM w AS w1 JOIN w AS w2 ON w1.key = w2.refWHERE w2.key = 123;
WITH
query will be materialized, producing a temporary copy ofbig_table
that is then joined with itself — without benefit of any index. This query will be executed much more efficiently if written asWITH w AS NOT MATERIALIZED ( SELECT * FROM big_table)SELECT * FROM w AS w1 JOIN w AS w2 ON w1.key = w2.refWHERE w2.key = 123;
big_table
.NOT MATERIALIZED
could be undesirable isWITH w AS ( SELECT key, very_expensive_function(val) as f FROM some_table)SELECT * FROM w AS w1 JOIN w AS w2 ON w1.f = w2.f;
WITH
query ensures thatvery_expensive_function
is evaluated only once per table row, not twice.WITH
being used withSELECT
, but it can be attached in the same way toINSERT
,UPDATE
,DELETE
, orMERGE
. In each case it effectively provides temporary table(s) that can be referred to in the main command.7.8.4. Data-Modifying Statements in
WITH
#INSERT
,UPDATE
,DELETE
, orMERGE
) inWITH
. This allows you to perform several different operations in the same query. An example is:WITH moved_rows AS ( DELETE FROM products WHERE "date" >= '2010-10-01' AND "date" < '2010-11-01' RETURNING *)INSERT INTO products_logSELECT * FROM moved_rows;
products
toproducts_log
. TheDELETE
inWITH
deletes the specified rows fromproducts
, returning their contents by means of itsRETURNING
clause; and then the primary query reads that output and inserts it intoproducts_log
.WITH
clause is attached to theINSERT
, not the sub-SELECT
within theINSERT
. This is necessary because data-modifying statements are only allowed inWITH
clauses that are attached to the top-level statement. However, normalWITH
visibility rules apply, so it is possible to refer to theWITH
statement's output from the sub-SELECT
.WITH
usually haveRETURNING
clauses (seeSection 6.4), as shown in the example above. It is the output of theRETURNING
clause,not the target table of the data-modifying statement, that forms the temporary table that can be referred to by the rest of the query. If a data-modifying statement inWITH
lacks aRETURNING
clause, then it forms no temporary table and cannot be referred to in the rest of the query. Such a statement will be executed nonetheless. A not-particularly-useful example is:WITH t AS ( DELETE FROM foo)DELETE FROM bar;
foo
andbar
. The number of affected rows reported to the client would only include rows removed frombar
.WITH
, for example:WITH RECURSIVE included_parts(sub_part, part) AS ( SELECT sub_part, part FROM parts WHERE part = 'our_product' UNION ALL SELECT p.sub_part, p.part FROM included_parts pr, parts p WHERE p.part = pr.sub_part)DELETE FROM parts WHERE part IN (SELECT part FROM included_parts);
WITH
are executed exactly once, and always to completion, independently of whether the primary query reads all (or indeed any) of their output. Notice that this is different from the rule forSELECT
inWITH
: as stated in the previous section, execution of aSELECT
is carried only as far as the primary query demands its output.WITH
are executed concurrently with each other and with the main query. Therefore, when using data-modifying statements inWITH
, the order in which the specified updates actually happen is unpredictable. All the statements are executed with the samesnapshot (seeChapter 13), so they cannot“see” one another's effects on the target tables. This alleviates the effects of the unpredictability of the actual order of row updates, and means thatRETURNING
data is the only way to communicate changes between differentWITH
sub-statements and the main query. An example of this is that inWITH t AS ( UPDATE products SET price = price * 1.05 RETURNING *)SELECT * FROM products;
SELECT
would return the original prices before the action of theUPDATE
, while inWITH t AS ( UPDATE products SET price = price * 1.05 RETURNING *)SELECT * FROM t;
SELECT
would return the updated data.WITH
sub-statements that could affect the same rows changed by the main statement or a sibling sub-statement. The effects of such a statement will not be predictable.WITH
must not have a conditional rule, nor anALSO
rule, nor anINSTEAD
rule that expands to multiple statements.