Skip to content

pg_petgraph

In-memory graph algorithms over paired SQL edge arrays

pg_petgraph · In-memory graph algorithms over paired SQL edge arrays

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102466pg_petgraphpg_petgraph0.3.0TYPEMITRust

PostgreSQL · 16, 17, 18

Usage

Sources:

pg_petgraph 0.3.0 provides graph algorithms as ordinary SQL functions over paired edge arrays. The exact Cargo features and CI support PostgreSQL 16, 17 and 18; the README’s older badge is not the current compatibility contract.

Calculate from Edge Arrays

A superuser creates the relocatable Rust/pgrx extension. No preload, restart or other extension dependency is required.

CREATE EXTENSION pg_petgraph;
SELECT * FROM pagerank(
  ARRAY[1,2,3,2,4]::bigint[], ARRAY[2,3,1,4,1]::bigint[]
);
SELECT * FROM scc(ARRAY[1,2,3]::bigint[], ARRAY[2,3,1]::bigint[]);
SELECT * FROM dijkstra(ARRAY[1,1,2]::bigint[], ARRAY[2,3,3]::bigint[], 1);
SELECT is_cyclic(ARRAY[1,2]::bigint[], ARRAY[2,1]::bigint[]);

Every edge pairs the values at the same position in sources bigint[] and targets bigint[]. Output retains the original node identifiers. To derive arrays from a table, use the same deterministic ordering in both aggregates.

Function Surface

pagerank returns node scores, with defaults damping = 0.85 and max_iter = 100. scc finds strongly connected components; connected_components finds weakly connected components. betweenness, closeness and eigenvector calculate centrality, and louvain returns community identifiers. dijkstra returns unit-edge distances from a supplied node. toposort returns an ordering or raises an error on a cycle; is_cyclic returns a boolean.

Input and Resource Limits

Graphs are directed, unweighted and rebuilt in memory on every call. Array lengths must match. Isolated nodes cannot be represented without an edge, and a missing start node makes dijkstra fail. In the reviewed implementation, null array elements become node 0 rather than being rejected: filter null endpoints before aggregation. Bound input size and iteration settings yourself; there is no persistent graph store, incremental index or disk-backed traversal.

Table reader