Skip to content
mentoor

Topic 5 of 5 · 6 min read

Modeling tenants, history and soft deletes

Goal: Choose a tenant and history design for a SaaS table and list the queries that must change because of it.

Decide who owns a row, how its past is kept and what "deleted" means before the first customer signs up, because each choice touches every query you write afterwards.

Check yourself · 1 of 3

In a shared table for many customers, which UNIQUE constraint fits a contact's email?

Choose one answer

Why it matters

In a shared SaaS database one missing WHERE clause shows one customer another customer's data. That is the most serious bug a data model can have. History and deletion are the same kind of decision: they change the meaning of every read.

How to do it

Tenants.

  1. Put tenant_id bigint NOT NULL REFERENCES tenants (id) on every tenant-owned table, including child tables, so no query needs a join to find the owner.
  2. Make uniqueness per tenant: UNIQUE (tenant_id, email), not UNIQUE (email).
  3. Enforce it in the database with row-level security. Enable it on the table, create a policy such as USING (tenant_id = current_setting('app.tenant_id')::bigint), and set that setting at the start of each request. Note that a table owner or a superuser bypasses policies unless you force them, so connect the app as a limited role.

History. 4. Pick the lightest tool that answers the real question. "Who changed this, and when" needs an audit table of (table, row_id, changed_by, changed_at, old, new). "What did this look like last March" needs a history table or valid-from and valid-to columns.

Soft deletes. 5. A deleted_at timestamptz column hides a row instead of removing it. Every read must add deleted_at IS NULL. Put it in a view or a partial unique index (UNIQUE (tenant_id, email) WHERE deleted_at IS NULL) so a deleted email can be reused. 6. Decide the real deletion policy: privacy laws may require true removal, so plan a purge job.

Before and after

-- Weak: one global email, no owner, deletes erase history
CREATE TABLE contacts (
  id serial PRIMARY KEY,
  email text UNIQUE,
  name text
);

-- Strong
CREATE TABLE contacts (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  tenant_id bigint NOT NULL REFERENCES tenants (id),
  email text NOT NULL,
  name text NOT NULL,
  deleted_at timestamptz
);
CREATE UNIQUE INDEX contacts_email_live
  ON contacts (tenant_id, email) WHERE deleted_at IS NULL;
ALTER TABLE contacts ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON contacts
  USING (tenant_id = current_setting('app.tenant_id')::bigint);

What goes wrong

  • One forgotten filter. A new report query omits tenant_id or deleted_at IS NULL and leaks or resurrects rows.
  • Global unique keys. Two tenants cannot both have admin@example.com.
  • Foreign keys to soft-deleted rows. An order points at a deleted product and the page breaks.
  • Policies that never apply. The app connects as the table owner, so row security is silently skipped.

Examples

The leaked report

A developer adds a new dashboard query: SELECT count(*) FROM invoices WHERE status = 'open'. It counts every tenant's invoices. With row-level security on, the same query returns only the current tenant's rows.

Reusing a deleted email

A user is soft-deleted and signs up again with the same email. A plain UNIQUE (tenant_id, email) rejects it. The partial index WHERE deleted_at IS NULL allows it and still blocks two live accounts.

Key points

  • Put tenant_id on every tenant-owned table, and make unique keys per tenant.
  • Row-level security enforces tenant isolation in the database, but owners and superusers bypass it unless the app uses a limited role.
  • Choose an audit table for who changed what, and a history table for what a row looked like at a past date.
  • A soft-delete flag must be added to every read, so hide it behind a view or use a partial unique index.
  • Decide the real deletion and purge policy early, because hidden rows are still stored data.

Common mistakes

  • Relying on every developer to remember a tenant filter in every query.
  • Defining a global UNIQUE on a field that is only unique within one tenant.
  • Adding deleted_at without updating existing queries, unique indexes and foreign-key targets.
  • Keeping soft-deleted personal data forever without a purge plan.

Check yourself · 2 of 3

What does a soft delete require of every read query on the table?

Choose one answer

Check yourself · 3 of 3

The app connects as the owner of a table with row-level security enabled, and tenants still see each other's rows. Why?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic