fix(kyc): enforce unique constraint on nullable columns#3768
Open
davidleomay wants to merge 3 commits into
Open
fix(kyc): enforce unique constraint on nullable columns#3768davidleomay wants to merge 3 commits into
davidleomay wants to merge 3 commits into
Conversation
…nd user_data indexes PostgreSQL treats NULL != NULL in unique indexes, so the composite unique index on kyc_step (userData, name, type, sequenceNumber) silently allowed duplicate FinancialData steps when type was NULL. Similarly, the user_data unique index was missing a NULL filter on the nationality column. - Add NULLS NOT DISTINCT to kyc_step unique index (PG 15+) - Add nationalityId IS NOT NULL to user_data unique index WHERE clause
Member
Author
|
CLEAN UP DATA BEFORE MERGING!! |
Yannick1712
approved these changes
May 27, 2026
Member
Author
Duplicate check results (production)kyc_step83 duplicate groups, 298 total rows — all with
Find all conflicting rows: SELECT ks.id, ks."userDataId", ks.name, ks.type, ks."sequenceNumber"
FROM kyc_step ks
WHERE EXISTS (
SELECT 1 FROM kyc_step ks2
WHERE ks2.id != ks.id
AND ks2."userDataId" = ks."userDataId"
AND ks2.name = ks.name
AND COALESCE(ks2.type, '__NULL__') = COALESCE(ks.type, '__NULL__')
AND ks2."sequenceNumber" = ks."sequenceNumber"
)
ORDER BY ks."userDataId", ks.name, ks."sequenceNumber", ks.id;Delete duplicates (keep the row with the lowest id per group): DELETE FROM kyc_step
WHERE id NOT IN (
SELECT MIN(id)
FROM kyc_step
GROUP BY "userDataId", name, COALESCE(type, '__NULL__'), "sequenceNumber"
);user_dataNo duplicates found — migration is safe to run on this index. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NULL != NULLin unique indexes, so the composite unique index onkyc_step(userData,name,type,sequenceNumber) silently allowed duplicate steps whentypewasNULL(e.g. FinancialData). Concurrent requests could create multiple FinancialData steps with the same sequence number.user_dataunique index on (identDocumentId,nationality,accountType,kycType) was missing aNULLfilter onnationality, allowing the same bypass.NULLS NOT DISTINCT(PG 15+) to thekyc_stepindex andnationalityId IS NOT NULLto theuser_dataindex WHERE clause.Test plan
NULL-type inserts intokyc_stepare now correctly blocked