AskData: plain-English analytics on PostgreSQL

Type
Personal project
Status
Complete, October 2026
Stack
FastAPI, Pydantic, sqlglot, OpenAI API; Next.js, TypeScript; PostgreSQL 16 in Docker Compose
Data
Two demo databases (Pagila, a DVD-rental store, and retail_ops, a synthetic retail company), or any PostgreSQL database through a read-only user
The answer comes as a sentence, a chart or table, the assumptions made under “I assumed”, and under “How I got this” the tables used, the SQL and the cost.

What it does

You ask a question in plain English and get a sentence with the names and numbers that matter, a chart or a table you can copy or download as CSV, and the SQL behind it. It handles follow-ups in the same conversation (“now only the top 5”), keeps past conversations, lets you browse the tables, and connects to your own PostgreSQL database through a read-only user.

It is built for people who know their business but don’t read SQL. They can’t check the query, so the app has to decide when to answer at all.

Answer, clarify or decline

A triage call sees the question, the conversation and the candidate tables, and returns one of three decisions. It answers by default and states small choices as assumptions. It asks only when two readings would give materially different answers, and declines only when no column could hold the fact.

“Who are our best customers?” could mean most spent or most rentals. Each option is a full question you can click.
“What is the profit margin for each film?” needs costs the database doesn’t record. No query runs, and it suggests questions it can answer.

How a question is answered

  1. Schema. At startup the backend reads tables, columns, types, keys and comments. Partitioned tables appear as one table, so the model can’t sum two monthly partitions and call it the total.
  2. Triage. One model call decides to answer, clarify or decline.
  3. Retrieval. Tables and columns are scored by the words in the question through a small business vocabulary (“spent” → amount, payment), plus the shortest foreign-key path between matched tables, so the model sees the joins it needs.
  4. SQL. One call writes a single SELECT and up to three assumptions in plain words; a second call reviews it against the question and can ask for one rewrite.
  5. Checks and run. sqlglot parses the query and allows one read-only SELECT. It runs in a read-only transaction with a 10-second timeout and a 200-row cap; errors go back to the model for a bounded repair.
  6. Answer. A small model writes the sentence from the result rows. Every number in it is checked against the rows, and a plain template replaces it if one doesn’t match.

Four of these steps call a model, each with one narrow job and a structured (Pydantic) output; the rest is plain Python. There is no agent loop, so the number of model calls per question, and with it the cost and the latency, has a fixed upper bound.

How AskData answers a question: triage, then table retrieval, SQL writing, review, safety check, read-only run and the answer sentence; vague questions get a clarifying question and unanswerable ones a refusal. How AskData answers a question: triage, then table retrieval, SQL writing, review, safety check, read-only run and the answer sentence; vague questions get a clarifying question and unanswerable ones a refusal.
Architecture, from the project README.

Results

I tested it on 52 questions about two demo databases, including questions that are vague on purpose and questions the data can’t answer. Each answer was checked against the result of a hand-written SQL query.

QuestionsAnswered correctlyVague: asked firstUnanswerable: declinedWrong answersMedian timeCost per question
All 5235 of 386 of 86 of 63 of 52 (6%)8.3 s / 8.7 s$0.0072 / $0.0051
32 held-out19 of 204 of 66 of 61 of 32 (3%)7.8 s / 8.3 s$0.0064 / $0.0044

“Wrong answers” are the ones that matter most: a confident answer that is wrong, or an answer to a question the data can’t support. Asking or declining is visible to the user and doesn’t count. The 32 held-out questions were written before any test run and never used to change prompts or rules, so that row is the fair one. Both runs gave the same outcome on every question; time and cost show run 1 / run 2.

Safety

AskData is meant to be pointed at databases it doesn’t own, so it doesn’t rely on a single check.

Connecting as a superuser is refused with the reasons; a read-only user passes.

Limits