Skip to content

Your first graph

After installing it and running CREATE EXTENSION pg_warren;:

CREATE TABLE people (id bigint PRIMARY KEY, name text NOT NULL);
CREATE TABLE follows (src bigint NOT NULL, dst bigint NOT NULL);
INSERT INTO people VALUES (1, 'ada'), (2, 'bob'), (3, 'cy'), (4, 'dee'), (5, 'eve');
INSERT INTO follows VALUES (1, 2), (2, 3), (3, 1), (3, 4), (4, 5);
SELECT warren.create_graph('social', 'SELECT id FROM people', 'SELECT src, dst FROM follows');
SELECT warren.refresh('social'); -- builds version 1
SELECT p.name, b.depth -- who ada reaches in 3 hops
FROM warren.bfs('social', ARRAY[1::bigint], 3) b
JOIN people p ON p.id = b.node_id ORDER BY b.depth, p.name;
-- ada 0 | bob 1 | cy 2 | dee 3
SELECT warren.pagerank('social', 'pr'); -- writes vector 'pr'
SELECT p.name, round(r.value::numeric, 3) AS rank
FROM warren.vector_topk('social', 'pr', 3, NULL::bigint) r
JOIN people p ON p.id = r.node_id ORDER BY rank DESC, p.name;
-- cy 0.243 | bob 0.209 | eve 0.209

The graph’s ids are bigint, so id arguments are cast (1::bigint), and functions with no id argument take a type witness (NULL::bigint) for their result type. A traversal reads the version, not the tables: rows inserted after refresh appear after the next refresh (docs/semantics.md).