r/SQL • u/donewitheverything26 • Oct 01 '26
Discussion gave claude read only access to our postgres. it said status_cd 4 means "cancelled". it means refunded
I set up a Postgres MCP server on a read only role last week so the analysts could ask questions without pinging me every hour.
The first real question was how many orders did we lose last month. Claude looked at orders, saw status_cd with values 1 to 6, decided 4 was cancelled and gave a clean number with a nice little breakdown. 4 is refunded. Cancelled is 6. Nobody wrote that down anywhere. It lives in an enum in the app code and in my head.
The number was off by about a third and it read exactly like a right answer. No I'm assuming, no hedge. The analyst was about to put it in a deck.
So now I'm stuck on how to give an agent the meaning of fields without me turning into a full time documentation service:
- Comments on the columns in Postgres and hope the server passes them through
- A markdown data dictionary in the system prompt
- A view layer with human names on everything
- Make the agent ask before it interprets any code column
What are people actually doing here? and has anyone got it to say I don't know what 4 means instead of picking one?
41
u/MoldyDucky Oct 01 '26
Comments for the column name has worked well in my experience as a stopgap, but the general concept for this problem is called building a semantic layer
10
u/NoviceCouchPotato Oct 01 '26
I understand it is status 4, 6 etc. in the source and your bronze layer. But don’t you do data modeling and create a silver/gold layer on top where you already mapped status 4 = refunded that your analysts can query?
Sounds like you need to get that sorted first before simply granting an MCP access to source data?
4
u/Kazcandra Oct 01 '26
Enums are a data type, they're not data. That's your problem. It's like saying "this value is an int"
If it's data, it goes in the tables.
3
u/my_password_is______ Oct 01 '26
well that's dumb
put it in a table
problem solved
5
u/CarbonChauvinist Oct 01 '26
In Fabric this is recommended to create an Ontology to assist agents in gaining correct context and business logic. Realize you're on Postgres, but am assuming something similar would be the way to start searching ... Perhaps?
5
u/NW1969 Oct 01 '26
Pointing agents/ai directly at your tables is a recipe for disaster.
Having a semantic layer is non-negotiable
There seems to be a general belief that implementing agents/ai is easy and doesn't require much effort. This is rarely the case - not only do you have to build the semantic layer but you also have to gather/validate/sign-off all the information that needs to be put into the semantic layer. This is the point where you discover there are 2 incompatible definitions of "revenue" being used and you open up the first of many cans of worms
2
u/SearchAtlantis Oct 01 '26
Is there no dimension table? One giant analytical god table?
2
u/Total-Produce-8745 Oct 01 '26
Would using something like Beekeeper Studio give you a better experience vs Claude
2
u/JaceBearelen Oct 01 '26
I really like dbt as the semantic layer for AI. Everything in the database is either a source or a model and everything is annotated down to the columns. Everything is just right there in structured text for the LLM. Then give it a way to analyze the data while it builds out the query and you’re golden. Everyone in my org is consistently impressed with how accurate it is.
2
u/KatFromSisense Oct 01 '26
If the app enum is where those meanings are defined, I'd wire that into deploy rather than keep a second dictionary by hand. Generate or update the lookup from the enum and fail CI if the two ever disagree. Then point the MCP role only at the decoded view. Somebody adding status 7 shouldn't quietly create a new mystery value for Claude.
2
u/ArielCoding Oct 01 '26
Add a status lookup table and give the agent a view that shows refunded instead of 4, so it never sees the raw codes, then generate that table from your app code and fail CI if they ever differ.
83
u/projexion_reflexion Oct 01 '26
It would be crazy to add a status codes table with names, right?
3
u/uncertainschrodinger Oct 01 '26
Short answer is to build a context layer (in-code docs, glossary/dictionary, semantic layer, etc.)
Longer answer: you need to build processes that makes this a "self-healing" loop so that when you correct the agent, it will automatically update the context
This is still hard when it is up to 1 person, so it's better if you can roll it out to some trusted superusers who can flag such cases to be fixed
I always compare this to hiring a new person and onboarding them - through dialogue and back n forth, you brush layers of knowledge on to their empty canvas and hope that they will one day have all the knowledge in their head as well.
1
u/InsideChipmunk5970 Oct 01 '26
look into property graphs on 19.
1
u/Significant_Tune9219 Oct 02 '26
Read-only stops writes; it does not stop wrong semantics. Codes like status_cd need a data dictionary or constrained enum view in the context the model sees, plus a rule that answers cite where the definition came from. I'd also force it to show the distinct values and counts it used before narrating what a code "means."
1
u/TsmPreacher Oct 01 '26
Give it access to your git repo so it can check these things first. Works for me.
1
u/Simma451 Oct 02 '26
Move to Oracle and use anotations to describe tables & columns 🤷♂️
1
u/RajeshRocketMan Oct 02 '26
so you're basically choosing between keeping metadata inside the db or building an external doc layer, but putting comments directly on the columns is usually the sane route. An external markdown dictionary is gonna get outdated in like a week and views are just more boilerplate to maintain. Postgres supports column comments natively. and we do the exact same thing on mariadb — just dump all that context into information_schema so the db remains the single source of truth and agents don't have to guess what a random int means.
1
u/False_Assumption_972 Oct 02 '26
Column comments plus a view layer is the cheapest durable fix. Create views with the enums decoded, like order_status as text, grant the MCP role access only to those views, and comment the view columns so the meaning travels with the data. Then build a test set of ten questions with known answers, including the lost orders one, and rerun it whenever the schema or prompt changes.
The confident wrong number is the real risk, so the test set matters more than the docs. Keeping field meanings attached to the records an answer came from is something we use SIGNLD for.
1
u/ysth Oct 02 '26
Sounds like it needs access to your code too.
1
u/SnooHesitations9295 Oct 03 '26
5. Give agent the source code. That's the only working solution.
1
u/deadatreides1 Oct 07 '26
The answer was sitting in the app repo the whole time, in that enum. An agent that can see the tables but not the code will guess, and guess confidently. A status table or a semantic layer is the real fix; the quick one is giving it the code next to the database. PTG-MEM indexes a repo into cards an MCP client can query, so "what does status_cd 4 mean" lands on the enum instead of on vibes.
0
u/Jhon_ST Oct 01 '26
I'd test whether that context is actually reaching the model before spending too much time deciding where the dictionary should live.
Make a tiny fixture with one cancelled order, one refunded order, and an unmapped status code. Then run the same questions with and without the mapping and keep the expected answers outside the model: cancelled and refunded must stay distinct, and the unmapped value should remain unknown rather than being guessed.
If your MCP/client exposes the tool result or trace, inspect that too. That gives you a regression test for both the context plumbing and whether the model actually uses the context, instead of relying only on a prompt saying "don't guess."
There's another ambiguity even after fixing 4 vs 6: what does "orders we lost" actually mean? Cancelled orders, refunded orders, or both? And is "last month" based on order date or when the status changed?
Those are business metric definitions, not something the database schema can establish by itself. I'd want the agent to ask for that definition rather than silently choosing one.
0
u/Hour-Measurement-835 Oct 01 '26
I'd stop trying to get it to say it doesn't know and take the integer away instead. Put the labels in a status_codes table and build a view over orders with COALESCE(s.label, 'unmapped ' || o.status_cd). Give the MCP role SELECT on that view and nothing on orders. A code someone adds next year then comes back as "unmapped 7" instead of a confident wrong label. Don't skip the revoke, if it can still read orders it'll find status_cd and start guessing again.
0
u/WorriedMeat Oct 01 '26
“Without me turning into a full time documentation service” is the wrong mindset for a data owner. Everyone’s lives are easier if documentation is codified. Especially with AI
0
u/Fancy_Letterhead_509 Oct 01 '26
just put comments on the columns and a view layer on top, the markdown doc will be outdated in 2 weeks
0
u/TemporaryDisastrous Oct 01 '26
We use agents over our datamarts and give them skills so they know how to use it and they pretty much get it right. It's a bit scary it just presents that as a right answer though. Maybe you need to initialise it to flag when it doesn't know what codes refer to.
3
u/[deleted] Oct 01 '26
[removed] — view removed comment
0
u/[deleted] Oct 01 '26
[removed] — view removed comment