arXiv:2604.16511cs.DBcs.CL2026-04

让大模型自动修复错误,把自然语言变成可执行的PostgreSQL查询

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

  • 两阶段流程:先生成SQL,再通过自愈循环诊断并修正错误
  • 在合成数据上最高提升9.3个百分点准确率,零错误回归
  • 适合想快速接入大模型查数据库的开发者和产品经理

我们提出SQL Query Engine,一个开源、自托管的服务,通过双阶段大模型流水线将自然语言问题转化为经过验证的PostgreSQL查询。第一阶段自动进行模式探测并生成SQL;多策略解析器可从任意输出格式(如JSON、代码块或纯文本)中提取SQL,无需结构化API支持。第二阶段在PostgreSQL中执行查询,若失败或返回空结果,则进入迭代自愈循环,由大模型基于完整的SQLSTATE码和诊断信息定位错误。两种机制防止性能下降:早期接受机制对成功查询立即返回结果,无需重新评估;最佳结果追踪保留每次重试中的最优部分结果。每会话的模式上下文缓存于Redis,进度事件通过Redis Pub/Sub和SSE推送,兼容OpenAI风格的/v1/chat/completions接口,现有工具无需修改即可使用。所有数据库连接在驱动层设置为只读。我们在五个LLM后端上对合成基准(75个问题,3个数据库)进行评估,自愈循环带来最高+9.3pp准确率提升,最佳模型(Llama 4 Scout 17B,57.3%)实现零错误回归;在BIRD基准(437个问题,11个从SQLite迁移至PostgreSQL的数据库)上,完整流水线达到49.0%执行准确率(GPT-OSS-120B,+4.6pp)。源码地址:https://github.com/codeadeel/sqlqueryengine。

原文摘要 · Abstract (English)

We present SQL Query Engine, an open-source, self-hosted service that translates natural language questions into validated PostgreSQL queries through a two-stage LLM pipeline. The first stage performs automatic schema introspection and SQL generation; a multi-strategy response parser extracts SQL from any LLM output format (JSON, code blocks, or raw text) without requiring structured output APIs. The second stage executes the query against PostgreSQL and, upon failure or empty results, enters an iterative self-healing loop in which the LLM diagnoses the error using full SQLSTATE codes and PostgreSQL diagnostic messages. Two mechanisms prevent regressions: early-accept returns successful queries immediately without LLM re-evaluation, and best-result tracking preserves the best partial result across retries. Schema context is cached per session in Redis, progress events stream via Redis Pub/Sub and SSE, and an OpenAI-compatible /v1/chat/completions endpoint lets existing tools work without modification. All database connections are read-only at the driver level. We evaluate across five LLM backends on a synthetic benchmark (75 questions, three databases) where the self-healing loop yields up to +9.3pp accuracy gains with zero regressions on the best model (Llama 4 Scout 17B, 57.3%), and on BIRD (437 questions, 11 databases migrated from SQLite to PostgreSQL) where the full pipeline reaches 49.0% execution accuracy (GPT-OSS-120B, +4.6pp). Source code: https://github.com/codeadeel/sqlqueryengine.

自然语言转SQL自愈系统PostgreSQL大模型应用

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。