PostgreSQL Recursive Query
Summary: in this tutorial, you will learn about the PostgreSQL recursive query using recursive common table expressions or CTEs.
Introduction to the PostgreSQL recursive query
In PostgreSQL, a common table expression (CTE) is a named temporary result set within a query.
A recursive CTE allows you to perform recursion within a query using the WITH RECURSIVE
syntax.
A recursive CTE is often referred to as a recursive query.
Here’s the basic syntax of a recursive CTE:
In this syntax:
cte_name
: Specify the name of the CTE. You can reference this CTE name in the subsequent parts of the query.column1
,column2
, … Specify the columns selected in both the anchor and recursive members. These columns define the CTE’s structure.- Anchor member: Responsible for forming the base result set of the CTE structure.
- Recursive member: Refer to the CTE name itself. It combines with the anchor member using the
UNION
orUNION ALL
operator. recursive_condition
: Is a condition used in the recursive member that determines how the recursion stops.
PostgreSQL executes a recursive CTE in the following sequence:
- First, execute the anchor member to create the base result set (R0).
- Second, execute the recursive member with Ri as an input to return the result set Ri+1 as the output.
- Third, repeat step 2 until an empty set is returned. (termination check)
- Finally, return the final result set that is a UNION or
UNION ALL
of the result sets R0, R1, … Rn.
A recursive CTE can be useful when dealing with hierarchical or nested data structures, such as trees or graphs.
PostgreSQL recursive query example
Let’s take an example of using a recursive query.
1) Setting up a sample table
First, create a new table called employees:
The employees
table has three columns: employee_id
, full_name
, and manager_id
. The manager_id
column specifies the manager id of an employee.
Second, insert some rows into the employees
table:
2) Basic PostgreSQL recursive query example
The following statement uses a recursive CTE to find all subordinates of the manager with the id 2.
Output:
How it works:
- The recursive CTE
subordinates
defines an anchor member and a recursive member. - The anchor member returns the base result set R0 which is the employee with the id 2.
The recursive member returns the direct subordinate(s) of the employee id 2. This is the result of joining between the employees
table and the subordinates
CTE. The first iteration of the recursive term returns the following result set:
PostgreSQL executes the recursive member repeatedly. The second iteration of the recursive member uses the result set above step as the input value, and returns this result set:
The third iteration returns an empty result set because no employee is reporting to the employee with the id 16, 17, 18, 19, and 20.
PostgreSQL returns the final result set which is the union of all result sets in the first and second iterations generated by the non-recursive and recursive members.
Summary
- Use the
WITH RECURSIVE
syntax to define a recursive query. - Use a recursive query to deal with hierarchical or nested data structures such as trees or graphs.