Disclosure: I build Schemity, a desktop ERD tool - this post is from our blog and uses it for the examples.

TL;DR: Use a deleted_at column on a table other tables point at, such as products or users, and make those foreign keys reference the surrogate id. Use an archive table for rows nothing references, where you want them physically out of the live table. Both fail in ways PostgreSQL will not warn you about: a soft-deleted row still accepts new references, and an archive copy written with SELECT * lands in the wrong columns after a schema change.

A soft delete column and an archive table fail in opposite places, so the choice is made per table by one question: does another table reference these rows? If it does, keep the rows where they are with a deleted_at column and point every foreign key at the surrogate id. If nothing references them, move them to an archive table and keep the live table plain. Get it backwards and you either hit a refused delete that tempts someone to drop the foreign key, or an archive that drifts away from the live table.

Everything below ran on PostgreSQL 18.3 in a throwaway container. Each section starts from a fresh copy of this shop, where order lines reference products by SKU, a natural key many schemas use (which key a foreign key should reference is its own decision):

CREATE TABLE products ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, sku text NOT NULL UNIQUE, name text NOT NULL ); CREATE TABLE order_items ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, order_id bigint NOT NULL, sku text NOT NULL REFERENCES products (sku), quantity int NOT NULL ); INSERT INTO products (sku, name) VALUES ('MUG-01', 'Blue mug'), ('MUG-02', 'Red mug'); INSERT INTO order_items (order_id, sku, quantity) VALUES (1, 'MUG-01', 2); 

What is the difference between a soft delete and an archive table?

A soft delete marks the row and leaves it in the table; an archive table moves the row into a second table and deletes it from the first.

Soft delete (deleted_at) Archive table (products_archive)
Deleting UPDATE products SET deleted_at = now() DELETE from products, INSERT into products_archive
Rows other tables reference Stay valid The delete is refused (default NO ACTION)
A new reference to a deleted row Accepted Refused
Reusing a unique value (a SKU, an email) Needs a partial unique index Works as before
Every query on the live table Needs WHERE deleted_at IS NULL, or a view that adds it Unchanged
A column added to the live table Nothing else to change Must be added to the archive, and every move must name its columns
Restoring One UPDATE, unless the unique value was reused Insert back with the old id, unless the unique value was reused

Each row is measured below, and each one has a trap behind it.

What goes wrong with a soft delete column?

The database keeps enforcing everything on a soft-deleted row as if it were live, because to PostgreSQL it is live.

Starting from the shop above:

ALTER TABLE products ADD COLUMN deleted_at timestamptz; UPDATE products SET deleted_at = now() WHERE sku = 'MUG-01'; INSERT INTO order_items (order_id, sku, quantity) VALUES (2, 'MUG-01', 1); 

A deleted product still takes new orders. The last INSERT succeeded. A foreign key checks that the row exists, not that it is live, and deleted_at means nothing to it. Only the application or a trigger stands between a deleted product and a new order line.

Deleting a parent does not delete its children. Soft-deleting a product leaves every row that references it live. Whatever ON DELETE CASCADE would have done, the application now has to do itself.

The unique value cannot come back. Re-adding a product under the old SKU, INSERT INTO products (sku, name) VALUES ('MUG-01', 'Blue mug v2'), failed with duplicate key value violates unique constraint "products_sku_key", because the soft-deleted row still holds it. The standard fix is a partial unique index, unique among live rows only:

ALTER TABLE products DROP CONSTRAINT products_sku_key; CREATE UNIQUE INDEX products_sku_live ON products (sku) WHERE deleted_at IS NULL; 

The first statement is refused while anything references the SKU. PostgreSQL answered cannot drop constraint products_sku_key on table products because other objects depend on it, with the detail constraint order_items_sku_fkey on table order_items depends on index products_sku_key. Its hint is Use DROP ... CASCADE, and that is the trap: ALTER TABLE products DROP CONSTRAINT products_sku_key CASCADE ran, printed one NOTICE, and dropped order_items_sku_fkey. Order lines now reference products by a value nothing enforces.

The foreign key cannot be put back. With the partial index created, ALTER TABLE order_items ADD FOREIGN KEY (sku) REFERENCES products (sku) failed with there is no unique constraint matching given keys for referenced table "products". The PostgreSQL manual says a foreign key must reference a primary key, a unique constraint, "or are columns from a non-partial unique index". A partial index never qualifies.

Then the history doubles. With the product re-added under its old SKU, the order history query returned every old order line twice:

INSERT INTO products (sku, name) VALUES ('MUG-01', 'Blue mug v2'); SELECT oi.order_id, oi.sku, p.name, p.deleted_at IS NOT NULL AS deleted FROM order_items oi JOIN products p ON p.sku = oi.sku ORDER BY 1, 3; 
 order_id | sku | name | deleted ----------+--------+-------------+--------- 1 | MUG-01 | Blue mug | t 1 | MUG-01 | Blue mug v2 | f 2 | MUG-01 | Blue mug | t 2 | MUG-01 | Blue mug v2 | f 

None of this happens when order_items references products (id). The surrogate key is never reused, the partial index on sku is only a uniqueness rule, and the history join stays one row per line. A natural key you plan to reuse after a soft delete cannot also be what foreign keys point at.

Restoring can collide too. Once the SKU has been reused, UPDATE products SET deleted_at = NULL on the old row fails on products_sku_live with a duplicate key, as an archive restore does below.

What goes wrong with an archive table?

An archive table works only for rows nothing references, and it drifts away from the live table unless every move names its columns.

This section's shop keeps products without deleted_at, references them by id, and moves deleted rows into an archive made with LIKE:

CREATE TABLE products ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, sku text NOT NULL UNIQUE, name text NOT NULL ); CREATE TABLE order_items ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, order_id bigint NOT NULL, product_id bigint NOT NULL REFERENCES products (id), quantity int NOT NULL ); CREATE TABLE products_archive ( LIKE products, archived_at timestamptz NOT NULL DEFAULT now() ); INSERT INTO products (sku, name) VALUES ('MUG-01', 'Blue mug'), ('MUG-02', 'Red mug'); INSERT INTO order_items (order_id, product_id, quantity) VALUES (1, 1, 2); WITH moved AS (DELETE FROM products WHERE sku = 'MUG-01' RETURNING *) INSERT INTO products_archive SELECT * FROM moved; 

A product that was ever ordered cannot be archived. The move failed with update or delete on table "products" violates foreign key constraint "order_items_product_id_fkey" on table "order_items". Order history is exactly what keeps a product referenced, so the rows you most want gone from the live table are the ones the archive cannot take. That is the default NO ACTION key. Declared ON DELETE CASCADE, the same move would delete the order lines with the product, and ON DELETE SET NULL would leave them pointing at nothing; neither keeps the history.

A column added later goes into the wrong place without an error. MUG-02 was never ordered, so it moved cleanly. After ALTER TABLE products ADD COLUMN launched_at timestamptz, the same SELECT * move archived a new product and stored its launch date, 2024-03-01 00:00:00+00, in archived_at. SELECT * matches columns by position, the new column took the position archived_at had, and the types matched, so PostgreSQL had nothing to refuse. When the added column was an int, the move failed instead with column "archived_at" is of type timestamp with time zone but expression is of type integer. Adding the column to the archive does not fix SELECT *: ALTER TABLE products_archive ADD COLUMN launched_at puts it after archived_at, so positions still disagree. Name the columns in every move.

LIKE copies less than it looks. The CREATE TABLE reference says LIKE copies "column names, their data types, and their not-null constraints", and nothing else without INCLUDING DEFAULTS, INCLUDING INDEXES and the rest. It also says the two tables "are completely decoupled after creation is complete", so no later change reaches the archive.

Restoring needs the old id and the old unique values. Inserting an archived row back failed with cannot insert a non-DEFAULT value into column "id" until the statement said OVERRIDING SYSTEM VALUE, which a GENERATED ALWAYS identity column requires. Then it failed again on products_sku_key, because the SKU had been reused in the meantime.

Soft delete vs archive table: which should you use?

Decide per table by what references it: soft delete for rows other tables point at, an archive table for rows nothing points at.

The table holds Use Because
Rows other tables reference (products, users, accounts) Soft delete, referenced by id History keeps its foreign keys; an archive move is refused
Rows nothing references (comments, drafts, notifications) Archive table The live table stays plain, with no filter on every query
Rows you restore often Soft delete Restore is one UPDATE unless a unique value was reused
Rows you must remove for good after a retention period Archive table, then delete from it Retention is a DELETE on one table nobody joins to

Rows that are referenced and also carry personal data that must be erased, such as users, stay soft-deleted, and the erasure blanks their personal columns in place: the order history keeps its key, and the person is gone.

Three other designs are worth naming, because they are often what the team actually needs:

  • A status column instead of a delete. A product nobody sells any more is in a state, discontinued, not deleted. A status column with a CHECK says so, and nothing about it suggests the row should vanish.
  • A view over live rows. CREATE VIEW products_live AS SELECT * FROM products WHERE deleted_at IS NULL puts the soft delete filter in one place, so it is not forgotten in the next query. A partial index WHERE deleted_at IS NULL on the columns you search keeps those lookups as small as an archive would.
  • A history table filled by a trigger. When the real requirement is "know what this row used to be", a trigger that copies the old row on every UPDATE and DELETE records changes as well as deletes, and the live table stays plain.

Two rules make either main choice safe. On a soft-delete table, every foreign key into it references id, and the partial unique index on the natural key is a uniqueness rule only. On an archive table, every move names its columns on both sides, INSERT INTO products_archive (id, sku, name) SELECT id, sku, name FROM moved. A column added later is then left out of the archive rather than written into another column, which we checked; adding it to the archive table is still part of every schema change to the live one.

How do you add soft delete to a table other tables reference by a natural key?

Move the foreign keys to id first, build the partial index before dropping the old constraint, and never use CASCADE.

Starting from the shop at the top of the post:

ALTER TABLE products ADD COLUMN deleted_at timestamptz; ALTER TABLE order_items ADD COLUMN product_id bigint; UPDATE order_items oi SET product_id = p.id FROM products p WHERE p.sku = oi.sku; ALTER TABLE order_items ADD CONSTRAINT order_items_product_id_not_null NOT NULL product_id NOT VALID; ALTER TABLE order_items VALIDATE CONSTRAINT order_items_product_id_not_null; ALTER TABLE order_items ADD CONSTRAINT order_items_product_id_fkey FOREIGN KEY (product_id) REFERENCES products (id) NOT VALID; ALTER TABLE order_items VALIDATE CONSTRAINT order_items_product_id_fkey; ALTER TABLE order_items DROP CONSTRAINT order_items_sku_fkey; CREATE UNIQUE INDEX CONCURRENTLY products_sku_live ON products (sku) WHERE deleted_at IS NULL; ALTER TABLE products DROP CONSTRAINT products_sku_key; 

The order is the point. The application must write product_id on new order lines before the backfill starts, or lines inserted in between fail the new NOT NULL. The NOT NULL ... NOT VALID and the foreign key NOT VALID are instant, and each VALIDATE reads every order line under a lock that lets reads and writes carry on; on PostgreSQL 17 or older, use a CHECK (product_id IS NOT NULL) the same way, then SET NOT NULL. CREATE UNIQUE INDEX CONCURRENTLY builds the partial index without holding back writes, must run outside a transaction, and exists before products_sku_key goes, so there is no moment with no uniqueness on sku. With the old foreign key dropped first, DROP CONSTRAINT products_sku_key has nothing depending on it and needs no CASCADE. On a large table, split the UPDATE into batches.

order_items.sku stays behind with nothing enforcing it. Keep it on purpose, as the SKU the customer saw when they ordered, or drop it; do not leave it looking like a key.

How does Schemity catch a soft-delete migration that drops a foreign key?

From the next release, Schemity reads the migration against your live database before it runs and reports the foreign key a dropped unique constraint takes with it, and the one PostgreSQL will refuse to recreate.

Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. While you design the next one, the soft-delete version of a table, it shows what the change does to the tables around it. On a diagram connected to the shop above, with 20,000 products and 100,000 order lines, impact analysis (a Schemity Pro feature) read the two statements at the start of the soft delete section, after ALTER TABLE products ADD COLUMN deleted_at timestamptz. On DROP CONSTRAINT products_sku_key it reported that order_items_sku_fkey depends on products.sku and that the database refuses the change. With CASCADE added, it reported the same foreign key as dropped by CASCADE, which PostgreSQL itself reports only as a NOTICE. It also put a cost on the partial index built without CONCURRENTLY: it holds back writes to products while it reads about 20,000 rows, 2.7 MB on disk.

Schemity's SQL migration drawer on a PostgreSQL 18.3 diagram of products and order_items, where order_items.sku references products.sku: 3 statements analysed; Can fail on existing data says Adds a UNIQUE on products, fails if duplicates exist among ~20K rows; Holds back other statements says Building the index holds back writes to products while it reads ~20K rows, 2.7 MB on disk; Dependent objects says 1 object depends on products.sku, and the database refuses the change, naming the referencing foreign key order_items_sku_fkey

For the version that recreates the foreign key against the partial index, impact analysis lists that statement under what can fail: adding a foreign key referencing products.sku, which the database refuses, because no primary key, UNIQUE constraint or unique index without a WHERE covers it. Schemity's SQL migration drawer for the five-statement file that swaps products' UNIQUE (sku) for a partial index and re-adds order_items_sku_fkey: 5 statements analysed; Can fail on existing data (2) lists Adds a UNIQUE on products, fails if duplicates exist among ~20K rows, and Adds a foreign key referencing products.sku, which the database refuses: no primary key, UNIQUE constraint or unique index without a WHERE covers exactly those columns; Holds back other statements lists the index build on ~20K rows, 2.7 MB on disk; Spreads through the diagram lists Drops and recreates a foreign key on order_items

The change preview (Pro) says the same through lint, as a foreign key that fails at runtime, and draws the partial index's U with a dot above it on sku: unique only among the rows its condition keeps. Schemity Free shows the migration SQL a save would run before it is applied, without these findings.

Schemity's change preview of the same migration on PostgreSQL 18.3: on the canvas, products gains deleted_at (marked N) and sku carries a U with a dot above it for the partial index; the footer reads u: 1 (+1 -1), idx: 1 (+1). Findings list 4 planned changes (the foreign key on order_items (sku) changed, the column deleted_at added, the unique constraint on products (sku) dropped, a unique index on products (sku) added); Lint: 2, including order_items.sku Foreign key references columns that are not unique; Impact: 4, including Adds a foreign key referencing products.sku, which the database refuses

Writing this post found every one of those findings missing; the fixes ship in the next release. Impact analysis said nothing about a dropped unique or primary key that another table's foreign key referenced, on any database. Measured for the fix: PostgreSQL 18.3 refuses the drop, MySQL 8.4.11 and MariaDB 11.4.13 refuse it with error 1553, and SQL Server 2025 refuses it with error 3725. It did not know a foreign key onto a partial index is refused either, and reported only the locks of checking it. And after a migration file dropped a single-column UNIQUE, the column still counted as unique, so lint read the partial index as a valid foreign key target.

Related reading

The partial unique index itself, and what it costs to build, is in unique constraint vs unique index, and why a UNIQUE (email, deleted_at) constraint protects nothing is in unique constraints and nullable columns. The NOT NULL ... NOT VALID step is measured in adding a NOT NULL column to a large table. For what each ON DELETE action does to the rows that reference a deleted one, see ON DELETE SET NULL vs CASCADE, and for everything else a CASCADE can take with it, what DROP COLUMN drops without a warning.