Query Optimization Reference
This page is the reference for pg_ripple's SPARQL query optimizer.
Overview
pg_ripple applies multiple optimization passes before generating SQL from SPARQL algebra:
- sparopt algebra optimizer: first-pass algebra rewriting (variable substitution, filter pushdown, DISTINCT elimination with SHACL hints).
- Self-join elimination: star patterns on the same subject are collapsed into single-scan plans with multiple joins.
- Filter pushdown: FILTER constants are encoded to
BIGINTat translation time so comparisons happen on integer values, not strings. - SHACL hints:
sh:maxCount 1predicates omit DISTINCT;sh:minCount 1predicates use INNER JOIN instead of LEFT JOIN. - Plan cache: compiled SQL is stored in
_pg_ripple.plan_cacheand reused across identical queries (keyed by query text + current role + relevant GUCs). - TopN push-down:
ORDER BY ... LIMIT Npatterns are pushed into subqueries to avoid sorting full result sets. - Leapfrog TrieJoin (WCOJ): worst-case optimal join planning for cyclic SPARQL graph patterns.
Status
SELECT feature_name, status FROM pg_ripple.feature_status()
WHERE feature_name LIKE '%plan%' OR feature_name LIKE '%cache%' OR feature_name LIKE '%wcoj%';
Plan Cache
The plan cache avoids re-compiling SPARQL→SQL for repeated queries. Key details:
- Cache key: SHA-256 of (query text, current_role, GUC snapshot)
- Cache invalidated on: VP promotion (schema change), extension upgrade,
plan_cache_reset() - Maximum entries:
pg_ripple.plan_cache_size(default: 512) - Eviction policy: LRU
-- Inspect cache
SELECT * FROM _pg_ripple.plan_cache;
-- Manual invalidation
SELECT pg_ripple.plan_cache_reset();
Property Path Optimization
Property paths compile to WITH RECURSIVE … CYCLE queries using PostgreSQL 18's
native CYCLE clause for hash-based cycle detection. Bounded depth paths
({n,m}) use iterative CTEs limited to pg_ripple.max_property_path_depth
hops (default: 20). Early fixpoint termination avoids iterating past convergence
for bounded hierarchies.
Magic Sets (Goal-Directed Inference)
When using pg_ripple.query_goal() for Datalog queries, magic sets
transformation rewrites rules to focus inference on the bindings needed to
answer the goal, avoiding full forward-chaining materialization.
SQL Functions
| Function | Description |
|---|---|
pg_ripple.plan_cache_reset() → void | Invalidate all cached query plans |
pg_ripple.explain_sparql(query TEXT, analyze BOOLEAN) → TEXT | Inspect query plan with optional live stats |