Why the order of filtering and deduplication matters more than it first appears.
Deduplication sounds straightforward: find equivalent records, keep one, discard the rest.
But there's a subtle failure mode. Deduplicating a dataset before applying a filter can make valid results disappear.
I encountered this problem while designing the public presentation layer of a news intelligence pipeline.
The disappearing-record problem
Imagine two copies of the same story:
| Record | Published | Status |
|---|---|---|
| A | Monday | Original |
| B | Wednesday | Duplicate |
A user requests stories published on Wednesday.
If we deduplicate globally first, the system might retain Monday's original and suppress Wednesday's duplicate. The Wednesday filter then removes Monday's record.
Result: zero articles.
But Wednesday did contain a matching article. The system discarded the only record eligible for the user's query.
The database isn't missing information. The query applied two individually reasonable operations in the wrong order.
Filter first. Deduplicate second.
The solution depends on the intended semantics of the application.
For our public feed, deduplication is a presentation decision—not an instruction to delete underlying database rows.
The relevant implementation, build_deduplicated_article_subquery(), takes an already-filtered SQL query and performs deduplication within that result set.
This matters because the representative record must be chosen from the documents the user is actually allowed to see.
Our implementation uses PostgreSQL window functions to handle two cases:
First, story-level duplicates. If a story has a non-duplicate member inside the filtered result, records explicitly marked as exact duplicates are suppressed. If the window contains only marked duplicates, one is retained. The newest eligible record is preferred, with article ID breaking ties between identical publication timestamps.
Second, repeated source/title combinations. Remaining rows are grouped by source and title, normalized for case and whitespace. ROW_NUMBER() selects one representative per group, with publication time and article ID determining the ordering.
The underlying articles remain intact.
Why this is more than a news-feed problem
The same issue can appear in customer dashboards, event histories, search interfaces, and analytics systems.
A canonical record selected across an entire database is not necessarily an appropriate representative for every filtered view.
There's an important distinction between two requirements:
- Global uniqueness: Show only one canonical record, regardless of the requested window.
- Query-local uniqueness: Show one representative among records satisfying the current filter.
Neither is universally correct. The application must choose deliberately.
For our feed, query-local deduplication preserves the visibility of relevant records without exposing unnecessary duplicates.
The engineering lesson
The solution wasn't a more sophisticated clustering model or a larger infrastructure component.
It was recognizing that operation order is part of data correctness.
Filtering determines which records are eligible. Deduplication determines which eligible records should be displayed.
Reverse those responsibilities, and a perfectly valid database can produce an incorrect answer.
Implementation note: Based on the query-local deduplication logic in app/article_duplicates.py.