Helm7

Can Postgres be your graph database?

Yes, for most applications. Postgres stores relationships as foreign keys, and a recursive query can walk them to any depth you set. You only need a dedicated graph database when you run deep pattern matching, want Cypher as your main language, or run graph algorithms over very large graphs.

Checked against current documentation on 16 November 2026.

What a graph database is for

A graph is two things: things and the links between them. Graph people call the things nodes and the links edges. A customer is a node, an order is a node, and "placed by" is an edge between them.

Neo4j describes its own database as one that stores data as nodes, relationships and properties instead of tables or documents. Its relationships can carry properties, just as nodes can. You query that model with Cypher, a language built for matching patterns of nodes and edges. See the Neo4j graph database introduction.

Here is the thing most guides skip. A relational database already stores nodes and edges. Every row is a node. Every foreign key is an edge. You have been running a graph for years, and the question is only whether SQL is a comfortable way to walk it.

Foreign keys are your edges

A foreign key says that a value in one table must match a row in another. The PostgreSQL manual shows the pattern with products and orders: an orders column that references the products table. That is an edge from an order to a product.

Many-to-many links use a join table. Each row in the join table is one edge, and it can carry its own columns, such as when the link was made. That covers the "properties on relationships" idea that graph databases advertise.

CREATE TABLE person (
  id   bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  name text NOT NULL
);

-- One row per edge: follower_id follows followee_id.
CREATE TABLE follows (
  follower_id bigint NOT NULL REFERENCES person (id),
  followee_id bigint NOT NULL REFERENCES person (id),
  since       date   NOT NULL DEFAULT current_date,
  PRIMARY KEY (follower_id, followee_id)
);

-- The primary key indexes walks that go forward. This one covers walks backward.
CREATE INDEX follows_followee_idx ON follows (followee_id);
A small social graph in plain Postgres. The second index matters: the manual notes that declaring a foreign key does not index the referencing column.

That last point catches people out. The manual says it is often a good idea to index the referencing columns, because deletes and updates on the referenced table scan them. Graph walks hit the same columns. An edge table with no index in the direction you walk is the usual reason a traversal is slow.

Walking the graph with WITH RECURSIVE

The PostgreSQL manual describes the shape of a recursive query. You write a starting term, then UNION or UNION ALL, then a term that refers to the query's own output. Postgres runs the start, then repeats the second part against the rows it just found, until nothing new appears.

The risk is a loop. If A follows B and B follows A, a naive walk never ends. Plain UNION removes exact duplicate rows, but the manual warns that it does not stop cycles where only some fields repeat. For example, a row that carries a depth counter is never an exact duplicate.

PostgreSQL 14 added the SQL-standard SEARCH and CYCLE clauses for this. Its release notes say the same results could be reached with existing syntax, "but much less conveniently". CYCLE names the columns to watch, a flag column that says a loop was found, and a column that records the path taken.

-- Everyone within three hops of person 1, loops cut.
WITH RECURSIVE reach(id, depth) AS (
    SELECT followee_id, 1
    FROM follows
    WHERE follower_id = 1
  UNION ALL
    SELECT f.followee_id, r.depth + 1
    FROM follows f
    JOIN reach r ON f.follower_id = r.id
    WHERE r.depth < 3
) CYCLE id SET is_cycle USING path
SELECT p.name, min(r.depth) AS hops
FROM reach r
JOIN person p ON p.id = r.id
WHERE NOT r.is_cycle
GROUP BY p.name
ORDER BY hops, p.name;
A bounded, cycle-safe walk. The depth limit and the CYCLE clause do different jobs: one stops long walks, the other stops loops.

Always keep a depth limit as well. CYCLE protects you from infinite loops. It does not protect you from a walk that fans out into millions of rows. In a social graph, three hops can reach a large share of the whole table.

If you want results in breadth-first order, the manual also documents SEARCH BREADTH FIRST. It adds an ordering column. The manual is clear that this affects how rows are sorted afterwards, not the order Postgres evaluates them in.

Where Postgres is enough, and where Neo4j is better

Be honest about the workload. Most "graph" needs in an application are small. Who reported this bug, which orders touch this product, what is connected to this account, what did this person approve. Those are one to four hops from a known starting row. Postgres handles that well, and you keep one database, one backup and one set of permissions.

Rule-of-thumb comparison for choosing a store. The Neo4j figures describe its documented model and tools; no speed claims are made.
NeedPostgres with recursive SQLA dedicated graph database such as Neo4j
Walk 1 to 4 hops from a known rowA good fitWorks, with a second system to run
Keep graph and business data in one transactionNatural: it is one databaseNeeds syncing between two stores
Row-level permissions you already wroteReused as they areRebuilt in the graph store
Match a pattern such as "A knows B, B works with C, C bought D"Possible, but the SQL grows quicklyWhat Cypher is designed for
Run graph algorithms over a large graphNot built inNeo4j Graph Data Science provides them
Write queries in CypherNot supported by core PostgresNative

Neo4j's Graph Data Science library is the clearest example of what you give up. Neo4j documents it as parallel versions of common graph algorithms, covering centrality, community detection, partitioning and clustering. It loads graph data into an in-memory catalog so the algorithms run fast. Postgres has nothing comparable built in. See the Graph Data Science introduction.

So the honest advice is this. Start with Postgres if your graph questions are short walks from a known row and your data already lives in tables. Move to a dedicated graph database when patterns get long and tangled, when Cypher would save your team real time, or when you need to run graph algorithms across the whole graph. If you want Cypher but also want to stay inside Postgres, the extension route is covered in Apache AGE and its alternative.

Keeping walks fast and safe

Three habits keep graph queries in Postgres healthy. Each one is cheap to adopt at the start and painful to add later.

  • Index both ends of every edge table. The primary key covers one direction. Add an index for the other, as the follows example does.
  • Cap the fan-out. A single popular row, such as an account with a million followers, can make one hop explode. Limit how many neighbours you take from any one row, and limit the total rows a walk may return.
  • Set a statement timeout for walks that run on behalf of a user or an agent. Postgres lets you set one per role or per transaction, so a runaway query is stopped by the database and not by hope.

Also decide early what an edge means. A foreign key says "this row belongs to that one". A join table can say "knows", "follows" or "approved", and a label column can tell several kinds of link apart in one table. Keeping a single edge table with a label is flexible. Keeping one table per kind of link is easier to index and to constrain. Both work.

How Helm7 does it

Helm7's relationships switch takes the first approach. It does not install Apache AGE and it keeps no second copy of your data. It reads your foreign keys from the Postgres catalogue and walks them. The switch is off by default, per database.

Two additions cover links that are not real foreign keys. Virtual foreign keys describe a column that points at another table without a constraint, and Helm7 suggests them from column names ending in _id. Labelled edges, stored in a table in a helm7 schema inside your own database, link any row to any other, such as "duplicate of" or "mentions".

A traversal compiles to one WITH RECURSIVE query with a CYCLE clause. It starts at a row, or at the best semantic matches for some words. Caps keep it safe on a shared database: at most 4 hops, 50 neighbours per row per hop, 500 rows in total, and a 5 second statement timeout. It runs as your app's own database role, so row level security applies and a hidden row is never returned or passed through.

// 1. Preview what turning it on would do. Nothing changes.
database_graph_enable
{ "productId": "prd_...", "databaseId": "DATABASE_ID", "dry_run": true }

// 2. Everything within two hops of customer 42.
database_graph_traverse
{ "productId": "prd_...", "databaseId": "DATABASE_ID",
  "start": { "table": "customers", "pk": "42" },
  "max_depth": 2, "fanout": 10 }
Two Helm7 tool calls. The dry run lists foreign key columns with no index, since walking inwards across one of those would scan the table.

There is also an opt-in step that suggests edges. It compares rows by vector similarity, asks a model to judge each pair, and stores the results as proposed. Proposed edges are ignored until you approve them. Reject one by deleting it.

This is one option among several. If you already run Neo4j, or you need Cypher and graph algorithms, Helm7 is not a replacement. It is for apps that want to follow their own relationships without adding another database. The databases page has the full list of switches. If you also want search by meaning, read semantic search on Postgres with pgvector.

Common questions

  • Is Postgres a graph database?

    Not by design, but it can act as one. Foreign keys are edges and rows are nodes, and recursive queries walk them. It lacks a native graph query language and built-in graph algorithms, which dedicated graph databases provide.

  • What does the CYCLE clause do?

    CYCLE tells Postgres which columns to watch for repeats in a recursive query. It adds a flag column that marks a row that closes a loop, and a column that holds the path. Postgres stops following a path once it loops. It was added in PostgreSQL 14.

  • When should I use Neo4j instead?

    When your questions are long, tangled patterns, when you want Cypher as your main language, or when you need to run graph algorithms over a large graph. Neo4j's Graph Data Science library covers algorithms such as community detection that Postgres does not include.

  • Do I need an index on foreign key columns?

    Yes, on the side you walk from. The PostgreSQL manual notes that a foreign key does not automatically index the referencing column. Without an index, each hop can scan the whole table.

  • Does Helm7 use Apache AGE?

    No. Helm7 reads foreign keys from the Postgres catalogue, adds optional virtual foreign keys and labelled edges, and walks them with one recursive SQL query. It keeps no second copy of your data.

Run your entire app stack, end to end, from $12 a month.

App, Postgres, auth, storage, jobs and domains on one platform, planned and priced before anything changes.

More guides