DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Comparing Managed Postgres Options on The Azure Marketplace
  • Useful System Table Queries in Relational Databases
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide

Trending

  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  1. DZone
  2. Data Engineering
  3. Databases
  4. Understanding Recursive Queries in Postgres

Understanding Recursive Queries in Postgres

Learn more about recursive queries in Postgres.

By 
Prasanna Venkataraman user avatar
Prasanna Venkataraman
·
Aug. 15, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
23.4K Views

Join the DZone community and get the full member experience.

Join For Free

Postgres provides us a way to write queries that are recursive in nature and fetch the data until the termination condition is met. This can be achieved using Common Table Expressions (CTEs).

Using the WITH clause, we can create a temporary table scope of which is bound to that query itself. If we can combine this with the RECURSIVE clause, we can write some pretty cool stuff like finding the topmost parent, traveling salesman problem or as simple as printing the numbers recursively, as shown below:

Basic Recursive Query

First, we will see how this basic example works. This query has 3 parts,

SELECT 1 as n #line 2

This is the initial value, and our working table would be populated with this.

SELECT n + 1 FROM print_numbers WHERE n < 5 #line 4

This is the recursive part. As you can see, we are referring to the print_numbers itself and this would get values from the working table, which I mentioned. This query would continue running till the termination condition is met ie. n should be less than 5 and it is important to avoid infinite loops.

SELECT n FROM print_numbers #line 6

This is the actual query, which is doing nothing but selecting all the values of column n from print_numbers CTE.

Ok so now that we have the basics, I will show you 2 places where we are using these recursive queries.

1. Given an entity name, find all its ancestors

Schema Structure

| id |                     name | parent |
|----|--------------------------|--------|
|  1 |                Apple Inc | (null) |
|  2 |               Google Inc | (null) |
|  3 |              Apple India |      1 |
|  4 |             Google India |      2 |
|  5 |            Apple Chennai |      3 |
|  6 |          Apple Bangalore |      3 |
|  7 | Apple Chennai - Ascendas |      5 |

Ok. Now to the query. Given “Apple Chennai — Ascendas”, we have to fetch all its ancestors,

Output:

| id |                     name | parent |
|----|--------------------------|--------|
|  7 | Apple Chennai - Ascendas |      5 |
|  5 |            Apple Chennai |      3 |
|  3 |              Apple India |      1 |
|  1 |                Apple Inc | (null) |

Again, this should be straightforward with the initial value, termination condition, and the final select query. The only catch is about the recursive part, which I will explain. To understand it, we will start from the base query.

Base query result:

| id |                     name | parent |
|----|--------------------------|--------|
|  7 | Apple Chennai - Ascendas |      5 |

Refer to this SQL Fiddle to play with the query:

http://sqlfiddle.com/#!17/85aaa/3

But our use case was slightly more complicated than this, which we will see next.

2. Fetch all the entities with the topmost parent

This means our output should be something like entity_id and its corresponding topmost parent's id. So it would be:

| id | parent_id |
|----|-----------|
|  1 |         1 |
|  2 |         2 |
|  3 |         1 |
|  4 |         2 |
|  5 |         1 |
|  6 |         1 |
|  7 |         1 |

As I mentioned, we are appending the id at each level to path column (#line 5), and finally, we are getting the minimum element from the path column (#line 8) as the topmost parent id.

I hope this is useful in learning about common table expressions.

Thanks!

Database

Published at DZone with permission of Prasanna Venkataraman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Comparing Managed Postgres Options on The Azure Marketplace
  • Useful System Table Queries in Relational Databases
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!