TL;DR
Implementing database-native functions inside a database kernel is a highly complex task that requires strict catalog registration, multi-file code placement, and deep dependency management. This paper introduces DBCooker, an LLM-based system tailored for database-native function synthesis that leverages graph-based analysis, pseudo-code planning, and progressive validation. DBCooker outpaces generic state-of-the-art coding agents, achieving an average of 34.55% higher accuracy across SQLite, PostgreSQL, and DuckDB.
What Problem Does This Paper Address?
Modern database systems constantly need new native functions—functions built directly into the database kernel—to support emerging application workloads or facilitate business migrations (e.g., moving legacy code from Oracle to PostgreSQL). However, writing these functions is vastly different from writing standard user-defined functions (UDFs) or general-purpose code.
As shown in the workflow below, implementing a single native SQL function like date_trunc() requires developers to navigate a tightly coupled environment:
- Catalog Registration: Registering multiple internal function units in system catalogs (like
pg_proc.datin PostgreSQL) based on specific input argument types. - Code Placement: Finding the exact source files across a massive repository to implement these units.
- Dependency Linking: Correctly referencing internal database macros and shared logic.
Missing or misusing these internal references leads to compilation failures or massive code duplication. General-purpose LLM coding tools (such as Claude Code or Qwen Code) struggle in this domain: they waste time scanning unrelated files, hallucinate non-existent database macros, and fail to capture fine-grained repository dependencies.
Methods
The authors present DBCooker, a system designed specifically around the architectural constraints of database kernels. It breaks the synthesis process down into three main modules:
- Function Characterization: Instead of relying on brute-force file searches, DBCooker uses deterministic, graph-based static analysis to map out dependencies. It extracts declarations from both official documentation and system catalogs, identifies distinct function units, and prunes away irrelevant codebase details to isolate essential reference units.
- Function Synthesis Operations:
- Pseudo-based Coding Plan Generation: It generates structured code skeletons with placeholders for variables and function bodies, filtering out low-quality plans using a deterministic scoring metric.
- Progressive Code Synthesis: Synthesis is treated as a fill-in-the-blank task. If a template-based approach fails, a probabilistic adaptation mechanism smoothly rolls back to a from-scratch exploratory synthesis mode.
- Three-Stage Code Validation: Code is rigorously checked at three levels: syntax (using parsers like ANTLR), compliance (running database-native compilation tools like
make install), and semantic (generating SQL test cases via an LLM to verify runtime behavior). - Adaptive Tool Orchestration: DBCooker abstracts its operations as callable tools and uses a memory-augmented controller to dynamically sequence steps based on how similar functions were successfully synthesized in the past.
Datasets
The system was evaluated on three mainstream database engines representing different complexities and implementation languages:
- SQLite (lightweight, written in C)
- PostgreSQL (full-featured, enterprise-grade, written in C)
- DuckDB (high-performance analytical, written in C++)
The authors evaluated a total of 348 functions across these systems (75 in SQLite, 145 in PostgreSQL, and 128 in DuckDB). To test DBCooker's ability to synthesize entirely new functions, they also collected distinct function declarations from one database (e.g., DuckDB) and attempted to implement them in another where they were completely absent (e.g., SQLite).
Key Insights and Ideas
- The Power of Internal References: Native database functions rely on a tiny, highly specialized subset of the codebase. Reusing these existing internal reference units reduces the required lines of code by up to 94.95% compared to writing them from scratch.
- Generic Agents Waste Time: General-purpose coding agents spend over 63% of their time traversing repositories and reading unrelated files, leaving less than 5% of their execution time for actual code generation.
- Declarations are the Main Bottleneck: Over 80% of errors made by advanced LLMs in this domain are declaration-related (e.g., redeclaring existing functions or using mismatched arguments), highlighting why database-specific catalog awareness is mandatory.
Key Results
- Substantial Accuracy Gains: DBCooker achieved significantly higher Compliance Accuracy (successful compilation) and Result Accuracy (passing all test cases) compared to baselines. On average, it outperformed state-of-the-art coding agents by 34.55%.
- Handling Complexity: For "HARD" functions (classified by the number of tokens and internal dependencies), DBCooker's adaptive orchestration allowed it to outperform other methods by 197.10%.
- New Function Synthesis: DBCooker successfully introduced 4 categories of entirely new native functions into the latest version of SQLite (v3.50), correctly handling complex aggregate functions like
covar_pop()by generating all required step, finalize, and inverse calculation units.
Limitations and Future Work
- Codebase Fragmentation vs. Context Windows: Database codebases are massive (PostgreSQL has over 1 million lines of code). Even as LLM context windows expand, blindly stuffing an entire codebase into the prompt incurs high inference costs and noise. Better localized retrieval remains essential.
- Deterministic Correctness vs. Probabilistic LLMs: Databases are mission-critical systems requiring absolute correctness. While DBCooker's progressive validation helps, bridging the gap between probabilistic LLM generation and deterministic kernel constraints remains an open challenge.
- Evolving Database Versions: Database internal macros and catalog definitions change across versions. Future work must ensure synthesis tools can dynamically adapt to version upgrades without needing full model retraining.
Summary updated