Skip to content

searchPillar 2 — Semantic query (SPARQL 1.1)

pgrdf.sparql(q TEXT) → SETOF JSONB parses SPARQL with spargebra, translates the algebra to dynamic SQL against the hexastore quad tables, executes it, and yields one JSONB row per solution.

Solution variables become JSONB keys; unbound variables come through as null.

pgrdf.sparql also dispatches UPDATE forms (INSERT, DELETE, MODIFY, lifecycle algebra) inside the caller's transaction — the read and write surface share a single UDF entry point.

Read surface

  • search BGP joins — N-pattern joins.
  • search FILTER — boolean composition + term-type tests.
  • hub OPTIONAL — left outer join.
  • hub UNION — disjoint pattern alternatives.
  • hub MINUS — set difference.
  • query_stats AggregatesCOUNT, SUM, AVG, MIN/MAX, GROUP_CONCAT, SAMPLE.
  • query_stats HAVING — post-aggregate FILTER.
  • search BIND — project computed values.
  • search Solution modifiersDISTINCT, type-aware ORDER BY, LIMIT, OFFSET.
  • search VALUES — inline bindings as a solution source.
  • search ASK — boolean queries.
  • account_tree GRAPH <iri> { … } — named-graph scoping (literal + variable forms).
  • hub Property paths^ + * ? \| with cycle-safe recursion + materialised-closure fast path.

Write surface

  • code SPARQL UPDATEINSERT DATA, DELETE DATA, pattern-driven INSERT/DELETE WHERE, atomic DELETE+INSERT/WHERE, graph-scoped WITH and inline GRAPH, plus lifecycle DROP / CLEAR / CREATE GRAPH.
  • code CONSTRUCTpgrdf.construct(q) → SETOF JSONB; templates (constant / variable / blank-node / multi-triple), GRAPH-scoped WHERE, WHERE-shorthand, round-trip ingest.
  • code DESCRIBEpgrdf.describe(q) → SETOF JSONB; the W3C SPARQL 1.1 §16.4 Concise Bounded Description of one or more resources.

Diagnostics

At a glance

sql
-- Multi-pattern join with a FILTER and a solution modifier.
SELECT * FROM pgrdf.sparql(
  'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
   SELECT ?s ?n
     WHERE { ?s foaf:name ?n .
             ?s <http://example.com/age> ?age
             FILTER(?age >= 30) }
   ORDER BY ?n LIMIT 50');

Next — BGP joins →

auto_storiesTraining

The SPARQL pillar splits naturally into three learning loops. Take them in order — each loop builds on the previous one:

Loop 1 — Read fundamentals

  • search BGP joins — N triple patterns become an N-way self-join. The core primitive.
  • search FILTER — once you can write a BGP, filtering it is the next leverage.
  • search Solution modifiers + ASK — DISTINCT, ORDER BY, LIMIT, OFFSET, plus existence checks.

Loop 2 — Composition

  • hub OPTIONAL — left outer join semantics; everything sparse-data needs.
  • hub UNION + MINUS — alternation and set difference.
  • query_stats Aggregates + HAVING + BIND — grouping, post-aggregate filtering, computed projections.
  • account_tree GRAPH clause — named-graph scoping (literal + variable forms, composition with all the above).

Loop 3 — Write surface + diagnostics

  • code SPARQL UPDATE — once you can read, the same UDF dispatches the six write forms.
  • code CONSTRUCT — read-side counterpart to UPDATE's template-from-pattern shape.
  • hub Property paths^ + * ? \|; the materialised-closure fast path is where inference and query pay each other back.
  • info sparql_parse + Error-message contract — programmatic shape inspection and stable error prefixes for tooling.

Learn more

MIT licensed. Documentation for pgRDF — built with VitePress, served via GitHub Pages.