Performance: compiled Cypher on PostgreSQL against Neo4j
This page compares the two end to end: a Cypher query compiled by cypher2sql and run as SQL on PostgreSQL, against the same query answered by Neo4j 5. On 603,500 identical rows it was 1.6× to 17.1× faster on all six query shapes (median 6.1×).
Each bar is how many times faster PostgreSQL answered that shape; higher is better, and all six clear the 1× "same as Neo4j" line.
One box per engine and shape over the same 25 timed runs: the box spans the quartiles, the whiskers 1.5×IQR, dots are outliers, and the number is the median. Log millisecond axis; lower is better. Every Neo4j figure includes its HTTP round trip.
The numbers
All figures are milliseconds as p50 / p95 over 25 timed runs; lower is better.
| Shape | PostgreSQL (ms) | Neo4j (ms) |
|---|---|---|
| 1. Single hop (400) | 1.027 / 1.294 | 8.712 / 9.770 |
| 2. Join-table hop (1) | 0.813 / 1.153 | 2.705 / 3.619 |
| 3. Products bought by one country (2,000) | 32.431 / 42.440 | 51.827 / 69.592 |
| 4. Products within three hops of one customer (664) | 4.511 / 5.435 | 77.200 / 113.355 |
| 5. Property filter (95) | 0.532 / 0.583 | 2.857 / 3.447 |
| 6. Paging (20) | 0.377 / 0.473 | 2.542 / 3.252 |
What the multiplier is made of
The timed PostgreSQL run is Schema.to_sql(...) plus psycopg execute and fetch over the local socket, so its samples include the compile; the compile-only table below isolates that share, 0.2–0.6 ms at p50 across the six shapes. Neo4j's own parse and plan happen inside its HTTP round trip, which its figures include.
Compile is not execution
| Shape | Compile p50 (µs) | Compile p95 (µs) |
|---|---|---|
| 1. Single hop | 486 | 543 |
| 2. Join-table hop | 603 | 668 |
| 3. Products bought by one country | 511 | 556 |
| 4. Products within three hops of one customer | 554 | 1,035 |
| 5. Property filter | 357 | 386 |
| 6. Paging | 218 | 237 |
A compiled pattern is one plain SELECT. This is MATCH (c:Customer {country: $country}) RETURN c compiled:
from tests.perf_fixture import SHAPES
simple = SHAPES[0]
q = simple.schema.to_sql(simple.cypher, simple.params)
print(q.sql)
Each win below comes from the plan, not from a different answer. The keys returned by both engines were identical on all six shapes.
- Single hop. The customer is a primary-key lookup and the orders are one
EXISTSreached throughorder_customer_id; the answer is 400 keys. - Join-table hop. The order's two lines come from the
order_lineprimary key(order_id, product_sku), the quantity predicate is applied there, andproduct_sku_cfetches the surviving product. - Inferred A–C–B. The country filter,
order_customer_idand theorder_linekey carry the chain. The 2,000 distinct products are the only wide step, which is why 32.431 ms is the smallest multiplier. - Untyped walk. Each recursive step looks the edge table up from the current frontier row by its own key columns, so the plan is an index-only scan per frontier row rather than a full read of every edge row; that is the 17.1×.
- Property filter. The composite
product_category_price_cindex serves both the pinned category and the price bound. - Paging. The country index serves the filter, and only 20 keys are returned after
ORDER BY idwithSKIP/LIMIT.
Where Neo4j could still win
On these six shapes Neo4j wins none at p50. The Neo4j figures at 2.542–2.857 ms sit at the roughly 3 ms HTTP floor, while PostgreSQL returns those answers in 0.377–0.813 ms, so those ratios measure the driver as much as the engine. A native graph engine still wins when traversal is the only work and the depth is far greater than three; that case is not measured here.
PostgreSQL only: compile, plan, execute
These five rows are a separate, earlier measurement on the same host and the smaller shop fixture: 10,000 customers, 50,000 orders, 100,000 order lines, 10,000 products, 20,000 reviews and 20,000 substitution rows. They are not mixed into the Neo4j ratios above.
Compile is the wall time of Schema.to_sql(...), median of 5 calls. Execution and planning are the median of 5 EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) runs.
| Shape (rows) | Compile (µs, median of 5) | Execution (ms, median of 5) |
|---|---|---|
| Simple match, selective filter (100) | 394 | 0.066 |
| Two typed hops, join tier (201) | 620 | 7.168 |
| Untyped inferred path, 32 alternatives (1) | 2,536 | 0.221 |
Recursive [*1..3] walk (14) |
399 | 4.912 |
| Recursive walk, pinned target (1) | 432 | 0.035 |
| Shape | Planning (ms, median of 5) | Shared buffers |
|---|---|---|
| Simple match, selective filter | 0.023 | 66 hit + 0 read |
| Two typed hops, join tier | 0.247 | 2,843 hit + 0 read |
| Untyped inferred path, 32 alternatives | 5.035 | 15 hit + 0 read |
Recursive [*1..3] walk |
0.167 | 429 hit + 0 read |
| Recursive walk, pinned target | 0.143 | 8 hit + 0 read |
Shared buffers are the root plan's blocks hit plus read; a node's counters already include its children's, so summing every node would count the same blocks many times.
- Selective match:
Bitmap Heap Scan→Bitmap Index Scan. The test fails if the shape stops being reached through an index that applies the pinned collation. - Two typed hops: one
Hash Joinover aSeq Scan. The target is read once because the emitted SQL keeps it inFROMand the correlated key insideEXISTS; the test accepts that scan only when the plan's own estimate expects at least half the table. - Inferred path:
Nested Loop→Index Only Scan. The plan holds 32 sibling subplans with no nesting, so doubling the alternatives roughly doubles the plan rather than squaring it. - Recursive walk: one statement-level
WITH RECURSIVE, oneRecursive Union, one loop, whether or not the target key is pinned. The test asserts both rows and the emitted SQL is snapshot-tested.
Compilation stays sub-millisecond except for the 32-alternative shape, where the emitted OR EXISTS list is 32 times the SQL. Execution is where the differences live, and they are plan differences, not SQL-text differences.
Print the same table on your own server:
import psycopg
from tests.perf_fixture import build_fixture
from tests.perf_report import report
conn = psycopg.connect(autocommit=True)
build_fixture(conn)
print(report(conn))
The collation trap
Every comparison against a value is emitted with COLLATE "C" so results follow code points, not your database's collation (see Strings and collation). A btree index on a text column with another collation cannot serve column COLLATE "C" = $1, so add the matching expression indexes: CREATE INDEX customer_country_c ON customer ((country COLLATE "C")) and CREATE INDEX product_sku_c ON product ((sku COLLATE "C")).
A pinned link is compared by code point on both sides, so both text columns need a matching expression index or a COLLATE "C" declaration; an index on one side alone is not enough. For the order-line link that means CREATE INDEX order_line_product_sku_c ON order_line (product_sku COLLATE "C") as well as product_sku_c. A declared node key pins the edge's undeclared foreign-key or join-table column too, and when neither key column is declared both keep their own collation and the plain index serves the link.
Methodology
What was timed
Measured 2026-11-29 on an Intel Core i7-8700 at 3.20 GHz (8 logical cores), 32 GiB of RAM, Linux 6.12.118+deb13-amd64, Python 3.13.18, PostgreSQL 18.7 with database collation en_US.UTF-8, and Neo4j from neo4j:5-community (heap 2G, pagecache 1G), pinned by digest sha256:c5fd91e41d44858ea678813fd53f8e60602e771a7656c502385247ffbcb0aec0.
Dataset seed 20261129: 500 customers, 2,000 products, 1,000 reviews, 200,000 orders and 400,000 order lines. That is 603,500 rows, or 203,500 nodes and 601,000 relationships in Neo4j.
After every load — at 2,000 orders and again at 200,000 — the benchmark counts every PostgreSQL table and every Neo4j label and relationship type, and fails unless all equal the generator's counts. The recorded run verified 603,500 table rows, 203,500 nodes and 601,000 relationships at the large size.
make bench-check loads only the 2,000-order dataset, verifies those counts and compares the returned keys of all six shapes. The recorded gate is 6 of 6 identical.
Each read measurement is 25 timed runs after 3 discarded warm-ups. p50 and p95 use linear interpolation between ranks over all 25 runs: no minimum, no dropped outlier, and no run cancelled against the shared 15-second ceiling.
The Neo4j client posts one transactional HTTP request per run (POST /db/neo4j/tx/commit, JSON in and out), so a floor of roughly 3 ms sits under even a one-row answer.
The PostgreSQL side ran psycopg 3.3.9, pinned in uv.lock; the Neo4j client is Python's standard-library urllib over HTTP, so no third-party graph driver is involved.
Ingest
The same dataset loaded from an empty store, median of 3 full loads, on the same host, seed and sizes. Every engine gets 603,500 rows; Neo4j also creates 601,000 relationships.
| Engine | Raw load (s, median of 3) | Index build (s) |
|---|---|---|
| PostgreSQL | 8.781 | 0.247 |
| Neo4j | 26.786 | 0.025 |
PostgreSQL loaded 68,731 rows/s. Neo4j loaded 22,531 source rows/s and 22,437 relationships/s (203,500 nodes, 7,597 nodes/s) through 604 batched UNWIND requests of 1,000 rows.
Excluded from the timed regions but measured: PostgreSQL table DDL 10 ms and ANALYZE 139 ms; Neo4j graph wipe 6.60 s. The index figures are not the same work: Neo4j creates its constraints and indexes on the empty graph before loading, while PostgreSQL builds its indexes over the loaded rows. Both loaders drive rows from Python — COPY over a Unix socket for PostgreSQL, JSON over HTTP for Neo4j.
Ingest is where the setups differ most. cypher2sql adds no ingest cost to a database you already own: no migration, no duplication, no sync job, because the rows stay in PostgreSQL and the compiler only reads your schema declarations. Neo4j needs a full load from your system of record and ongoing synchronisation to keep two copies together; the sync is not measured here, and on a live system it is the larger bill.
Indexes on both sides
PostgreSQL indexes, from the run:
customer_country_c:CREATE INDEX customer_country_c ON public.customer USING btree (country COLLATE "C")customer_pkey:CREATE UNIQUE INDEX customer_pkey ON public.customer USING btree (id)order_customer_id:CREATE INDEX order_customer_id ON public."order" USING btree (customer_id)order_pkey:CREATE UNIQUE INDEX order_pkey ON public."order" USING btree (id)order_line_pkey:CREATE UNIQUE INDEX order_line_pkey ON public.order_line USING btree (order_id, product_sku)order_line_product_sku_c:CREATE INDEX order_line_product_sku_c ON public.order_line USING btree (product_sku COLLATE "C")product_category_price_c:CREATE INDEX product_category_price_c ON public.product USING btree (category COLLATE "C", price_cents)product_pkey:CREATE UNIQUE INDEX product_pkey ON public.product USING btree (sku)product_sku_c:CREATE INDEX product_sku_c ON public.product USING btree (sku COLLATE "C")review_pkey:CREATE UNIQUE INDEX review_pkey ON public.review USING btree (customer_id, product_sku)
Neo4j indexes and constraints, from the same run (the two built-in LOOKUP indexes have no label or properties, which the server reports as None(None)):
constraint customer_id_unique: UNIQUENESS NODE Customer(id)constraint order_id_unique: UNIQUENESS NODE Order(id)constraint product_sku_unique: UNIQUENESS NODE Product(sku)constraint review_key_unique: UNIQUENESS NODE Review(customer_id, product_sku)index customer_country: RANGE NODE Customer(country)index customer_id_unique: RANGE NODE Customer(id) constraint=customer_id_uniqueindex index_343aff4e: LOOKUP NODE None(None)index index_f7700477: LOOKUP RELATIONSHIP None(None)index order_id_unique: RANGE NODE Order(id) constraint=order_id_uniqueindex product_category_price: RANGE NODE Product(category, price)index product_sku_unique: RANGE NODE Product(sku) constraint=product_sku_uniqueindex review_key_unique: RANGE NODE Review(customer_id, product_sku) constraint=review_key_unique
The graph holds exactly the declared edges (PLACED, CONTAINS with a quantity property, WROTE), so the untyped walk explores the same relationships on both sides.
Build the plan fixture
The recorded comparison dataset is generated by scripts/bench/dataset.py (seed 20261129, large = 200,000 orders / 603,500 rows) and loaded by make bench; this smaller fixture backs only the PostgreSQL-only compile, planning and execution tables above. The fixture is the tutorial's shop at scale. The tests.* imports and the module command below need a repo checkout: the sdist ships the library and its docs, not the test suite. The command builds the fixture in a scratch database and drops and recreates its six tables, so point it somewhere disposable:
The builder is also importable, which is how the command, this page and the tests share it:
import psycopg
from tests.perf_fixture import SIZES, build_fixture
conn = psycopg.connect(autocommit=True)
build_fixture(conn)
print(", ".join(f"{table} {count:,}" for table, count in SIZES.items()))
customer 10,000, order 50,000, order_line 100,000, product 10,000, review 20,000, substitute 20,000
build_fixture creates the tutorial's tables through tests/_shop.py and tests/ddl.py, loads deterministic rows with generate_series, creates the indexes the shapes need, and runs ANALYZE, so the planner works from real statistics rather than guesses.
Reproduce
make bench # Docker and a network pull; writes the recorded run and the charts
make bench-check # the 2,000-order equality gate
make charts # redraw docs/assets/charts from the recorded run, no Docker
Keeping this page true
tests/test_explain.py builds this fixture through tests/perf_fixture.py and asserts the plan properties above on every run. docs/performance.md is executed by make check as well, so its Python blocks cannot drift.
Change the emitted SQL and rerun the command to refresh the numbers — a plan regression fails the test long before the table looks wrong. The Neo4j comparison is refreshed by make bench and its equality gate by make bench-check; both need Docker and a network pull, so make check never runs them.
The charts are a pure function of the recorded run in scripts/bench/bench.json: make charts redraws them without a re-measurement, and make bench regenerates both.
Back to the tutorial or the API reference.