Who is this row about? Get identity right before building the pipeline.
Primary keys, grain, and data contracts look administrative until an order loses half its contents.
Start with the sentence, not the connector.
A store has order O-01 with coffee worth Rp40,000 and bread worth Rp60,000, followed by order O-02 with tea worth Rp80,000. If one row represents one order item, order_id alone is not unique. In this example, the identity is the pair order_id and line_id. The sentence “one row represents one item in one order” makes the rule visible. Without it, three teams may treat the same row as an order, a payment, or an item. Everyone feels right. The meeting becomes a debate sport.
PostgreSQL distinguishes primary keys, unique constraints, not-null constraints, and foreign keys. A constraint enforces a rule the database can check. It does not automatically understand business meaning. A key can be technically unique and still represent the wrong concept. Write down what a row means before choosing its key columns.
A contract needs to explain meaning.
A contract for the store example defines item identity, amount type, currency, event time, and allowed statuses. Add the source owner, schema version, and treatment for unknown values. Rp40,000 is different from 40,000 cents. A timestamp without a timezone can also lead two teams to group dates differently. “The amount column exists” has not answered those questions.
Not every violation needs to block the whole pipeline. An empty ID might go to quarantine; a new promotion code might be handled as unknown. Choose based on impact. The important part is that counts and examples of bad records stay visible. Do not quietly discard them just to keep the dashboard looking healthy.
A change in meaning can be more dangerous than a change in type.
Changing amount from before-discount to after-discount does not have to change the schema, but it breaks historical comparisons. Prepare before-and-after examples, affected consumers, and a compatibility window. A column keeping its name is not proof that its meaning stayed the same.
For a small team, one contract document and one change test can be more useful than a new platform with no owner. Store the contract version in batch metadata. Test the new version against a separate output before consumers switch over. A little coordination costs less than three dashboards that are all convinced they are right.
Deduplication is not just DISTINCT.
Two payments for the same amount are not necessarily duplicates. Two deliveries of the same event ID may represent one event. Check whether the ID stays stable on retry, whether a record can be updated, and how versions are decided. DISTINCT makes identical representations look the same. It does not solve business identity.
In the visual, a weak key lets the Rp40,000 coffee row get overwritten by the Rp60,000 bread row. The total falls from Rp180,000 to Rp140,000. This is not a network packet going missing. The identity model is wrong. More expensive infrastructure will not bring back forty thousand rupiah that the model misidentified.
How to check the foundations.
Ask the team: What does this row represent? When do two rows count as the same event? Who is allowed to change that definition? Test cancellations, refunds, and empty IDs. An answer that only works when every transaction succeeds is not enough.
Monitor duplicate-key rate, quarantined rows, and changes in key distribution. Set thresholds according to consumer needs, not a magic number. Keep replayable failure examples. Good tests help you recognize a similar problem again. They do not force every indicator to zero by hiding the evidence.
An example to inspect.
-- PostgreSQL, an order-item grain example
CREATE TABLE order_items (
order_id text NOT NULL,
line_id integer NOT NULL,
amount_idr numeric(14,2) NOT NULL CHECK (amount_idr >= 0),
PRIMARY KEY (order_id, line_id)
);
Two rows have the same order_id but different line_id values. Can one be discarded as a duplicate?
Not based on order_id alone. At the order-item grain, both may be valid items. Check the key pair and the source contract.
Keep these three things.
- Write down what one row represents.
- Test empty IDs, retries, and version changes.
- Assign a contract owner and a quarantine path.
Sources, not slogans.
Primary references reviewed on 7 November 2026. The explanation, examples, and visual were prepared by RapiApp. The diagram simplifies the topic for learning. The article explains its assumptions and limits.
- PostgreSQL: Constraints
- Kimball: Grain
Continue the foundations.
- Who is this row about? Get identity right before building the pipeline.
- The pipeline ran twice. Why did revenue double too?
- The data arrived late. That does not mean the event just happened.