PostgreSQL Graph Database: Everything You Need To Know

Sa Wang
Software Engineer
March 25, 2024
PostgreSQL Graph Database: Everything You Need To Know


Searching for insights on PostgreSQL’s potential as a graph database? This article cuts to the chase, presenting the essential information needed to understand PostgreSQL’s graph database functionality. With the use of extensions such as Apache AGE, PostgreSQL transcends traditional relational database limits, embracing graph data’s interconnected nature. Learn about PostgreSQL’s tools for graph data processing and real-world applications without the sales pitch.

Key Takeaways

  • Graph databases, such as Neo4j, use structures of nodes and edges to efficiently store and query highly connected data, allowing for greater flexibility and relationship-focused data modeling compared to traditional relational databases.
  • PostgreSQL, primarily a relational database, can function as a graph database with extensions like Apache AGE, enabling graph data processing and the execution of graph and relational queries within the same environment.
  • Performance optimization in PostgreSQL graph databases involves techniques like indexing, the EXPLAIN ANALYZE command, and partitioning. Adhering to best practices is crucial for effectively tackling performance issues and the complexity of usage.
  • PuppyGraph, as the first and only graph query engine, can directly query one or more of users’ existing relational databases, including PostgreSQL, into a unified graph model in under 10 minutes. By bypassing traditional graph databases' cost, latency, and maintenance hurdles, PuppyGraph is a powerful option for companies that look to add graph models on their existing SQL data but don’t want to maintain a separate graph database and complex ETLs.

Understanding Graph Databases

An example of graph visualization by PuppyGraph

A graph database is a type of NoSQL database that uses graph structures with nodes, edges, and properties to represent and store data. It places relationships at the heart of its data model, treating them as equally important to the data itself. Complex data can be queried at high-performance due to this feature. In contrast to the fixed schema of relational databases, graph databases offer more flexibility. Adding new attributes is made possible without restructuring the entire database.

One popular graph database is Neo4j, which operates with a native graph storage model. The absence of an underlying abstraction layer allows data to be stored exactly as modeled, thus enhancing performance and flexibility. Neo4j is ACID-compliant, supporting atomicity, consistency, isolation, and durability for transactions.

Graph databases are designed to efficiently handle complex, highly connected data and relationship analysis. With the increasing importance of graph data analytics, relational databases might find this cumbersome due to the resource-intensive nature of join operations. Graph databases excel in scenarios where data is highly connected, data retrieval is a priority over simple storage, and where data models change frequently or require flexibility.

A graph query language is a specialized language designed to interact with graph databases or structures, allowing users to explore relationships within the data. It offers a way to express complex queries for traversing relationships, aggregating data, and applying filters. These languages cater to graph-oriented models, where entities are connected by edges, enabling the retrieval of specific insights and navigation through the graph.

One notable graph query language is OpenCypher or Cypher, used by databases like Neo4j, SAP HANA Graph, and Redis graph. Cypher facilitates an intuitive approach to working with property graphs by emphasizing relationships over the traditional columns and rows seen in SQL. As a declarative language, it is optimized for graph data, making it a powerful tool for uncovering patterns and understanding connections within data. Graph query languages, including Cypher, Gremlin, and SPARQL, are essential for efficiently querying graph data, offering users the ability to gain a deeper insight into their data's relational aspects.Yet, graph databases do not present a one-size-fits-all solution. They are less optimal for scenarios where:

  • Most queries involve scanning the entire graph
  • Handling simple key/value data retrieval
  • Storing large attributes like BLOBs and CLOBs without needing to connect them to other entities.

PostgreSQL as a Graph Database

PostgreSQL is best known as a relational database system. However, it can also function as a graph database, although in an awkward way without any additional extensions. With extensions like Apache AGE, PostgreSQL's graph database capabilities are greatly improved. Apache AGE extends PostgreSQL to support graph data models, enabling users to work with graph data (nodes and edges) alongside traditional relational data. It integrates graph database capabilities into PostgreSQL without requiring a separate graph database system. This integration allows users to benefit from PostgreSQL's features, such as ACID transactions, while also performing graph-specific operations.

Apache AGE enables users to:

  • Gain access to graph database functionality within PostgreSQL, allowing them to perform graph query modeling within Postgres' existing relational model.
  • Read and write graph data in nodes and edges, and use various algorithms for data analysis, such as variable length and edge traversal.
  • Execute hybrid queries that combine openCypher and SQL, enabling simultaneous querying of relational and graph data.
  • Achieve fast graph query processing for both indexing and efficient query execution.
  • Visualize graph and relational data for a clearer understanding of complex data relationships.
  • Utilize a web user interface, Apache AGE Viewer, for data visualization and exploration, handling large graph data and providing insights through various graph algorithms.

Apache AGE comes with the following features:

  • It is optimized for handling complex connected graph data and provides robust database features like ACID transactions, multi-version concurrency control (MVCC), stored procedures, triggers, constraints, and a flexible data model (JSON).
  • It supports the Cypher query language, enabling users to write complex queries more easily and query multiple graphs simultaneously.
  • It allows for the creation of property indexes on vertices and edges to speed up graph queries.
  • Apache AGE fully supports PostgreSQL features and API, extending PostgreSQL's capabilities with graph database functionality.
  • It is intended to be compatible with all relational databases in future development, starting with MariaDB and MySQL.
  • Apache AGE is a community-driven project with support for various programming languages through relevant SDKs and drivers.

Graph Database Modeling in PostgreSQL

In PostgreSQL, creation and modeling of graph data involve creating nodes and edges. Typically, two tables are created: one for nodes and one for edges. The nodes table usually contains an auto-incrementing primary key and additional data columns to store node attributes. The edges table references two rows from the nodes table to represent relationships, with foreign keys to node IDs and a composite primary key ensuring uniqueness. Inserting nodes and edges into these tables creates the graph data structure, which can then be visualized and queried.

With Apache AGE, vertex and edge can be labeled by specific functions. Thus a new table is generated within the graph's namespace. We can also create vertices and edges with Cypher directly. It will automatically make tables with the contents. We can just use the intuitive representation of graphs like other graph databases. 

Querying Graph Data in PostgreSQL

The use of recursive queries, which permit traversing graph data structures without specifying the exact number of required joins, enables querying of the graph data in PostgreSQL. These recursive queries consist of a base case and a recursive case, using the WITH RECURSIVE clause for graph traversal. In this way, we can write complex queries like n-hop, but in a somewhat clumsy manner.

Apache AGE allows for the integration of Cypher queries within SQL queries. The Cypher part of a query is wrapped inside a cypher function, enabling hybrid queries that combine the power of SQL with the expressiveness of Cypher for graph data manipulation. This approach allows users to perform complex queries involving both relational and graph data, leveraging the strengths of both models. 

Apache AGE processes Cypher queries through a series of steps. AGE parses Cypher queries that are embedded in Cypher function calls, interpreting the grammar of the openCypher language. The parsed Cypher query is transformed into a query tree, which PostgreSQL can understand. This tree is then attached as a subquery node within the larger SQL query execution plan. AGE understands specific graph operations and generates plan nodes related to these operations, integrating them into the overall query execution plan. The plan nodes related to graph operations are executed within PostgreSQL's query processing framework, ensuring that graph queries work seamlessly with PostgreSQL's fully transactional system.

Performance Optimization

Several techniques are involved in the optimization of the performance of graph databases in PostgreSQL. One such method is indexing, considered a key technique for optimizing database performance. Indexes in PostgreSQL and Apache AGE can be single-column, for speeding up searches based on one column’s values, or multi-column, for optimizing queries involving multiple columns. In Apache AGE, indexing graph data structures involves creating indexes for nodes and relationships to optimize graph queries.

The EXPLAIN ANALYZE command in PostgreSQL is another technique. It can optimize queries by providing a detailed execution plan along with performance metrics. Reindexing, the process of rebuilding an index to improve database performance, can be useful for maintaining index efficiency over time.

Another performance optimization technique is partitioning. Declarative partitioning is a PostgreSQL feature that simplifies the creation and management of partitions. Partitioning by range, where tables can be divided according to a time column, helps speed up query execution and makes data processing more convenient. It is essential, however, to manage the number of locks created during queries and transactions on partitioned tables to prevent the exhaustion of shared memory and potential downtime.

Real-World Use Cases

In today's data-driven world, the ability to model, analyze, and leverage complex relationships within data can provide organizations with a significant edge. This section explores some real-world use cases where PostgreSQL as a graph database shines, illustrating its versatility and power across different industries and applications.

Social Networks

Social networking platforms are perhaps the most intuitive examples of graph data applications, where users (nodes) are interconnected through various types of relationships (edges), such as friendships, followers, or likes. Using PostgreSQL with a graph extension allows for efficient querying and analysis of these connections, enabling features like friend recommendations based on mutual connections, influence scoring, and the detection of communities within the network.

Recommendation Systems

E-commerce platforms and content providers can use PostgreSQL as a graph database to power their recommendation engines. By modeling products, users, and interactions (views, purchases, ratings) as a graph, businesses can easily identify patterns and relationships between users and items. This setup facilitates the implementation of sophisticated recommendation algorithms that suggest products or content based on a user's behavior and preferences, significantly enhancing user experience and engagement.

Fraud Detection

In the financial sector, graph databases can significantly enhance fraud detection mechanisms. By representing transactions and account holders as graphs, it becomes easier to spot unusual patterns, such as circular transactions that might indicate money laundering or multiple accounts linked to a single individual in a suspicious manner. PostgreSQL, equipped with graph processing capabilities, can handle real-time analysis of these networks, allowing for the rapid detection and mitigation of fraudulent activities.

Network and IT Operations

Graph databases are exceptionally well-suited for modeling and managing network infrastructures, including data centers and cloud environments. Nodes and edges can represent various components (servers, routers, applications) and their connections or dependencies. PostgreSQL with graph extensions can help IT professionals visualize the network topology, perform impact analysis for planned changes or outages, and detect anomalies or bottlenecks within the network, leading to more efficient operations and maintenance.

Knowledge Graphs

Knowledge graphs represent a collection of interlinked descriptions of entities – objects, events, or concepts. PostgreSQL, when used as a graph database, can store and manage these entities and their complex interrelations, supporting semantic queries that enable more intuitive and powerful searches. This is particularly useful in fields like research, where discovering connections between different studies, concepts, or datasets can uncover new insights, or in enhancing search engines and virtual assistants with a deeper understanding of user queries.

Challenges and Best Practices

Utilizing PostgreSQL for graph database purposes entails employing its inherent relational database functionalities to manage and interrogate graph-based data. Graph databases are uniquely structured to prioritize relationships between data points as much as the data itself. Below, we list various challenges and best practices for effectively utilizing PostgreSQL for graph data management.


  • Schema Design Complexity: Designing a schema to effectively model graph structures within a relational database can be complex. You need to ensure that your tables and relationships accurately represent your graph data.
  • Recursive Query Performance: While PostgreSQL supports recursive queries (e.g., with Common Table Expressions or CTEs), these can become inefficient for deep recursion or very large graphs.
  • Managing Relationships: In a pure graph database, relationships are first-class citizens. In PostgreSQL, relationships must be managed with foreign keys and join tables, which can add overhead and complexity.
  • Indexing and Optimization: Proper indexing is crucial for performance, but finding the right indexing strategy for graph-like queries in PostgreSQL can be challenging.
  • Feature Limitations: PostgreSQL lacks some of the advanced graph-specific features found in dedicated graph databases, such as graph algorithms and optimizations for traversals.

Best Practices

  • Use Extensions: Leveraging PostgreSQL extensions like Apache AGE provides native graph data types, functions and performance optimizations to enable graph database capabilities within PostgreSQL, simplifying modeling and querying complex graph structures compared to manual schema creation.
  • Optimize Schema Design: Design your schema with graph operations in mind. Use tables to represent nodes and edges, ensuring that foreign keys and indexes are properly set up to optimize joins and recursive queries.
  • Leverage Recursive CTEs for Traversals: For traversing relationships, make extensive use of recursive Common Table Expressions to execute queries that walk through the graph.
  • Indexing Strategy: Apply appropriate indexes (e.g., GIN or GIST indexes) on foreign keys and any fields frequently used in WHERE clauses or as join conditions to speed up queries.
  • Partitioning: For very large graphs, consider partitioning your tables to improve query performance by reducing the number of rows scanned during each query.
  • Monitor and Optimize Queries: Regularly monitor query performance using EXPLAIN ANALYZE and optimize queries by tweaking indexes, adjusting the schema, or refactoring queries to reduce complexity.
  • Stay Updated: PostgreSQL continues to evolve, so staying updated with the latest versions can ensure you benefit from performance improvements and new features that might enhance your graph data handling capabilities.

Integrating Graph and Relational Data

PostgreSQL, being a robust database, allows for seamless integration of graph and relational data when working with extensions. As the complexity of queries and data escalates, solutions like PuppyGraph, which facilitate powerful hybrid queries and data analysis, might be the best option.

Before and after PuppyGraph architecture diagram 

PuppyGraph, the first and only graph query engine in the market, enables multiple graphs hierarchically and the simultaneous execution of SQL, Cypher and Gremlin queries on the same table, thereby making it possible to leverage the power of both relational and graph databases within a single database environment - no ETL required. This hybrid approach, which enables querying multiple graphs, is particularly beneficial for complex queries that require both relational data and complex graph data. Moreover, it allows users to query graph data efficiently. PuppyGraph's benchmarks show that its performance at scale heavily outweighs anything offered by other graph databases on the market (e.g., achieving 10-hop in less than 2 seconds).

PuppyGraph sets itself apart by decoupling storage from computation, capitalizing on the advantages of columnar data lakes to deliver significant scalability and performance gains. When conducting intricate graph queries like multi-hop neighbor searches, the need arises to join and manipulate numerous records. The columnar approach to data storage enhances read efficiency, allowing for the quick fetching of only the relevant columns needed for a query, thus avoiding the exhaustive scanning of entire rows.

PuppyGraph boosts efficiency by employing min/max statistics and predicate pushdown, dramatically decreasing the volume of data that needs to be scanned. Its integration with vectorized data processing, which executes operations on groups of values at once, further elevates PuppyGraph’s scalability and swift query response capability. This approach not only simplifies data analysis but also enhances query performance. Additionally, its auto-partitioned, distributed computing architecture effectively handles large-scale datasets, guaranteeing strong scalability in terms of both storage and computational power.

All of these above imply that as a developer, you are not confined to choosing between relational or graph databases. You can enjoy the best of both worlds, managing complex connected graph data alongside maintaining the robust database features vital for handling large data sets, while also being able to support multiple relational databases, including PostgreSQL. If your data is already prepared, no extract, transform, load (ETL) processes are needed before using PuppyGraph. These features establish PuppyGraph as an ideal choice for processing complex, connected data in the current Big Data era. To learn more, here is the getting started for PuppyGraph and how to query PostgreSQL data as a graph using PuppyGraph.


The ability to perform both relational and graph queries in PostgreSQL with extensions opens up new possibilities. Developers can now enjoy the reliability and maturity of a relational database while also leveraging graph structures for complex connected data analysis. As PostgreSQL continues to expand its functionality, it further solidifies its place as a versatile, scalable open-source database solution for modern applications. With robust relational capabilities augmented by powerful graph analytics and graph query engine tools like PuppyGraph, PostgreSQL offers the best of both worlds in a single platform. 

Ready to add graph models on your existing PostgreSQL data? Download the forever free PuppyGraph Developer Edition or begin your free 30-day trial of the Enterprise Edition today. 

Sa Wang is a Software Engineer with exceptional mathematical abilities and strong coding skills. He earned his Bachelor's degree in Computer Science from Fudan University and has been studying Mathematical Logic in the Philosophy Department at Fudan University, expecting to receive his Master's degree in Philosophy in June this year. He and his team won a gold medal in the Jilin regional competition of the China Collegiate Programming Contest and received a first-class award in the Shanghai regional competition of the China University Student Mathematics Competition.

Join our newsletter

See PuppyGraph
In Action

See PuppyGraph
In Action

Graph Your Data In 10 Minutes.

Get started with PuppyGraph!

PuppyGraph empowers you to seamlessly query one or multiple data stores as a unified graph model.

Dev Edition

Free Download

Enterprise Edition


  • Forever free
  • Single node
  • Designed for proving your ideas
  • Available via Docker install


Based on the Memory and CPU of the server that runs PuppyGraph.
  • 30 day free trial with full features
  • Everything in Developer + Enterprise features
  • Designed for production
  • Available via AWS AMI & Docker install
* No payment required

Developer Edition

  • Forever free
  • Single noded
  • Designed for proving your ideas
  • Available via Docker install

Enterprise Edition

  • 30-day free trial with full features
  • Everything in developer edition & enterprise features
  • Designed for production
  • Available via AWS AMI & Docker install
* No payment required