r/SQL • u/FamiliarSlide7685 • Sep 07 '26
Discussion we have is_active and active_flag on the same table and they disagree on 3% of rows
Postgres 15. Same 60 column table I've been posting about, I promise I'll stop eventually.
is_active, boolean. active_flag, smallint. Both populated, both written by something, and they disagree on about 3% of rows. Nobody knows which one the app respects. I spent an afternoon on it and my best finding is that the disagreement rate has been slowly climbing since a 2021 migration, which tells me one of them stopped being maintained and not which one.
This is worse than the columns nobody can explain. flag_2 at least announces itself as a mystery, so nobody builds on it. These two both look like answers. Someone joined in July and spent a week working out which of three status-ish columns actually mattered, and picked wrong first.
What I've tried: grepping for both, which gives me hits in dbt, the app repo and a commented-out block in an old migration. Checking which one the ETL writes, which is both. Asking the two people who've been here longest, who gave me different answers with the same amount of confidence.
what I think I should do is pick the one the app actually reads at runtime, make the other a generated column off it, and let anything that disagrees break loudly. what stops me is that I can't prove which one the app reads without reading the whole app, and the reads I'm worried about aren't in the app anyway, they're in Metabase questions owned by analysts.
so:
- when two columns claim the same thing and disagree, is there anything better than picking one and waiting for screaming
- has anyone made the wrong one a generated column or a view over the right one, and did it actually stop the divergence or just move it
- more generally, do you have any way of catching a column whose meaning drifted while its name and type stayed the same. that's the failure I can't monitor for and it's the one that's actually happened here
43
u/feignapathy Sep 07 '26
Are you unable to send a list of records to the business owners or data data stewards and ask them to audit the records?
If you have 10 recent records with conflicting information, ask them to verify if the records are active or not. That will tell you which column is the source of truth for
activeor not.15
u/Holiday-Tip-3720 Sep 07 '26
pull the 3 percent and have the business teams confirm. or in the 3% verify duplication. the issues ive seen in the past were due to duplicates and we were able to track down where we were getting false logic in the ETL. it was a where that needed a parens statement btw.
8
u/Blues2112 Sep 07 '26
Is it possible that the two flags could represent different things? Is_Active perhaps referring to the account itself, while active_flag denoting status at the transaction level, or something?
If AI isn't helping, and the Data Stewards are giving conflicting/confusing info, it just sounds like you're gonna need to dig DEEP into the flow of the code until you find sourcing for each column, and then backtrace from there.
5
u/kagato87 MS SQL Sep 07 '26
Relational databases are supposed to prevent this... Single source of truth and all that...
You need to find out from the source. As in the source code of the application. One of those fields should be depreciated and removed, then you can remove the other.
Otherwise, yea, wait for the complaints.
We actually have a few meters like this - a two different clocks/counters, but they have an enum column to say which one it is. (Which is also something to look for - is there something else that dictates which flag is truth and which is for when it becomes suspect?)
3
u/tehdlp Sep 07 '26
Ideally the database has limited direct access, so you or someone else can verify the actual usage whether through an ORM, SQL statements in strings, etc, because there's only one or two applications querying and everything else is abstracted above that.
If you literally can't tell what the hell is reading from your tables at an org level, you are screwed and should look for other things to focus on. As someone who is screwed, I'm sure there's plenty of others who could join our club.
6
u/rbobby Sep 07 '26
Lightweight.
Ok you're gonna have to get out of my database.
But more seriously does you app have unit tests and a test database? Drop one of the columns and run the tests. That will give you a bit of sizing info at least.
2
u/NoEggs2025 Sep 07 '26
Assuming it’s a staging. It might be attributes from the previous tables maybe even from the source/provider. Needs to be labeled as such. And the contradicting flags need to be properly labeled too.
2
u/git0ffmylawnm8 Sep 07 '26
Check upstream pipelines to see how the fields are populated.
2
u/SkullLeader Sep 07 '26
Assuming that one column or the other is entirely correct (is that even true in this case?), sounds to me like whichever one the app reads is the correct one. Assuming people use the app and no one screams.
And I'm not that familiar with Postgres but in SQL server you (or someone with DBA access at least) can monitor in real time what queries are being run, figure out which ones are coming from the app and see what the actual query is.
> without reading the whole app
What is so bad about doing this? Unless the app is just spaghetti code, it shouldn't be too difficult. Worst case, this is a great use case for AI. Give it the source code for the app and ask it to analyze which of these two columns are being used.
2
u/SantaCruzHostel Sep 07 '26
Can a column have a foreign key to the same table? I'm half joking, but this sounds (high level) straightforward.
2
u/scbywrx Sep 09 '26
If you're in DBT, find the lineage, find out where everything starts, and backtrack it from there, and then go back to the business owners. I know you said that nobody has any idea, but maybe this is a good time to revisit what these columns are supposed to mean. One may actually need to be deprecated. Find out and trace all of the things backwards.
One of the neatest things is the new DBT MCP pieces that you can work in with Claude and/or Cursor, for that matter, any tool that you want to play with. Use the lineage, and then it's very simple if you're using this in DBT: trace it and then have it write up a document that tells where these columns come from and where it was.
You may even have the capability of going back through time through the Git history and being able to go through and trace any of the Git commits to find out which one is now current and which one is not. Beyond that, once you get the trace, that's the best part of the data engineer's job. You hand it back to whoever is the data steward at the time, because data engineers may know the system, may know the business, but they shouldn't have ownership of the large calculations. Your data stewards should, so the governance body should then be able to make a decision on which one to keep and/or combine, collapse, or restate what is active.
Try that. Let us know what you get and how they react when you hand them something that they have to make a decision on.
1
u/Enigma1984 Sep 07 '26
Is it actually a Type 2 table or are these columns just being written from somewhere else?
1
u/Green_Chamomile Sep 07 '26
Your Metabase problem is greppable, people just forget it. Metabase stores every saved question in its own application database, table report_card, with the query in the dataset_query column. One query against the Metabase app DB tells you every saved question that mentions is_active or active_flag, with owner names attached. If you can't touch the app DB, the search API gets you most of the way too.
1
u/mikeblas Sep 08 '26
That's the problem, really. This can be figured out by looking at code. If your team / organization can't read its own code, then ... ?
1
u/metric_skeptic Sep 08 '26
This is exactly the kind of problem that looks like a SQL problem but really isn’t.
If two fields represent the same business concept but disagree on 3% of rows, I’d be hesitant to simply pick whichever one the app currently reads.
I’d first try to establish the ownership and lineage:
Which system is the source of truth?
Which field is written by the application vs. ETL?
Which one is used in production logic?
When did the divergence start?
Are the 3% concentrated in particular dates, entities, or migration cohorts?
The fact that the disagreement has been slowly increasing since the 2021 migration is especially interesting. That sounds less like random data quality noise and more like two definitions slowly drifting apart.
And I really like the idea of making the disagreement fail loudly once the source of truth is established.
A duplicated/generated field is only safe if you can continuously prove that it still agrees with the canonical one.
2
u/anon586346 Sep 10 '26
You won’t find out from just looking at the data. You need to trace this back to the migrations and business processes that created them and hope there’s some clues there.
-2
u/IQ4EQ Sep 07 '26
Can’t AI read the codebase and give you some hints?
-3
u/PTcrewser Sep 07 '26
Have you asked Ai?