GanosBase Networking is a spatio-temporal engine extension of PostgreSQL (PolarDB for PostgreSQL (Compatible with Oracle)). It provides geospatial routing and network analysis functions based on a graph model — finding shortest, fastest, or cost-optimal paths across road networks and other geometric graphs. GanosBase Networking is fully compatible with pgRouting functions, so existing applications can migrate without modification.
Quick start
This section walks through a minimal example: install the extension, create a graph table, insert edges, build a topology, and query a path.
Prerequisites
Before you begin, ensure that you have:
A PolarDB for PostgreSQL (Compatible with Oracle) instance
Database superuser or a role with the
CREATE EXTENSIONprivilege
Install the extension
CREATE EXTENSION Ganos_Networking WITH SCHEMA public CASCADE;Install the extension into the public schema to avoid permission issues.
Create the edge table
CREATE TABLE edge_table (
id BIGSERIAL,
dir character varying,
source BIGINT,
target BIGINT,
cost FLOAT,
reverse_cost FLOAT,
capacity BIGINT,
reverse_capacity BIGINT,
category_id INTEGER,
reverse_category_id INTEGER,
x1 FLOAT,
y1 FLOAT,
x2 FLOAT,
y2 FLOAT,
the_geom geometry
);Insert edge data
INSERT INTO edge_table (
category_id, reverse_category_id,
cost, reverse_cost,
capacity, reverse_capacity,
x1, y1,
x2, y2) VALUES
(3, 1, 1, 1, 80, 130, 2, 0, 2, 1),
(3, 2, -1, 1, -1, 100, 2, 1, 3, 1),
(2, 1, -1, 1, -1, 130, 3, 1, 4, 1),
(2, 4, 1, 1, 100, 50, 2, 1, 2, 2),
(1, 4, 1, -1, 130, -1, 3, 1, 3, 2),
(4, 2, 1, 1, 50, 100, 0, 2, 1, 2),
(4, 1, 1, 1, 50, 130, 1, 2, 2, 2),
(2, 1, 1, 1, 100, 130, 2, 2, 3, 2),
(1, 3, 1, 1, 130, 80, 3, 2, 4, 2),
(1, 4, 1, 1, 130, 50, 2, 2, 2, 3),
(1, 2, 1, -1, 130, -1, 3, 2, 3, 3),
(2, 3, 1, -1, 100, -1, 2, 3, 3, 3),
(2, 4, 1, -1, 100, -1, 3, 3, 4, 3),
(3, 1, 1, 1, 80, 130, 2, 3, 2, 4),
(3, 4, 1, 1, 80, 50, 4, 2, 4, 3),
(3, 3, 1, 1, 80, 80, 4, 1, 4, 2),
(1, 2, 1, 1, 130, 100, 0.5, 3.5, 1.999999999999, 3.5),
(4, 1, 1, 1, 50, 130, 3.5, 2.3, 3.5, 4);Update geometry and direction
UPDATE edge_table
SET
the_geom = st_makeline(st_point(x1, y1), st_point(x2, y2)),
dir = CASE
WHEN (cost > 0 AND reverse_cost > 0) THEN 'B' -- both directions
WHEN (cost > 0 AND reverse_cost < 0) THEN 'FT' -- source to target only
WHEN (cost < 0 AND reverse_cost > 0) THEN 'TF' -- target to source only
ELSE ''
END;Build the topology
SELECT pgr_createTopology('edge_table', 0.001);This call populates the source and target columns with node identifiers based on the geometry.
Query a path
All three examples below use the same edge_table. The inner query selects only the columns each algorithm requires.
Dijkstra's algorithm (general edge SQL — requires id, source, target, cost, reverse_cost):
SELECT * FROM pgr_dijkstra(
'SELECT id, source, target, cost, reverse_cost FROM edge_table',
2, 3
);Expected output:
seq | path_seq | node | edge | cost | agg_cost
-----+----------+------+------+------+----------
1 | 1 | 2 | 4 | 1 | 0
2 | 2 | 5 | 8 | 1 | 1
3 | 3 | 6 | 9 | 1 | 2
4 | 4 | 9 | 16 | 1 | 3
5 | 5 | 4 | 3 | 1 | 4
6 | 6 | 3 | -1 | 0 | 5
(6 rows)A* algorithm (edge SQL with x and y values — requires id, source, target, cost, reverse_cost, x1, y1, x2, y2):
SELECT * FROM pgr_astar(
'SELECT id, source, target, cost, reverse_cost, x1, y1, x2, y2 FROM edge_table',
2, 12,
directed := false, heuristic := 2
);Expected output:
seq | path_seq | node | edge | cost | agg_cost
-----+----------+------+------+------+----------
1 | 1 | 2 | 2 | 1 | 0
2 | 2 | 3 | 3 | 1 | 1
3 | 3 | 4 | 16 | 1 | 2
4 | 4 | 9 | 15 | 1 | 3
5 | 5 | 12 | -1 | 0 | 4
(5 rows)Turn restriction shortest path (TRSP) algorithm (requires a restrictions query in addition to the edge query):
SELECT * FROM pgr_trsp(
'SELECT id::INTEGER, source::INTEGER, target::INTEGER, cost FROM edge_table',
2, 7, false, false,
'SELECT to_cost, target_id::int4,
from_edge || coalesce('','' || via_path, '''') AS via_path
FROM restrictions'
);Expected output:
seq | id1 | id2 | cost
-----+-----+-----+------
0 | 2 | 4 | 1
1 | 5 | 10 | 1
2 | 10 | 12 | 1
3 | 11 | 11 | 1
4 | 6 | 8 | 1
5 | 5 | 7 | 1
6 | 8 | 6 | 1
7 | 7 | -1 | 0
(8 rows)Remove the extension (optional)
DROP EXTENSION Ganos_Networking CASCADE;Key concepts
Graph model
A graph is an ordered pair G = (V, E), where:
Vis a set of vertices (nodes)E ⊆ {(u, v) | u, v ∈ V}is a set of edges
Graphs are classified into various types such as undirected graphs, undirected simple graphs, directed graphs, and directed simple graphs. GanosBase Networking supports graphs specified as directed or undirected for calculation. Graphs can be represented in two ways:
Graph with costs: each edge has a
costvalue (weight from source to target)Graph with costs and reverse costs: each edge also has a
reverse_costvalue (weight from target to source)
Algorithms
GanosBase Networking includes the following algorithms and analysis functions:
| Algorithm / function | Use case |
|---|---|
| Dijkstra's algorithm / bidirectional Dijkstra's algorithm | Shortest path on a general graph |
| A* algorithm / bidirectional A* algorithm | Shortest path with geographic coordinates (faster heuristic) |
| Johnson's algorithm | All-pairs shortest path (sparse graphs) |
| Floyd-Warshall algorithm | All-pairs shortest path (dense graphs) |
| K shortest path algorithm | Multiple alternative shortest paths |
| Turn restriction shortest path (TRSP) algorithm | Shortest path with turn restrictions |
| Traveling salesman algorithm | Optimal route visiting multiple destinations |
| Prim's algorithm | Minimum spanning tree |
| Kruskal's algorithm | Minimum spanning tree |
| Flow analysis | Maximum flow in a network |
| Topology operations | Build and query graph topology |
| Component operations | Find connected components |
| Contractions | Graph compression for faster routing |
Some functions also accept a cost or cost matrix for calculation.
Use cases
Route planning: Calculate the shortest or fastest path between points for logistics, delivery, and ride-hailing applications. GanosBase Networking also supports non-geographic graphs such as Internet node connections.
Geospatial analysis: Perform road network analysis including nearest neighbor search and service zone analysis to understand the distribution and relationships of geospatial data.
Traffic flow management: Analyze traffic volume data, predict congestion, and generate optimization recommendations for urban traffic management and intelligent transportation systems.
Function structure
GanosBase Networking follows the pgRouting function signature convention:
pgr_<name>(inner_query, parameters [, optional_parameters])| Parameter | Required | Description |
|---|---|---|
inner_query | Yes | An SQL query that returns the graph data the function needs. The exact columns required depend on the algorithm. |
parameters | Yes | Required arguments for the function. |
optional_parameters | No | Optional arguments with default values when omitted. |
Function overloads
Most functions support multiple overloads based on the number of start and end vertices.
| Overload | Start vertices | End vertices |
|---|---|---|
| One-to-one | 1 | 1 |
| One-to-many | 1 | Multiple |
| Many-to-one | Multiple | 1 |
| Many-to-many | Multiple | Multiple |
| Combinations | Multiple pairs | Multiple pairs |
Inner query formats
The inner query passes graph data to the function. The required columns depend on the algorithm.
General edge SQL
Used by: Dijkstra's algorithm, bidirectional Dijkstra's algorithm, pgr_withPoints, flow analysis, component operations, and other general routing functions.
| Column | Type | Default | Description |
|---|---|---|---|
id | integer | Required | Unique identifier of the edge. |
source | integer | Required | Starting node of the edge. |
target | integer | Required | Ending node of the edge. |
cost | numeric | Required | Weight of the edge from source to target. |
reverse_cost | numeric | -1 | Weight of the edge from target to source. A negative value excludes the edge (target → source) from the graph. |
General edge SQL without ID
Used by: Johnson's algorithm, Floyd-Warshall algorithm, and other all-pairs algorithms.
| Column | Type | Default | Description |
|---|---|---|---|
source | integer | Required | Starting node of the edge. |
target | integer | Required | Ending node of the edge. |
cost | numeric | Required | Weight of the edge from source to target. |
reverse_cost | numeric | -1 | Weight of the edge from target to source. A negative value excludes the edge (target → source) from the graph. |
General edge SQL with x and y values
Used by: A* algorithm, bidirectional A* algorithm.
| Column | Type | Default | Description |
|---|---|---|---|
source | integer | Required | Starting node of the edge. |
target | integer | Required | Ending node of the edge. |
cost | numeric | Required | Weight of the edge from source to target. A negative value excludes the edge (source → target) from the graph. |
reverse_cost | numeric | -1 | Weight of the edge from target to source. A negative value excludes the edge (target → source) from the graph. |
x1 | numeric | Required | x coordinate of the starting node. |
y1 | numeric | Required | y coordinate of the starting node. |
x2 | numeric | Required | x coordinate of the ending node. |
y2 | numeric | Required | y coordinate of the ending node. |
Combinations SQL
Specifies custom start–end vertex pairs for combinations overloads.
Restrictions SQL
Used by: Turn restriction shortest path (TRSP) algorithm.
| Column | Type | Default | Description |
|---|---|---|---|
path | integer array | Required | A sequence of edge IDs that forms a restricted (forbidden) path. |
cost | numeric | Required | The cost of taking the restricted path. |
Points SQL
Used by functions that work with intermediate points on edges (for example, pgr_withPoints).
| Column | Type | Default | Description |
|---|---|---|---|
pid | integer | Auto-assigned | Unique identifier of the point. |
edge_id | integer | Required | Unique identifier of the nearest edge to this point. |
fraction | numeric | Required | Relative position of the point along the edge. Valid range: 0 to 1. |
side | char | b | Side of the edge the point is on. Valid values: b (both sides), r (right), l (left). |
Point identifiers use negative numbers internally. When start_vid or end_vid columns in the result contain negative values, they refer to points, not vertices.
Result columns
Path result columns
Returned by single-path functions: pgr_dijkstra, pgr_bdDijkstra, pgr_aStar, pgr_bdAstar, pgr_dijkstraNear, and similar functions.
| Column | Type | Description |
|---|---|---|
seq | integer | Sequential row number, starting from 1. |
path_seq | integer | Position within the path, starting from 1. |
[start_vid] | bigint | Starting vertex identifier. Returned only when the query has multiple starting vertices. |
[end_vid] | bigint | Ending vertex identifier. Returned only when the query has multiple ending vertices. |
node | bigint | Identifier of a node in the path from start_vid to end_vid. |
edge | bigint | Identifier of the edge from the current node to the next. -1 indicates the last node in the path. |
cost | float | Cost to traverse the edge from the current node to the next. |
agg_cost | float | Cumulative cost from start_vid to the current node. |
The pgr_dijkstraNear function always returns start_vid and end_vid columns, regardless of the number of vertices in the query.
The pgr_withPoints function returns the same columns, with the following behavior for point identifiers:
In
start_vid: positive values are starting vertices; negative values are starting points.In
end_vid: positive values are ending vertices; negative values are ending points.In
node: positive values are vertices; negative values are points.
Multiple-path result columns (selective)
Returned by functions that find multiple alternative paths (selective): pgr_KSP, pgr_withPointsKSP, pgr_edgeDisjointPaths, and similar functions.
| Column | Type | Description |
|---|---|---|
seq | integer | Sequential row number, starting from 1. |
path_id | integer | Path identifier. The first path from start_vid to end_vid is 1. |
path_seq | integer | Position within the path, starting from 1. |
[start_vid] | bigint | Starting vertex identifier. Returned only when the query has multiple starting vertices. |
[end_vid] | bigint | Ending vertex identifier. Returned only when the query has multiple ending vertices. |
node | bigint | Identifier of a node in the path from start_vid to end_vid. |
edge | bigint | Identifier of the edge from the current node to the next. -1 indicates the last node in the path. |
cost | float | Cost to traverse the edge from the current node to the next. |
agg_cost | float | Cumulative cost from start_vid to the current node. |
Multiple-path result columns (non-selective)
Returned by functions that are not selective for multiple paths.
| Column | Type | Description |
|---|---|---|
seq | integer | Sequential row number, starting from 1. |
path_id | integer | Path identifier. The first path from start_vid to end_vid is 1. |
path_seq | integer | Position within the path, starting from 1. |
start_vid | bigint | Unique identifier of the starting vertex. |
end_vid | bigint | Unique identifier of the ending vertex. |
node | bigint | Identifier of a node in the path from start_vid to end_vid. |
edge | bigint | Identifier of the edge from the current node to the next. -1 indicates the last node in the path. |
cost | float | Cost to traverse the edge from the current node to the next. |
agg_cost | float | Cumulative cost from start_vid to the current node. |
Cost function result columns
Returned by cost and cost matrix functions.
| Column | Type | Description |
|---|---|---|
start_vid | bigint | Unique identifier of the starting vertex. |
end_vid | bigint | Unique identifier of the ending vertex. |
agg_cost | float | Total cost of the path from start_vid to end_vid. |
What's next
For the complete SQL reference for all supported functions, see the pgRouting documentation.