Skip to content

pgrdf

pgrdf : RDF, SPARQL, SHACL, and OWL reasoning for PostgreSQL

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2640
pgrdf
pgrdf
0.5.0
FEAT
MIT
Rust
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sLd--
No
Yes
Yes
Yes
no
no
Relationships
Schemaspgrdf
See Also
rdf_fdw
pg_sparql
rdkit

PG14-17 only; production hook/cache deployments should preload pgrdf.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PIGSTY
0.5.0
18
17
16
15
14
pgrdf-
RPM
PIGSTY
0.5.0
18
17
16
15
14
pgrdf_$v-
DEB
PIGSTY
0.5.0
18
17
16
15
14
postgresql-$v-pgrdf-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
el8.aarch64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
el9.x86_64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
el9.aarch64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
el10.x86_64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
el10.aarch64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
d12.x86_64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
d12.aarch64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
d13.x86_64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
d13.aarch64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
u22.x86_64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
u22.aarch64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
u24.x86_64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
u24.aarch64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
u26.x86_64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
u26.aarch64
MISS
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0
PIGSTY 0.5.0

Source

pig build pkg pgrdf;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pgrdf;		# install via package name, for the active PG version

pig install pgrdf -v 17;   # install for PG 17
pig install pgrdf -v 16;   # install for PG 16
pig install pgrdf -v 15;   # install for PG 15
pig install pgrdf -v 14;   # install for PG 14

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pgrdf';

Create this extension with:

CREATE EXTENSION pgrdf;

Usage

Sources: pgRDF upstream README, pgRDF user guide, local metadata.

pgRDF stores RDF data inside PostgreSQL and exposes SQL-callable helpers for Turtle/TriG/N-Quads loading, SPARQL query/update, named graphs, SHACL validation, and RDFS/OWL 2 RL materialization.

CREATE EXTENSION pgrdf;
SELECT pgrdf.version();

Preload And PostgreSQL Version Caveat

The local Pigsty metadata packages pgrdf for PostgreSQL 14, 15, 16, and 17 only. Upstream also documents PostgreSQL 14-17 support and defers PostgreSQL 18 while it remains pinned to pgrx 0.16.

pgrdf must be present in shared_preload_libraries before PostgreSQL starts. Without preload, upstream documents that the shared-memory dictionary and plan-cache atomics are not initialized and the first pgRDF call can fail.

shared_preload_libraries = 'pgrdf'

Restart PostgreSQL after changing this setting, then verify:

SHOW shared_preload_libraries;

SELECT pgrdf.parse_turtle(
  '@prefix ex: <http://example.org/> . ex:t a ex:T .',
  1::bigint,
  'http://example.org/'
);

Load RDF

Use parse_turtle for inline Turtle payloads and load_turtle for server-side files. Graph ids are bigint values; named graph helpers map ids to IRIs.

SELECT pgrdf.add_graph(100::bigint, 'http://example.org/graph/main');

SELECT pgrdf.parse_turtle(
  '@prefix ex: <http://example.org/> .
   ex:alice ex:knows ex:bob .
   ex:alice ex:name "Alice" .',
  100::bigint,
  'http://example.org/graph/main'
);

SELECT pgrdf.load_turtle('/srv/rdf/foaf.ttl', 100::bigint);
SELECT pgrdf.count_quads(100::bigint);

Related ingest and graph-management functions documented upstream include parse_trig, parse_nquads, add_graph, drop, clear, copy, move_graph, graph_id, and graph_iri.

Query With SPARQL

pgrdf.sparql(text) returns SPARQL results as SQL rows. The upstream v0.5 surface includes SELECT and ASK, filters, ordering, limits, OPTIONAL, UNION, MINUS, aggregates, VALUES, BIND, CONSTRUCT, DESCRIBE, named-graph GRAPH clauses, and property paths.

SELECT *
FROM pgrdf.sparql(
  'PREFIX ex: <http://example.org/>
   SELECT ?person ?name
   WHERE {
     ?person ex:name ?name .
     FILTER(REGEX(?name, "^A", "i"))
   }
   ORDER BY ?name
   LIMIT 20'
);

Named-graph queries can bind graph IRIs:

SELECT *
FROM pgrdf.sparql(
  'PREFIX ex: <http://example.org/>
   SELECT ?g (COUNT(*) AS ?n)
   WHERE { GRAPH ?g { ?s ex:name ?name } }
   GROUP BY ?g
   ORDER BY ?g'
);

Update Graphs

The upstream v0.5 surface includes SPARQL Update forms such as INSERT DATA, DELETE DATA, INSERT/DELETE WHERE, DELETE+INSERT WHERE, and graph lifecycle statements.

SELECT pgrdf.sparql(
  'PREFIX ex: <http://example.org/>
   INSERT DATA {
     GRAPH <http://example.org/graph/main> {
       ex:bob ex:name "Bob" .
     }
   }'
);

Reasoning And Validation

Use pgrdf.materialize(graph_id, profile) to write inferred triples for rdfs or owl-rl profiles. Materialization is intended to be repeatable; upstream documents that previous inferred rows are dropped before writing the new closure.

SELECT pgrdf.parse_turtle(
  '@prefix ex:   <http://example.com/> .
   @prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
   @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
   ex:Engineer rdfs:subClassOf ex:Person .
   ex:Person   rdfs:subClassOf ex:Agent .
   ex:alice    rdf:type        ex:Engineer .',
  100::bigint
);

SELECT pgrdf.materialize(100::bigint, 'owl-rl');

SELECT *
FROM pgrdf.sparql(
  'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
   PREFIX ex:  <http://example.com/>
   SELECT ?class WHERE { ex:alice rdf:type ?class }'
);

Use pgrdf.validate(data, shapes, mode) for SHACL validation; upstream documents JSONB sh:ValidationReport output and native SHACL Core support. SHACL-SPARQL constraint execution is documented upstream as gated by its RDF library dependency, so treat mode => 'sparql' as an advanced surface to verify against the exact installed version.

Operational Helpers

Useful introspection and cache-management helpers documented upstream include:

FunctionUse
pgrdf.stats()Inspect runtime counters and cache state
pgrdf.shmem_reset()Reset shared-memory dictionary/cache state
pgrdf.plan_cache_clear()Clear prepared SPARQL plan cache
pgrdf.sparql_parse(text)Inspect parsed SPARQL without executing it

The pgrdf.path_max_depth setting guards property-path expansion depth.

Last updated on