Postgres as a Graph Database: (Ab)using pgRouting
The pgRouting extension, usually paired with PostGIS for geospatial shortest-path queries, can serve as a lightweight graph layer in Postgres instead of Apache AGE or Neo4j. Three non-GIS examples model graphs as SQL node and edge tables: pgr_dijkstra computes a project's path through a task-dependency DAG, pgr_astar routes data across servers weighted by latency, and Dijkstra paths over a video-relationship graph produce ranked recommendations via ordinary SQL aggregation. Each example includes the table schemas and queries.
Related posts
- pg_graphql: A GraphQL extension for PostgreSQL
Supabase mediumAdeptpg_graphql is an open-source PostgreSQL extension, written in C and SQL, that reflects a GraphQL schema from existing tables and resolves queries entirely inside the database. Supabase built it because its 1 GB free-tier VMs had no memory left for a separate GraphQL process like Graphile or Hasura. Each GraphQL request is transpiled into a single SQL statement that aggregates results as JSON. This avoids N+1 queries, respects row level security, and gives multi-operation requests transactional semantics. Pagination follows the Relay connection spec using keyset pagination, the API is one SQL function (gql.resolve) exposed over HTTP via PostgREST, and a benchmark shows about 2,205 requests/second on a 4-core machine.
- pg_graphql v1.0
Supabase mediumAdeptpg_graphql, a PostgreSQL extension that compiles GraphQL requests into a single SQL query, reaches 1.0 with a SemVer backwards-compatibility guarantee. Running inside Postgres instead of as a separate server keeps its memory footprint negligible on 1 GB Free Plan instances. That design also gives it Postgres roles and RLS for security, an always-in-sync schema with no N+1 queries, and ACID rollback across multi-mutation requests. The original pure-SQL transpiler hit limits near 4,000 lines because SQL functions pass by value and the planner stops inlining deeply nested calls, so it was rewritten in Rust with pgx. After the rewrite, overhead is under 300µs per request, about 1,060 queries/sec per connection from SQL, and about 645 req/s through the full HTTP and auth stack on Free Plan hardware.
- How pg_graphql works
Supabase mediumAdeptpg_graphql is a Rust-based Postgres extension that reflects the SQL schema as a GraphQL schema and serves queries through a `graphql.resolve` SQL function, with no separate server. A request is tokenized and parsed into an AST with the graphql-parser crate, then validated against the GraphQL spec using a reflected schema built from system catalogs. That schema is memoized and reloaded only when the search path, role or schema version changes. Validated queries become builder objects that emit SQL, with quote_ident/quote_literal guarding against injection, and the SQL returns jsonb that is passed back to the client as JSON.
- 20x faster than pgvector: introducing pg_embedding extension for vector search in Postgres and LangChain
Neon mediumAdept
pg_embedding brought HNSW graph-based approximate nearest neighbor indexing to Postgres. On the GIST-960 dataset (1M 960-dim vectors, k=100, 4 vCPU/16 GB), it ran 5–30x faster than pgvector's IVFFlat at equal recall. IVFFlat needs more probes to raise recall and drifts toward a sequential scan, while HNSW's layered graph gives roughly logarithmic search. HNSW trades higher memory use, slower index builds (driven by efConstruction) and L2-only distance for that speed. Neon has since deprecated pg_embedding in favor of pgvector, which now supports HNSW natively.
- postgres.new: In-browser Postgres with an AI interface
Supabase mediumAdeptdatabase.build (formerly postgres.new) runs unlimited Postgres databases entirely in the browser via PGlite, a WASM build of Postgres, paired with GPT-4o that has full autonomy over the disposable database. The LLM imports CSVs, generates Chart.js charts, builds ER diagrams from real DDL, and does in-browser semantic search using pgvector and Transformers.js. SQL errors are fed back to the model so it can self-correct, and embeddings are stored in a meta table and referenced by ID to avoid sending large vectors to the model. PGlite relies on Postgres's single-user mode plus wire protocol support, a /dev/blob virtual device for COPY, and IndexedDB/OPFS persistence. A new TypeScript library, pg-gateway, handles the startup/auth handshake so standard Postgres clients can connect to PGlite deployments.