arXiv:2609.06301cs.DBSep 2026

Manuscript Offprint

arXiv:2609.06301

Compiling Linear Datalog to SQL for Program Analysis

Amir Shaikhha, Anna Herlihy, Hung Ngo

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Datalog is a declarative query language that has proven highly effective for expressing static program analyses. Although Datalog has deep roots in database theory, most recent advances have largely emerged from the programming languages and compiler communities, with systems such as Soufflé. In contrast, modern relational engines have made significant progress in optimizing recursive SQL.

This paper revisits the connection between Datalog and relational databases, advocating recursive SQL as a backend for Datalog evaluation. We present a compilation framework that translates Datalog programs, particularly those in the Linear Datalog fragment, into equivalent recursive SQL queries. To bridge the gap between Datalog and SQL, the compiler routes every program through an intermediate language called Midlog. The compiler additionally recovers functional dependencies from the program and exposes them as schema keys, unlocking the engine's standard query optimizations. This approach enables existing database engines to execute a broad class of program analyses, outperforming the Soufflé engine by up to an order of magnitude on the Umbra backend. Umbra achieves a geometric-mean speedup of 5.46×5.46\times at 8 threads, whereas DuckDB is competitive with Soufflé single-threaded and is slower at 8 threads (geometric-mean speedup of 0.68×0.68\times). Furthermore, the generated SQL is portable; it runs on seven database systems without any engine modification. Our results highlight what the relational engines require to fully support Datalog for large-scale program analysis.

1 Introduction

Datalog is a declarative programming language popular in program analysis: many static analyses, such as points-to analysis, call-graph construction, and dataflow analysis, are fixed-point computations. A direct implementation of such analyses in a general-purpose language requires encoding how the fixed point is reached (worklists, dependency tracking, iteration order). In Datalog, it is a set of recursive rules describing how to derive new facts from known facts, with a bottom-up engine computing the least fixed point. This combination of expressiveness and efficiency has made Datalog an attractive choice for program analysis tasks in both academia and industry [3, 4, 27, 31].

Despite its database-theory roots, most academic development of Datalog has happened outside the database community. State-of-the-art systems such as Soufflé [27] are standalone compilers translating Datalog into optimized C++, bypassing relational engines entirely, even though Datalog is relational algebra extended with a fixed-point operator [2].

Relational engines (RDBMS) have meanwhile made significant progress in recursion support. The SQL:1999 standard introduced recursive common table expressions (CTEs), which expose the entire recursive computation to the query optimizer, enabling global rewrites, indexing, cost-based optimization, and parallel execution [26, 38].

These two lines of work have evolved in isolation. Datalog compilers extract performance through specialization, but cannot reuse the indexing, optimization, and parallelism of RDBMS. RDBMS support recursive queries, but lack Datalog’s concise abstractions and expressiveness. We target both axes, compiling Datalog into recursive SQL so that RDBMS serve as efficient Datalog backends without custom runtimes.

In this paper, we propose DLSQL, a compiler that translates Linear Datalog, a well-defined fragment of Datalog in which each recursive rule contains at most one recursive predicate in its body, into recursive SQL. Thanks to this restriction, recursion maps directly to SQL's recursive CTEs.

Even within Linear Datalog, a substantial mismatch with recursive SQL remains. Midlog is an intermediate language that bridges this gap and is designed around two fundamental differences. First, despite the support for mutual recursion in SQL:1999, no engine we tested implements it (except for MariaDB); thus, unlike Datalog, SQL in practice does not support mutual recursion [20]. Second, SQL employs a fundamentally different scoping discipline than Datalog.

We make the following contributions:

• We identify Linear Datalog as an expressive subset of Datalog and express reduced kernels of five canonical static analyses, spanning call-graph construction, borrow checking, and points-to/escape/dataflow analysis (Section 3).

• We propose Midlog, an intermediate language that resolves the two mismatches between Linear Datalog and recursive SQL: mutual recursion and scoping (Section 4).

• We present the DLSQL compiler pipeline, which routes every program through Midlog in four phases (Section 5).

  • We develop two optimizations: a multi-head elimination removing unnecessary mutual recursion, and a functional-dependency pass recovering schema keys from the input data, enabling the engine to optimize further (Section 6).

  • We evaluate DLSQL on five static program-analysis workloads across seven database engines and against four state-of-the-art Datalog systems: Soufflé, FlowLog, Logica, and RecStep. On Umbra, DLSQL achieves a geometric-mean speedup of 5.46×5.46\times over Soufflé at 8 threads, and up to 14.3×14.3\times on individual benchmarks. On DuckDB, it reaches 0.96×0.96\times single-threaded and 0.68×0.68\times at 8 threads. Furthermore, we study portability across engines, the impact of both optimizations, and scaling with input size (Section 7).

References (17)