DM: Chapter 6
Data and Information Management: Positive Datalog
Instructor: Bas Ketsman
Academic Year: 2025-2026
Topic: Adding recursion to query languages via Positive Datalog.
Agenda of Today
Introduction to Recursion: Adding recursive capabilities to standard query languages.
Datalog Syntax: Formal structure of Datalog rules and programs.
Model-Theoretic Semantics: Declarative logic-based interpretation of Datalog.
Datalog as a Query Language: Definition of queries and output.
Datalog Properties and Expressiveness: Comparison with other languages and limitations.
Datalog Syntax
Definition of a Datalog Rule:
A Datalog rule over a schema is an expression of the form:
The Head: The expression .
The Body: The expression .
Parameters:
The number of body atoms .
Each predicate is a member of the schema .
The terms and (for every ) are tuples consisting of constants and variables.
Safety Requirement: Every variable that appears in the head tuple must also be mentioned in at least one body atom tuple .
Datalog Program: A Datalog program over schema is a finite set of Datalog rules over .
Comparison Note: While Datalog has a similar flavor to Prolog, it is not the same (e.g., Datalog is purely declarative and does not use Prolog\'s search strategy).
Example: Reachability in Graphs
Scenario: Given a directed graph represented by an relation, collect all nodes reachable from a specific source node .
Rule 1 (Base Case):
Rule 2 (Recursive Step):
Extensional vs. Intensional Relations
Intensional Database Predicates (IDB):
A relation name is intensional if it occurs in the head of at least one rule.
These predicates are computed through definitions within the program.
Example: in the reachability program.
Extensional Database Predicates (EDB):
A relation name is extensional if it does not occur in any rule head.
These predicates are defined through explicit enumeration within the database.
Example: in the reachability program.
Schema Assumption: Similar to conjunctive queries (CQs), schemas are often not explicitly provided; it is assumed that databases consist of the extensional relations of the program .
Example: Points-To Analysis
Context: Based on the work of Smaragdakis and Balatsouras (2015).
EDB Conversion of C Instructions:
Instruction
a = &bbecomes .Instruction
a = bbecomes .Instruction
a = *bbecomes .Instruction
*a = bbecomes .
IDB Relation: stores pairs of variables where may point to .
Rules for Analysis:
Datalog Semantics Overview
Datalog utilizes coinciding declarative and operational semantics:
Model-Theoretic Semantics: Declarative approach based on First-Order (FO) logic formulas.
Fixpoint Semantics: Operational approach based on the repeated application of a fixpoint operator.
Proof-Tree Semantics: Note that this is mentioned as out of scope for the current course.
Model-Theoretic Datalog Semantics
Interpretation: A Datalog rule of the form is interpreted as a First-Order sentence :
FO Theory: A Datalog program defines a set of constraints called an FO theory:
Model Definition: A database over the extensional schema of () is a model for if it satisfies all sentences in the FO theory.
Program Output: Given a Datalog program and database of , the output is defined as the minimal model (with respect to set inclusion ) of that contains .
Validity Proof Hint:
A model of containing always exists (specifically, the database containing all possible facts over the Domain ).
The intersection of all valid models is itself a model and is the unique minimal model.
Fixpoint Semantics: Immediate Consequences
Immediate Consequences of P over D:
Include all facts currently in .
Include facts for which there exists a rule and a valuation mapping all body atoms into existing facts in , results in the head mapping to .
Operational Note: This computes the rule similarly to a Conjunctive Query (CQ).
Immediate Consequence Operator ():
A function mapping databases over to their immediate consequences.
.
.
Fixpoint Definition: A database is a fixpoint for if .
Least Fixpoint (): The first/least database that establishes a fixpoint for and .
Definition of Output: The output is defined as .
Equivalence of Semantics
Proposition: For any Datalog program and database over , the Fixpoint and Model-Theoretic semantics are equivalent because:
is monotone.
There exists an integer such that the fixpoint is reached at .
Every fixpoint of containing is a model for and .
Every model for and contains the set produced by .
Datalog as a Query Language
Definition: A Datalog query over schema is a pair .
is a Datalog program over schema .
.
.
.
Query Output (): The set of tuples present in relation within the resulting database instance .
Properties and Expressiveness
Preservation Properties: Datalog queries are preserved under homomorphisms and are monotone.
Expressive Limitations: Because of these properties, Datalog cannot express:
Inequalities.
Negative relational atoms.
Set difference.
Complexity: Naive evaluation of Datalog takes polynomial many steps relative to the size of the database.
Naive Datalog Evaluation Example
Database : , .
Program Rules:
Rounds of Evaluation:
Round 1: .
Round 2: .
Round 3: .
Round 4: . (Fixpoint reached).
Critique: Already discovered facts are derived repeatedly in every round, which is inefficient.
Semi-Naive Datalog Evaluation
Logic: Avoid redundant work by focusing on "new" facts found in the previous step.
Variables:
: Facts found after step .
.
Rule Expansion (Example ):
Let the rule be a function .
Expanded:
Procedure:
Maintain two copies for each IDB relation : the relation itself and its delta .
Apply the immediate consequence operator using the split formula to isolate new derivations.
Update relations and deltas after each application until the delta is empty.
Linear Datalog Programs
Definition: A rule is linear if it contains at most one IDB relation name in its body.
Linear Program: A program consisting exclusively of linear rules.
Efficiency: Linear programs are significantly more efficient to compute than non-linear programs.
Linear Delta Derivation:
This removes the need to cross-join multiple IDB relations or deltas from different steps.
Undecidability and Satisfiability
Undecidable Properties:
Determining if there is a bound on the number of applications of the immediate consequence operator needed for a fixpoint.
Determining query containment () between two Datalog queries.
Satisfiability Question: The transcript asks if every Datalog query is satisfiable.
Questions & Discussion
Discussion on Minimal Models:
Question: Which databases are minimal models for and in the reachability example?
Answer Logic: The unique minimal model is the one generated by the fixpoint iteration, containing only nodes with a path from 's'.
Discussion on Immediate Consequences:
Question: What are the immediate consequences of the reachability program on a graph with nodes , , ?
Answer: In the first step, we derive . In subsequent steps, we derive and .
Discussion on Performance:
Question: Why use Semi-Naive evaluation?
Answer: To avoid the overhead of re-deriving facts that were already established in previous rounds.