Python binding for Minigraf, an embedded graph database. Open a local database file without a separate server or config file. Use bi-temporal Datalog queries to ask when a fact applied and when the database recorded it.
pip install minigrafSupports Python 3.9+ on Linux (x86_64, aarch64), macOS (universal2), and Windows (x86_64).
import json
from minigraf import MiniGrafDb
# In-memory database
db = MiniGrafDb.open_in_memory()
db.execute('(transact [[:alice :name "Alice"] [:alice :age 30]])')
result = json.loads(db.execute("(query [:find ?n ?a :where [?e :name ?n] [?e :age ?a]])"))
print(result["results"]) # [["Alice", 30]]
# File-backed (persisted to disk)
db = MiniGrafDb.open("path/to/mydb.graph")
db.execute('(transact [[:bob :name "Bob"]])')
db.checkpoint()minigraf.open() takes the Rust OpenOptions as keyword arguments. An option left out
keeps its default.
import minigraf
# Read-only: shared lock, nothing written. Any number of read-only handles can share
# the file; writes raise MiniGrafError with API-014, a missing file STG-042.
src = minigraf.open("app.graph", read_only=True, page_cache_size=4096)
# Every option: read_only, page_cache_size, allow_unlocked, wal_checkpoint_threshold,
# max_derived_facts, max_results, synchronous (minigraf.SyncMode.FULL / NORMAL),
# query_memory_limit, allow_overrides, transaction_chunk_facts, max_iterations.
db = minigraf.MiniGrafDb.open_with_options("app.graph", minigraf.OpenOptions(max_derived_facts=10_000))query_memory_limit is the most bytes one query may hold, counted per entry by the
operators that keep state (join build sides, negation and or tables, distinct sets,
aggregate groups, window rows, rule-derived facts). If a query exceeds the limit, it raises
MiniGrafError with QRY-012 and releases its query memory. There is no limit by default;
2**63 - 1 also disables it. Rows a cursor returns do not count against the limit. Use db.query() to open a cursor for a large result.
wal_checkpoint_threshold=minigraf.WAL_CHECKPOINT_NEVER turns off automatic checkpoints
and the checkpoint when the handle closes, for applications that call checkpoint() on
their own schedule.
Query text may override some settings with keywords such as :max-derived-facts 1000.
allow_overrides lists the minigraf.Overridable keywords it may use
(MAX_DERIVED_FACTS, MAX_RESULTS, QUERY_MEMORY_LIMIT, MAX_ITERATIONS). Left out, the
defaults apply: minigraf.OVERRIDABLE_DEFAULTS, read from the core library (today
MAX_DERIVED_FACTS, MAX_RESULTS and MAX_ITERATIONS). A list replaces the defaults, it
does not add to them, and [] refuses every keyword: a query that uses one raises
MiniGrafError.Api with [API-025] from execute(), query() and prepare().
from minigraf import Overridable
# Query text may set only :query-memory-limit.
db = minigraf.open("app.graph", allow_overrides=[Overridable.QUERY_MEMORY_LIMIT])
# The defaults plus :query-memory-limit.
db = minigraf.open(
"app.graph",
allow_overrides=[*minigraf.OVERRIDABLE_DEFAULTS, Overridable.QUERY_MEMORY_LIMIT],
)max_iterations is the most rounds each stratum of a rule query may run (default
1,000,000; more raise MiniGrafError with [INT-020]). transaction_chunk_facts is how
many facts one transaction on a file database stages before it is written in chunks
(default 65,536, clamped to 1..=1,000,000). A negative count raises [API-017].
db.query() returns a cursor whose answer is fixed when it opens. Iterating gives
rows, decoded as in execute(); next_batch(n) gives a JSON string of up to n rows,
or None at the end.
with db.query("(query [:find ?n :where [?e :name ?n]])") as cursor:
print(cursor.vars()) # ["?n"]
for row in cursor:
print(row)db.prepare() parses a query once; execute() and query() run it with its $slots
bound, from keyword arguments or a dict (names without the $). Each run reads the
database as it is then. A prepared query keeps its database open until both are freed.
pq = db.prepare("(query [:find ?n :where [?e :age $age] [?e :name ?n]])")
print(json.loads(pq.execute(age=30))["results"])
print(json.loads(pq.execute({"age": 40}))["results"])
with pq.query(age=30) as cursor:
for row in cursor:
print(row)bool, int, float, str and None bind as values, and a uuid.UUID as an entity
(it also fits a value slot as a ref). For the other kinds use minigraf.Value.KEYWORD(":k"),
minigraf.Value.REF(uuid_str), minigraf.Entity(uuid), minigraf.TxCount(n) or
minigraf.Timestamp(ms) for :as-of, and minigraf.ANY_VALID_TIME for :valid-at. Any
other Python type raises TypeError. A missing slot raises MiniGrafError.Api with
[API-022], a slot in an attribute position [API-023], and a value of the wrong kind
for its slot [API-024].
A set or frozenset binds a set value, for in? and the other set predicates or a
set-valued attribute: pq.execute(ids={1, 2, 3}). Its elements are bool, int, float,
str, None or uuid.UUID (a ref). minigraf.Value is not hashable, so a set with keyword
elements is written minigraf.Value.SET([minigraf.Value.KEYWORD(":role/admin")]). A set
inside a set raises MiniGrafError.Api with [API-021]; a list is not a set. Set values
come back as JSON arrays from execute() and as Value.SET([...]) in fact records.
with_max_derived_facts(n), with_query_memory_limit(bytes) and with_max_iterations(n)
each return a new prepared query with that limit; the one they are called on is
unchanged. allow_overrides only restricts keywords in the query text, not calls
to these methods. A permitted override in the query still takes precedence. 2**63 - 1 means no limit and a
negative value raises [API-017]. A derived query keeps its database open, as any
prepared query does.
reach = db.prepare("(query [:find ?x ?y :where (reach ?x ?y)])")
small = reach.with_max_derived_facts(10_000).with_query_memory_limit(16 << 20)
small.execute() # [INT-020] or [QRY-012] past a limit; reach.execute() is unchangedA predicate subclasses minigraf.Predicate and is called in Datalog as [(name ?v)].
An aggregate subclasses minigraf.Aggregate; its accumulator() returns a new
minigraf.Accumulator (step(value), finish(count)) for each group or window
partition, and it is used as (name ?v) in :find or with :over. Values arrive as
minigraf.Value (Value.INT64, Value.TEXT, …, with the value in .value), and
finish returns one.
from minigraf import Accumulator, Aggregate, Predicate, UdfError, Value
class Adult(Predicate):
def test(self, value):
if not isinstance(value, Value.INT64):
raise UdfError.Failed("age is not an integer")
return value.value >= 18
class Product(Aggregate):
def accumulator(self):
return ProductAcc()
class ProductAcc(Accumulator):
def __init__(self):
self.product = 1
def step(self, value):
self.product *= value.value
def finish(self, count): # count: values stepped
return Value.INT64(self.product)
db.register_predicate("adult?", Adult())
db.register_aggregate("product", Product())
db.execute("(query [:find ?n :where [?e :age ?a] [(adult? ?a)] [?e :name ?n]])")
db.execute("(query [:find ?g (product ?a) :where [?e :group ?g] [?e :age ?a]])")
db.execute("(query [:find ?a (product ?a :over (:order-by ?a)) :where [?e :age ?a]])")- Failures. A callback that raises stops the query with
QRY-014, raised asMiniGrafError.Query; it is never read asFalseor a missing value. RaiseUdfError.Failed(msg)to choose the message. Any other exception also fails the call, with itsrepr(such asValueError('bad')) as the message. AValue.REFfromfinishwhose value is not a UUID is a failing call too. - Lifecycle. The database keeps a strong reference to each registered object until
the database and every cursor and prepared query opened from it are freed. There is
no unregister. Predicates and aggregates have separate names, and registering a name
twice in either is an error (
INT-040). A query sees the functions registered when it opened. - Threading. A callback runs on the thread that calls
execute(of the database or a prepared query) or a cursor'snext_batch, including a prepared query's cursor. The binding releases the GIL while the core runs and the callback re-acquires it, so queries on different threads may call the same object at once; it must be thread-safe. One accumulator is used by one query on one thread at a time. - Cost. Each call crosses the FFI once: one Python call per value.
- Re-entry. A callback may query the same database, open a cursor or register another
function. Within a write transaction, the restrictions below still apply.
Calls that violate them return
INT-001.
Stage changes with transact or retract. Queries in the same transaction
see these changes. Commit to apply them together or roll back to discard them (minigraf#824). Available in the pinned, unreleased v3 core (commit
38b10383), which also provides incoming references.
import json
import minigraf
db = minigraf.open("bank.graph")
db.execute("(transact [[:alice :balance 100] [:bob :balance 50]])")
# The with block rolls back on exit unless the transaction was committed,
# also when an exception leaves it.
with db.begin_write() as tx:
tx.execute("(transact [[:alice :balance 70] [:bob :balance 80]])")
tx.execute("(retract [[:alice :balance 100] [:bob :balance 50]])")
rows = json.loads(tx.execute("(query [:find ?b :where [:alice :balance ?b]])"))["results"]
if rows[0][0] >= 0:
tx.commit()db.begin_write(synchronous=None, chunk_facts=None): an option set toNoneuses the database setting.synchronous(SyncMode.FULLorSyncMode.NORMAL) sets the WAL sync mode for this transaction.chunk_factssets how many facts a file-backed transaction stages before writing in chunks (a negative count is API-017). Read-only databases raise API-014.tx.execute(datalog)returns whatdb.executereturns; a staged write returns{"ok": true}. A statement that fails stages nothing and leaves the transaction open. There are no cursors or prepared queries inside a transaction.tx.commit()applies everything as one transaction; a commit that fails rolls back.tx.rollback()discards it. Either way the transaction is finished:execute,incoming_refsandcommitthen raise API-018, androllbackdoes nothing.- Commit is always explicit: leaving the
withblock, or the transaction being garbage collected, rolls back. Python may collect it late, so end every transaction withcommit(),rollback()or awithblock. - An open transaction holds the database's write lock. On the thread that began it,
database calls (
execute,query,prepare,fact_log,checkpoint, anotherbegin_write, prepared queries) raise INT-001, including when called from a user function. A user function also gets INT-001 if it callsexecute,incoming_refs,commit, orrollbackon its own transaction. Other threads wait to write or begin a transaction; their queries see only committed data. Use the transaction on the thread that opened it. A different thread that uses it and then writes throughdbwill wait for that transaction to end. - Rules registered inside a transaction stay registered after a rollback.
- The transaction keeps its database open until it is freed.
tx.incoming_refs(target: str, max_links: int, max_records: int) -> IncomingRefs
works on begin_write()'s transaction both inside and outside with. The public
minigraf package exports IncomingRef, IncomingRefBudget and IncomingRefs.
from minigraf import IncomingRefs, IncomingRefBudget
with db.begin_write() as tx:
result = tx.incoming_refs("82958a33-8334-5ef8-ae38-de39ce3fab69", 100, 10_000)
if isinstance(result, IncomingRefs.COMPLETE):
links = [(link.source, link.attribute) for link in result.links]
examined = result.records_examined
# Plan any deletes using the complete set, then stage and commit them.
elif isinstance(result, IncomingRefs.EXHAUSTED):
budget = result.budget # IncomingRefBudget.LINKS or .RECORDS
examined = result.records_examined
# Retry with larger budgets or abandon the delete.The lookup sees committed data and changes already staged in this transaction. It
does not stage any changes. A complete result contains IncomingRef records with canonical
lowercase UUID source strings and attribute strings; order is unspecified.
Complete-empty is distinct from exhaustion, which has no partial link list.
Run the lookup before staging deletes. Proceed only if it returns IncomingRefs.COMPLETE. If it returns IncomingRefs.EXHAUSTED, retry with higher budgets or skip the deletes. Exhaustion does not undo earlier
staged writes; an explicit commit still commits them.
- Both budgets are required and each sets an exact cap. No value disables a cap. The accepted
range is
0..=min(i64::MAX, usize::MAX):2^63-1on 64-bit targets and2^32-1on 32-bit targets (including Android's 32-bit ABIs). Zero is a real limit;0, 0completes empty only when no record needs inspection. Large budgets do not allocate proportional memory up front. - The links budget caps distinct
(source, attribute)pairs. The records budget caps fact versions examined across all layers, including superseded versions, retractions and lookahead. The returned record count never exceeds that budget; checkpointing can change the count without changing the links. - Only stored scalar Ref values count. Keywords (even an alias of the target), UUID-looking Strings, refs inside sets and rule-derived facts are excluded. Valid time is ignored: future and expired assertions count. The newest assertion or retraction decides whether a link is live.
- Invalid UUIDs and negative or unrepresentable budgets give
API-017. A finished transaction givesAPI-018before argument validation. A callback reentering its own transaction givesINT-001before locking or validation. Storage and decoding errors remain errors. Complete, exhausted andAPI-017leave the transaction open for further statements, retry, commit or rollback.
Python integers outside the signed 64-bit range also raise MiniGrafError with
[API-017], preserving finished/reentrant precedence. Wrong Python types may
raise TypeError.
db.fact_log(filter) streams every fact version (assertions and retractions, with
tx_count, tx_id and valid-time bounds) as FactRecords. LogWriter builds a new
file from those records and keeps their valid-time bounds. Use
it to load the data during an offline migration.
from minigraf import FactFilter, LogWriter, OpenOptions
src = minigraf.open("old.graph", read_only=True)
with src.fact_log(FactFilter()) as log, LogWriter.create("new.graph", OpenOptions()) as out:
batch = []
for rec in log:
if not rec.attribute.startswith(":secret/"):
batch.append(rec)
if len(batch) == 10_000:
out.append_batch(batch)
batch = []
out.append_batch(batch)
out.advance_tx_count(src.current_tx_count())
# Leaving the `with` block finishes the file; an exception abandons it and leaves no file.FactFilter takes attributes, attribute_prefixes, entities (UUID strings),
tx_from/tx_to (inclusive), order (FactOrder.TX or FactOrder.STORAGE) and
window. A record's value is a minigraf.Value (TEXT, INT64, FLOAT64, BOOL,
REF, KEYWORD, NULL), so a ref and a string stay different. A valid_to of
minigraf.VALID_TIME_FOREVER means forever.
Every error is a MiniGrafError whose msg starts with its code, such as
[API-015]. Its class follows the code's category: MiniGrafError.Parse, .Query,
.Storage, .Api (a bad argument, a closed object, a bind mistake) or .Other. See the error reference.
append_batch stops at the first rejected record and ends the message with
(batch index N); the records before it stay appended.
bench/envelope.py runs the core benchmark queries (minigraf#394) through
Python's db.query(...) and next_batch(1000). It reports median and p99 wall
time and peak RSS alongside the Rust results.
Results are published in minigraf's docs/BENCHMARKS.md.
# In minigraf: build the fixtures and the report.
cargo run --profile envelope --example perf_envelope -- --scales 1m --json envelope.json
# Here, with the binding installed (maturin develop --release):
python bench/envelope.py ../minigraf/target/perf-envelope/fixture-v1-1m.graph \
--report ../minigraf/envelope.json --scale 1mRequires Rust stable toolchain and maturin.
pip install maturin
maturin developThe core cascade sends core-release with a full tag such as v3.0.0-alpha.
After the branch move, the default branch's release workflow selects main for
3.x and v2.x for 2.x, and updates only the selected branch. Production runs from
other workflow branches are rejected; a missing source branch has no fallback.
Preparation converts the core dependency to the exact crates.io version and
checks its lockfile source, checksum and resolved features. The default branch's workflow tests and packages the prepared source from the selected branch and
runs the consumer tests.
The tested wheels and release identity are saved before the branch and annotated
tag are pushed. Python uses PEP 440 (3.0.0a0, 3.0.0b0, 3.0.0rc1); Git tags
and the Cargo dependency retain the core spelling.
Retry from the default branch with gh workflow run release.yml -f version=<tag>.
An existing tag selects the original prepared commit, even if the branch has
advanced. The evidence linked from the tag identifies the authenticated release run and its tested wheels. A retry verifies that
evidence, leaves matching PyPI wheels in place, and uploads only missing original
wheels. It stops if a published wheel differs or the evidence is unavailable.
Never delete or move a release tag to recover. A successful retry installs and
tests the exact version from PyPI, even if it did not upload anything.
Evidence is retained for 90 days; retain it through recovery. Older tags without
this evidence cannot be resumed automatically.
Before the branch move, a manual rehearsal can use the proposed workflow branch:
gh workflow run release.yml --ref <workflow-branch> \
-f version=v3.0.0-alpha -f rehearsal=true -f source_alias=v3 \
-f candidate=<full-core-sha>
gh workflow run release.yml --ref <workflow-branch> \
-f version=v2.0.12 -f rehearsal=true -f source_alias=mainAfter the move, omit source_alias. A 3.x rehearsal uses the explicit candidate
core SHA and records its actual crate version separately from the wheel label;
a 2.x rehearsal resolves the published crate. Both use prepared Git bundles,
without pushing temporary branches. Rehearsals do not change tags, branches,
registry entries, releases, or issues. The run summary and a retained artifact
record the selected branch and core dependency, wheel hashes, and consumer test
results.
Runs for the same tag are serialized. GitHub can cancel an older pending run when another is queued; it does not provide a durable release queue. Re-dispatch any canceled pending release that is still needed.
The alpha retains the existing packaging limits: Linux requires glibc 2.34 or newer, no source distribution is published, and the macOS universal wheel contains both architectures. This does not mean the wheel was tested on both at runtime. Python's glibc/sdist work remains #9.
MIT OR Apache-2.0