Summary: in this tutorial, you will learn how to create a PostgreSQL recursive view using the CREATE RECURSIVE VIEW statement.
Introduction to the PostgreSQL recursive view
In PostgreSQL, a recursive view is a view whose defining query references the view name itself.
A recursive view can be useful in performing hierarchical or recursive queries on hierarchical data structures stored in the database.
PostgreSQL 9.3 added a new syntax for creating a recursive view specified in the standard SQL. The CREATE RECURSIVE VIEW statement is syntax sugar for a standard recursive query.
Here’s the basic syntax of the CREATE RECURSIVE VIEW statement:
In this syntax:
First, specify the name of the view you want to create in the CREATE RECURSIVE VIEW clause. You can add an optional schema to the name of the view.
Second, add a SELECT statement to define the view. The SELECT statement references the view_name to make the view recursive.
The CREATE RECURSIVE VIEW statement is equivalent to the following statement: