
SaaS Unique Constraints: Define Business Identity Before Preventing Duplicate Records
Define tenant-scoped business identity, normalization, null handling, and conflict recovery before adding database uniqueness rules.
MVPs & SaaS Projects
Make PostgreSQL exclusion queries explicit about null values, tenant scope, and the business meaning of an absent match.

LET’S BUILD SOMETHING USEFUL
Talk to ATARAXY DEVELOPERS (SMC-PRIVATE) LIMITED about your website, app, or custom software.
A SaaS dashboard lists accounts that have not been placed on a temporary exclusion list. The query works until an imported exclusion row contains a missing account identifier. Suddenly the dashboard appears empty. The application has not deleted the accounts, but a Boolean assumption in the query has changed which rows qualify.
This is a classic place where SQL’s handling of NULL becomes a product issue. An exclusion query should express the intended business rule for known identifiers, missing identifiers, and incomplete evidence. Reviewing that rule is more useful than mechanically replacing one SQL keyword with another and hoping that the result now matches what the business meant.
Create a small fixture with account identifiers 10, 20, and 30. Put 20 and a null value in the exclusion input. Ask which accounts the business expects to see. Most teams will say 10 and 30, but they should also explain what the incomplete exclusion row means and whether it should block the operation, be ignored, or enter a review queue.
The PostgreSQL subquery documentation explains that NOT IN can evaluate to null when no equal value is found and a right-hand value is null. A WHERE condition retains rows that satisfy its condition, so unknown is not the same as true. The resulting empty-looking report is a logical outcome, not evidence that the database lost records.
An account without an exclusion record is different from an account with an exclusion record whose identifier could not be resolved. Decide whether the latter is a data-quality error, a provisional restriction, or an import row that must not participate until reviewed. Keep that decision visible in the ingestion and reporting design.
For a billing workflow, a missing identifier may justify stopping a batch for review. For a noncritical recommendation list, the team may choose to quarantine the bad row and proceed with the valid records. These choices should not depend on the incidental behavior of NOT IN. A documented policy gives the application a reasoned result and a useful error message.
A correlated NOT EXISTS can express that no matching exclusion row exists. A simplified query might read SELECT a.id FROM accounts a WHERE NOT EXISTS (SELECT 1 FROM exclusions e WHERE e.account_id = a.id). This describes equality-based absence. Review it against the fixture, including any nullable account identifier allowed by the schema.
Filtering nulls out of the subquery can be appropriate when the policy explicitly ignores unresolved exclusion rows. It is not a universal repair: the application may instead need to reject the input. Keep the data-quality decision separate from the query expression so that future developers do not interpret a null filter as permission to discard every incomplete business rule.
Ordinary equality and null-aware comparison answer different questions. PostgreSQL’s comparison documentation describes IS NULL and IS DISTINCT FROM, including null-aware comparison behavior. Choose the operator according to the domain. Two missing external identifiers do not necessarily identify the same customer simply because both are absent.
Suppose an import lacks a supplier reference on two rows. Treating those null references as equal could incorrectly connect unrelated records. In another workflow, a nullable configuration value may deliberately use null as the default state. Write examples for both interpretations before deciding how a join, deduplication rule, or exclusion condition should handle the missing value.
An exclusion query in a multi-tenant application must compare records within the intended tenant or organization. A correct null treatment does not fix a missing tenant predicate. Include the relevant scope in the correlation or use the application’s established authorization and row-security design, then verify it through the actual execution role.
Also define whether expired, deleted, draft, or superseded exclusions count. An account may have an old exclusion that no longer applies. If the query merely asks whether any historical record exists, the account can remain hidden indefinitely. Pair the identity relationship with the business state and effective interval that determine current eligibility.
Check required identifiers at the import boundary and distinguish blank text, invalid text, unresolved references, and SQL null. Preserve the original row reference for investigation. A useful import summary reports accepted, rejected, and unresolved rows rather than quietly converting every problem into a null identifier.
Where the business contract requires a known account, use schema constraints that enforce that requirement. A NOT NULL rule can prevent a class of bad data, but it should follow the chosen model rather than suppressing legitimate uncertainty. If unresolved records are allowed, store them in a state that cannot accidentally masquerade as an active, fully identified exclusion.
An exclusion test should assert which accounts appear and why. Include an empty exclusion set, one known match, one nonmatch, a null in the exclusion input, duplicated exclusions, and an unresolved account reference. Add expired exclusions and a second tenant with similar identifiers. A count of two is insufficient if the wrong two accounts are shown.
Review the displayed explanation too. If the system hides an account, support should be able to identify the current rule responsible. If the batch stops because exclusion data is incomplete, expose that specific condition to the authorized operator. Do not present a blank results screen as a successful search when the system could not safely determine eligibility.
Once the query returns the intended records, inspect its plan on representative data and verify appropriate indexes for the scope and correlation. Avoid assuming that one syntax is always faster. Distribution, table size, constraints, and the surrounding query can affect the actual plan and workload.
Use a realistic mix of active and historical exclusions, including tenants of different sizes. Measure the path the application really executes, with its actual filters and limits. A fast isolated example is not enough if the production query combines eligibility with joins, ordering, and pagination that change the work required.
AI assistance can help produce fixtures or explain an unfamiliar query, but a generated rewrite must be checked against the domain examples. Ask which null cases change behavior and which scopes the rewrite assumes. Keep the approved business rule alongside the query review so that a plausible SQL explanation cannot substitute for a product decision.
For a rollout, compare old and new result membership on a controlled snapshot. Investigate differences before changing the customer-visible list, and retain enough diagnostic detail to explain expected corrections. This makes the deployment a reviewable behavior change rather than a silent shift in eligibility.
PostgreSQL exclusion queries need an explicit policy for missing values, matching records, and current scope. Reproduce null cases with small fixtures, select the expression that matches the intended rule, and verify record membership before optimizing performance. A correct result should be explainable to both the database reviewer and the person relying on the dashboard.
LET’S BUILD SOMETHING USEFUL
Talk to ATARAXY DEVELOPERS (SMC-PRIVATE) LIMITED about your website, app, or custom software.