SPARQL Reference

This page is the reference for pg_ripple's SPARQL 1.1 query and update engine.

Overview

pg_ripple implements SPARQL 1.1 Query Language and SPARQL 1.1 Update as native PostgreSQL SQL functions. All SPARQL execution is performed inside the extension via the spargebra parser, an algebra optimizer (sparopt), and a translation layer that converts SPARQL algebra to PostgreSQL SQL executed through SPI. Results are decoded back through the dictionary to return RDF terms as text.

Status

SELECT feature_name, status FROM pg_ripple.feature_status()
WHERE feature_name LIKE 'sparql%';

SQL Functions

FunctionDescription
pg_ripple.sparql(query TEXT) → SETOF recordExecute a SPARQL SELECT query
pg_ripple.sparql_update(update TEXT) → voidExecute SPARQL 1.1 Update (INSERT DATA, DELETE DATA, DELETE/INSERT WHERE, CLEAR, DROP, COPY, MOVE, ADD)
pg_ripple.sparql_construct(query TEXT) → TEXTExecute SPARQL CONSTRUCT, return Turtle
pg_ripple.sparql_describe(iri TEXT) → TEXTExecute SPARQL DESCRIBE, return Turtle
pg_ripple.sparql_ask(query TEXT) → BOOLEANExecute SPARQL ASK query
pg_ripple.explain_sparql(query TEXT, analyze BOOLEAN) → TEXTReturn JSON explain plan for a SPARQL query
pg_ripple.sparql_cursor(query TEXT, page_size INT) → TEXTOpen a server-side cursor for large result sets
pg_ripple.sparql_cursor_next(cursor_id TEXT, page_size INT) → SETOF recordFetch next page from cursor
pg_ripple.sparql_cursor_close(cursor_id TEXT) → voidClose cursor and release resources
pg_ripple.sparql_cursor_turtle(query TEXT, page_size INT) → TEXTOpen CONSTRUCT cursor returning Turtle pages
pg_ripple.sparql_cursor_jsonld(query TEXT, page_size INT) → TEXTOpen CONSTRUCT cursor returning JSON-LD pages
pg_ripple.subscribe_sparql(id TEXT, query TEXT, graph_iri TEXT) → voidRegister a live subscription
pg_ripple.unsubscribe_sparql(id TEXT) → voidRemove a live subscription
pg_ripple.list_sparql_subscriptions() → SETOF recordList active subscriptions

SPARQL 1.1 Feature Coverage

pg_ripple supports the full SPARQL 1.1 specification:

  • SELECT with projection, DISTINCT, REDUCED, LIMIT, OFFSET, ORDER BY
  • CONSTRUCT with graph patterns and template triples
  • DESCRIBE returning a CBD (Concise Bounded Description)
  • ASK returning boolean
  • Graph patterns: BGP, OPTIONAL, UNION, MINUS, GRAPH, SERVICE, FILTER, BIND, VALUES
  • Property paths: |, /, ^, ?, *, +, !, {n}, {n,}, {n,m}
  • Aggregate functions: COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT, SAMPLE
  • Built-in functions: All ~50+ SPARQL 1.1 scalar functions
  • Subqueries: nested SELECT patterns
  • SPARQL Update: all 10 update forms

RDF-star Support

Triple-quoted patterns <<s p o>> in both subject and object positions are supported. The dictionary stores RDF-star terms as encoded triples (hash of the quoted triple's subject, predicate, and object encoded together).

Performance Notes

  • Integer joins: all SPARQL-to-SQL translation encodes bound terms to BIGINT before generating SQL; no string comparisons occur inside VP table queries.
  • Filter pushdown: FILTER constants are encoded at translation time.
  • Self-join elimination: star patterns on the same subject are collapsed into single-scan plans.
  • The plan cache (_pg_ripple.plan_cache) stores compiled SQL for reuse across repeated queries.

SPARQL Extension Function IRI Namespace (API-04, v0.91.0)

All pg_ripple SPARQL extension functions are defined under the canonical namespace:

http://pg-ripple.org/fn/

The shorthand pg: prefix maps to this namespace in all SPARQL queries executed through pg_ripple. The prefix is auto-declared — queries do not need to explicitly declare PREFIX pg: <http://pg-ripple.org/fn/>, though doing so is harmless and is the recommended style for queries intended to run against multiple SPARQL endpoints.

Available extension functions

Short formFull IRISinceDescription
pg:confidence(?s, ?p, ?o)http://pg-ripple.org/fn/confidencev0.87.0Highest confidence score across models for a triple
pg:pagerank(?node)http://pg-ripple.org/fn/pagerankv0.88.0PageRank score for a node (default topic)
pg:pagerank(?node, ?topic)http://pg-ripple.org/fn/pagerankv0.88.0PageRank score for a node in a named topic
pg:similar(?a, ?b)http://pg-ripple.org/fn/similarv0.27.0Cosine similarity between embedding vectors
pg:fuzzy_match(?a, ?b)http://pg-ripple.org/fn/fuzzy_matchv0.87.0Trigram similarity (requires pg_trgm)
pg:confPath(?pred, ?minConf)http://pg-ripple.org/fn/confPathv0.87.0Property path with confidence threshold filter

Federation note

Federation partners that wish to invoke pg_ripple extension functions remotely must use the full IRI form, as remote endpoints do not auto-declare the pg: prefix:

FILTER(<http://pg-ripple.org/fn/confidence>(?s, ?p, ?o) > 0.8)