Skip to content

Postgres

Postgres 14 or later (tested on 16), as drivine4j states, keeps a graph in two tables, as Oracle does, read with joins and written through a function the store keeps. No extension is needed but pgvector, for vector search, which Drivine makes where the server has it. Models, the query DSL and the object manager are the same as on the graph engines; the persistence manager runs plain SQL, with cursors and transactions, and Cypher compiled to SQL: see Cypher on a relational database.

DATABASE_TYPE=POSTGRES
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=app
DATABASE_USER=app
DATABASE_PASSWORD=…
DATABASE_DEFAULT_GRAPH_PATH=drivine # names the graph's tables; this is the default

The graph’s name names tables, so it is letters, digits and underscores, begins with a letter, and is at most 49 characters; Postgres folds it to lower case. The tables are made on the first connection when they aren’t there, as on Oracle, whatever is sent first: Cypher or SQL through the persistence manager, a statement of the object manager, or a schema change. A user who may read the tables but not make them works once they are made. Postgres is a graph store like the others, so a schema catalog that names no database applies to it too, and makes its indexes at startup.

How a graph is stored. drivine4j’s store, byte for byte, so the JVM, Node and Python read and write one graph: <graph>_node (nid, labels, props, stamp, words) and <graph>_edge (eid, src, dst, type, props), with labels and props as jsonb and a GIN index over the labels, so a node carries any number of them; <graph>_schema, the indexes and constraints Drivine made; and <graph>_write, the function a save’s steps are carried out by, as one statement that is applied whole or not at all.

  • A statement you write yourself is SQL, unless written as Cypher (see Cypher on Postgres, Oracle and SQLite). Parameters are named $name, as in Cypher, and a statement is bound only the ones it names outside quoted text and comments: the $t of '$t' or $q$ $t $q$ is none. A row comes back as a dict of its columns, a jsonb column as Python values.
  • A raw where, for a load or a delete, is SQL over the root’s row n: its props, labels and stamp, say (n.props ->> 'name') = $name, read over the row a statement calls it by, n_ (the row the shared module’s fragments read). A property of the root may be written as Cypher writes it, n.name = $name (or n.`a name` ), and is read as that property, as text, and one whose stored name begins with $, n.`$score` , by the name the store holds it by; a property compared with true or false, or with a parameter bound to True or False, is compared with the text a JSON boolean is read as. To compare one with a number, read it as a number in SQL: CAST(n.`$score` AS NUMERIC) > $least. A date in a raw where is compared with the property’s JSON text, the tag a save holds it as, so (n.props ->> 'at') = $when finds nothing: compare dates in the query DSL.
  • A number is compared as a number only where one is stored: '7' is not 7, as on the graph engines.
  • Behaviour change: text is compared and ordered by its code points, as Neo4j orders it, in the query DSL’s conditions (gt, lt and the rest on text), orders, seeks and grouped reads, as in compiled Cypher (Cypher on Postgres, Oracle and SQLite): their SQL is the shared module’s (drivine-cypher-spec#235), in the C collation (COLLATE "C"), where the DSL used the database’s collation before. So a, B, b sorts as B, a, b, as on SQLite and Neo4j, where it sorted a, b, B.
  • A save checks the stamp, as on the graph engines. A save turned away by a serialization failure (40001) or a deadlock (40P01) is run again.
  • Index the id of each class you save: a range index or a uniqueness constraint is a partial index over the label’s nodes, which finds a node by its id.
  • matches takes a Postgres regular expression, matched against the whole value.
  • A vector index is a pgvector HNSW index over the embeddings of the label’s nodes of its size; load_nearest is approximate through it, and exact with no index made. A full-text index is a trigram index over the text a search reads, which narrows the search; load_matching scores the same with it and without it, and reads words, a "phrase", +must, -not and a prefix*. A word is a run of letters as Unicode’s Alphabetic holds them (with the vowel signs of Indic scripts), decimal digits and underscores, as the text’s [[:alnum:]_] reads it: m²x is the phrase m x. In a phrase a * is text, as on Neo4j: "of sou*" is the phrase of sou. It scores by BM25, as Lucene and so Neo4j’s full-text index do (k1 1.2, b 0.75): a term weighs as it is rare among the label’s nodes, and a text’s counts against its length, in characters, beside the label’s average. A term written again at once, kiwi kiwi kiwi, is counted each time. A prefix* adds the same to every node that holds it, however often and however rare, as Lucene’s prefix query does: in kiw* melon, a node with kiwi and no melon scores about 0.54 of one with both, as on Neo4j. Oracle without Oracle Text scores the same way. A dotted name, as org.example.models, is words here, split at each dot, as it is on Oracle and SQLite: example models finds it, and the name itself is the phrase of its words. Neo4j’s default analyzer keeps such a name one token, which the whole name finds and example models doesn’t. drivine4j’s FullTextIndexSpec picks another analyzer there; drivine-python doesn’t take one yet.
  • An index or a uniqueness constraint, a vector index’s HNSW index among them, is built with no parallel workers (max_parallel_maintenance_workers 0), and the session’s own setting is put back after, whether the build passed or failed. A session that builds an index in parallel keeps a shared memory segment in /dev/shm for as long as it lives, and the pool keeps it alive: in a container’s 64 MB, Docker’s default, enough of them leave too little for a later HNSW build, which asks for nearly all of maintenance_work_mem at once and fails with could not resize shared memory segment ... No space left on device. A large build takes longer for it. drivine4j closes the session after the build instead, and keeps its parallel workers; psycopg_pool would log a warning for each connection given back closed.