← Latest papers
💬 NLP

SQL Query Engine: A Self-Healing LLM Pipeline for Natural Language to PostgreSQL Translation

This paper introduces SQL Query Engine, an open-source, self-hosted service that translates natural language into validated PostgreSQL queries using a two-stage LLM pipeline featuring automatic schema introspection, a flexible response parser, and an iterative self-healing loop that significantly boosts execution accuracy while ensuring read-only safety and zero regressions.

Original authors: Muhammad Adeel Ijaz

Published 2026-04-21
📖 5 min read🧠 Deep dive

Original authors: Muhammad Adeel Ijaz

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you have a very smart, but slightly impatient, robot assistant. You want to ask it questions about your company's database (like "How many orders did we sell last month?"), but the robot only speaks "Database Language" (SQL).

If you ask the robot a question, it tries to write the code to get the answer. Sometimes, it gets it right immediately. But often, it makes a typo, uses the wrong column name, or forgets a rule. In the old days, the robot would just say, "Error," and give up, leaving you with no answer.

SQL Query Engine is a new system that changes the rules. It's like giving that robot a self-healing superpower and a strict safety inspector.

Here is how it works, broken down into simple concepts:

1. The Two-Stage Process: The Architect and The Inspector

Instead of just guessing the answer once, the system works in two distinct steps:

  • Stage 1: The Architect (Generation)
    The robot looks at your database (like looking at a blueprint of a house) and tries to write the SQL code to answer your question. It doesn't just guess; it uses a "multi-strategy parser" to make sure it actually finds the code, even if the robot wrote it inside a messy paragraph or a JSON box.
  • Stage 2: The Inspector (Evaluation & Self-Healing)
    This is the magic part. Before showing you the result, the system runs the code against the database.
    • If it works: Great! It shows you the answer immediately.
    • If it fails: The system doesn't give up. It catches the error message (e.g., "Column 'order_date' doesn't exist") and hands it back to the robot. It says, "Hey, you made a mistake here. The database says 'order_date' is actually called 'order_timestamp'. Fix it and try again."
    • The robot tries again. If it fails again, the system gives it the new error, and the robot tries again. This loop continues until it gets it right or runs out of tries.

2. The Safety Nets: "Don't Fix What Isn't Broken"

The paper highlights two clever tricks to make sure the robot doesn't accidentally break a good answer while trying to "fix" it:

  • The "Early-Accept" Rule: If the robot writes a query that works perfectly and returns data, the system stops immediately. It doesn't ask the robot to "double-check" its own work. This prevents the robot from overthinking and accidentally changing a correct answer into a wrong one.
  • The "Best Result" Tracker: Imagine the robot tries 5 times. The first try was perfect but empty (no data found). The second try was broken. The third try was perfect and had data. The system remembers the best thing it saw. If the robot runs out of tries, the system gives you the best answer it found, rather than just saying "I failed."

3. The Safety Guard: The "Read-Only" Goggles

One of the biggest fears with AI talking to databases is that it might accidentally delete your data or change prices.

  • The Analogy: Imagine the robot is wearing special goggles that make it physically unable to touch anything that isn't a window.
  • How it works: The system forces the database connection to be Read-Only. Even if the robot tries to write a command to DELETE or UPDATE data, the database driver (the bouncer at the door) simply refuses to let the command through. It's a hard safety wall, not just a polite request.

4. The Memory: The "Session Notebook"

If you ask the robot a question, then ask a follow-up question, the system remembers the context.

  • The Analogy: Instead of asking the robot to re-read the entire blueprint of the house every time you ask a question, it keeps a notebook (cached in Redis) open on the table. It only looks at the blueprint once at the start of the conversation, then just flips through its notebook for follow-ups. This makes the conversation much faster.

5. The Results: Does it actually work?

The authors tested this system on two types of challenges:

  • Synthetic Tests: Made-up questions on clean, perfect databases.
  • Real-World Tests (BIRD): Messy, real-world questions on complex databases.

The Verdict:

  • On the clean tests, the self-healing loop improved accuracy by up to 9.3%.
  • On the messy real-world tests, it improved accuracy by 4.6%.
  • Crucially, for the best models, it never made a correct answer worse (zero "regressions").

Why is this a big deal?

Most AI tools today are "one-and-done." If they get it wrong the first time, you're stuck. This system treats AI like a human employee: it lets them make a mistake, gives them the feedback ("Hey, check that column name"), and lets them try again until they get it right.

It turns a brittle, error-prone process into a resilient, self-correcting pipeline that is safe enough to use in real businesses without fear of deleting your data.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →