pgrouting
pgrouting · pgRouting Extension
Overview
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| Requires | plpgsql postgis |
| See Also | postgis mobilitydb h3 pg_polyline q3c pointcloud qdgc pg_geohash pg_sphere pg_eviltransform |
Packages
| Linux / PG | PG19 | PG18 | PG17 | PG16 | PG15 |
|---|---|---|---|---|---|
| el8.x86_64 | N/A | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| el8.aarch64 | N/A | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| el9.x86_64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| el9.aarch64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| el10.x86_64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| el10.aarch64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| d12.x86_64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| d12.aarch64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| d13.x86_64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| d13.aarch64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| u22.x86_64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| u22.aarch64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| u24.x86_64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| u24.aarch64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| u26.x86_64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
| u26.aarch64 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 | PGDG 4.0.3 |
Source
github.com/pgRouting/pgrouting
Install
Make sure PGDG repo available:
Install this extension with pig:
Create this extension with:
Usage
Sources:
pgRouting 4.0.3 extends the PostGIS/PostgreSQL geospatial database to provide geospatial routing and other network analysis functionality. This release fixes duplicate nodes and edges in undirected pgr_aStar, pgr_bdAstar and pgr_bdDijkstra results when forward and reverse costs differ. Revalidate stored routes and downstream row counts when earlier results were materialized under that condition.
This library contains the following features:
- All Pairs Shortest Path (Floyd-Warshall, Johnson)
- A* algorithm (with bidirectional variant)
- Dijkstra algorithms (cost, cost matrix, driving distance, K shortest paths, via routing, nearest)
- Bidirectional Dijkstra
- Traveling Salesman Problem (TSP)
- Network flow (max flow, Boykov-Kolmogorov, Edmonds-Karp, push-relabel)
- Spanning trees (Kruskal, Prim with BFS/DFS/driving distance variants)
- Graph components (connected, strong, biconnected, articulation points, bridges)
- Turn Restriction Shortest Path (TRSP)
- WithPoints routing (arbitrary locations on edges)
- Graph contraction and utility functions
Getting Started
Enable the extension (requires PostGIS):
Graph Representation
pgRouting represents graphs using SQL queries that return edge data. The standard edge query format:
| Column | Type | Description |
|---|---|---|
id | ANY-INTEGER | Edge identifier |
source | ANY-INTEGER | Starting vertex identifier |
target | ANY-INTEGER | Ending vertex identifier |
cost | ANY-NUMERICAL | Weight (source to target); negative values exclude the edge |
reverse_cost | ANY-NUMERICAL | Weight (target to source); default -1 (non-existent) |
Simple Example Without Geometry
Create a graph and find the shortest path:
Function Families
Dijkstra - Shortest Path
The core routing function. Supports one-to-one, one-to-many, many-to-one, many-to-many, and combinations signatures.
Returns: (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
One to One:
One to Many:
Many to Many (undirected):
Combinations:
Dijkstra Cost
Returns only aggregate cost without path details:
Returns: (start_vid, end_vid, agg_cost)
Dijkstra Cost Matrix
Generate a cost matrix for a set of vertices:
Dijkstra Via
Route through an ordered sequence of vertices:
Dijkstra Near
Find the nearest vertex to a set of targets:
A* - Shortest Path
Uses the A* heuristic algorithm. Requires additional coordinate columns (x1, y1, x2, y2) in the edges query.
| Option | Type | Default | Description |
|---|---|---|---|
directed | BOOLEAN | true | Graph direction |
heuristic | INTEGER | 5 | Distance heuristic (0-5) |
factor | FLOAT | 1 | Units manipulation |
epsilon | FLOAT | 1 | Approximation factor |
Also available: pgr_aStarCost, pgr_aStarCostMatrix
Bidirectional Algorithms
Bidirectional variants search from both ends simultaneously:
pgr_bdDijkstra,pgr_bdDijkstraCost,pgr_bdDijkstraCostMatrixpgr_bdAstar,pgr_bdAstarCost,pgr_bdAstarCostMatrix
K Shortest Paths (Yen’s Algorithm)
Find the K shortest paths between two vertices:
Returns: (seq, path_id, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
Driving Distance
Find all vertices reachable within a given distance:
Returns: (seq, depth, start_vid, pred, node, edge, cost, agg_cost)
Traveling Salesman Problem
Matrix-based TSP:
Returns: (seq, node, cost, agg_cost)
Euclidean TSP (uses coordinates directly):
Network Flow
Compute maximum flow and related properties:
Flow edges SQL uses capacity and reverse_capacity instead of cost/reverse_cost.
Spanning Trees
Kruskal’s algorithm:
Prim’s algorithm:
Graph Components
Turn Restriction Shortest Path (TRSP)
Route with forbidden path restrictions:
Restrictions SQL format:
| Column | Type | Description |
|---|---|---|
path | ARRAY[ANY-INTEGER] | Sequence of forbidden edge IDs |
cost | ANY-NUMERICAL | Cost of the forbidden path |
WithPoints - Routing from Arbitrary Locations
Route between points located on edges (not just vertices):
Points SQL format:
| Column | Type | Default | Description |
|---|---|---|---|
pid | ANY-INTEGER | Point identifier | |
edge_id | ANY-INTEGER | Closest edge | |
fraction | ANY-NUMERICAL | Position on edge (0-1) | |
side | CHAR | b | r(right), l(left), b(both) |
Graph Contraction
Simplify graphs by contracting vertices:
Utility Functions
Working with Geometries
Building a Routing Topology
Extract vertices from spatial edges and build topology:
Setting Costs from Geometry Length
Getting Route Geometry
Combine routing results with edge geometries:
Performance Tips
Bound queries to the area of interest to reduce processed edges:
All Pairs Shortest Path
For computing distances between all pairs of vertices:
Returns: (start_vid, end_vid, agg_cost)
Upgrading to 4.0.3
Keep PostgreSQL, PostGIS and pgRouting libraries compatible. After replacing server files, use the available ALTER EXTENSION pgrouting UPDATE path and reconnect sessions holding the old library. Preserve topology, cost units and dependent objects; test representative directed and undirected routes before replacing materialized results. Shared preload is not required.