pgrdf

RDF, SPARQL, SHACL, and OWL reasoning for PostgreSQL

Overview

PackageVersionCategoryLicenseLanguage
pgrdf0.6.4FEATMITRust
IDExtensionBinLibLoadCreateTrustRelocSchema
2640pgrdfNoYesYesYesNoNopgrdf
Relatedrdf_fdw pg_sparql rdkit

PG14-17 only; production hook/cache deployments should preload pgrdf; pgrx patched to 0.18.1.

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY0.6.41817161514pgrdf-
RPMPIGSTY0.6.41817161514pgrdf_$v-
DEBPIGSTY0.6.41817161514postgresql-$v-pgrdf-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64PIGSTY MISS
el8.aarch64PIGSTY MISS
el9.x86_64PIGSTY MISS
el9.aarch64PIGSTY MISS
el10.x86_64PIGSTY MISS
el10.aarch64PIGSTY MISS
d12.x86_64PIGSTY MISS
d12.aarch64PIGSTY MISS
d13.x86_64PIGSTY MISS
d13.aarch64PIGSTY MISS
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
u22.x86_64PIGSTY MISS
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
u22.aarch64PIGSTY MISS
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
u24.x86_64PIGSTY MISS
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
u24.aarch64PIGSTY MISS
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
PIGSTY 0.6.4
u26.x86_64PIGSTY MISS
u26.aarch64PIGSTY MISS

Build

You can build the RPM / DEB packages for pgrdf using pig build:

pig build pkg pgrdf         # build RPM / DEB packages

Install

You can install pgrdf directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

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

Install the extension using pig or apt/yum/dnf:

pig install pgrdf;          # Install for current active PG version
pig ext install -y pgrdf -v 17  # PG 17
pig ext install -y pgrdf -v 16  # PG 16
pig ext install -y pgrdf -v 15  # PG 15
pig ext install -y pgrdf -v 14  # PG 14
dnf install -y pgrdf_17       # PG 17
dnf install -y pgrdf_16       # PG 16
dnf install -y pgrdf_15       # PG 15
dnf install -y pgrdf_14       # PG 14
apt install -y postgresql-17-pgrdf   # PG 17
apt install -y postgresql-16-pgrdf   # PG 16
apt install -y postgresql-15-pgrdf   # PG 15
apt install -y postgresql-14-pgrdf   # PG 14

Preload:

shared_preload_libraries = 'pgrdf';

Create Extension:

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.