Sub topic 12
Unique indexes and constraints with `INCLUDE`
This chapter assumes basic SQL, B-tree indexes, and simple EXPLAIN output. You should know how UNIQUE prevents duplicate key values and how an index can help a WHERE clause.
A covering index can enforce uniqueness while also carrying columns needed by a read query. The important rule is that only the key columns participate in uniqueness. Columns in INCLUDE are payload.
A unique covering index
Suppose an application identifies customer profiles by email but often reads the display name and subscription plan at the same time:
DROP TABLE IF EXISTS customer_profile;
CREATE TABLE customer_profile (
customer_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL,
display_name text NOT NULL,
plan text NOT NULL
);
CREATE UNIQUE INDEX customer_profile_email_idx
ON customer_profile (email)
INCLUDE (display_name, plan);
INSERT INTO customer_profile (email, display_name, plan)
VALUES
('[email protected]', 'Ana', 'pro'),
('[email protected]', 'Bo', 'free');
SELECT email, display_name, plan
FROM customer_profile
WHERE email = '[email protected]';
INSERT INTO customer_profile (email, display_name, plan)
VALUES ('[email protected]', 'Another Ana', 'free')
ON CONFLICT (email) DO NOTHING;The second insert adds no row because email is the unique key. display_name and plan do not affect uniqueness. They are available as index payload for queries that select those columns.
| Index part | Example columns | Affects uniqueness? | Used as search keys? |
|---|---|---|---|
| Key | `email` | Yes | Yes |
| Included payload | `display_name`, `plan` | No | No |
The index can support a lookup by email and may answer the query without reading the heap when the visibility map allows an index-only scan.
Unique index versus unique constraint
CREATE UNIQUE INDEX creates a unique index object. It enforces uniqueness, but it is not the same catalog object as a named UNIQUE constraint.
A table constraint is useful when database metadata, migration tools, or schema conventions expect a constraint:
CREATE TABLE account (
account_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL UNIQUE,
display_name text NOT NULL,
plan text NOT NULL
);PostgreSQL does not provide ordinary table-constraint syntax for adding INCLUDE columns directly to a UNIQUE constraint. You can keep the constraint and add a separate covering index:
CREATE INDEX account_email_covering_idx
ON account (email)
INCLUDE (display_name, plan);This creates two indexes, so use this design only when the explicit constraint is required. A unique covering index is often sufficient when all you need is uniqueness plus efficient reads.
A unique index with INCLUDE can also be used for conflict handling when its key columns match the conflict target:
INSERT INTO customer_profile (email, display_name, plan)
VALUES ('[email protected]', 'Updated Ana', 'pro')
ON CONFLICT (email) DO UPDATE
SET display_name = EXCLUDED.display_name,
plan = EXCLUDED.plan;Design limits and trade-offs
Use a column as a key when the query filters, joins, sorts, or enforces uniqueness with that column. Use INCLUDE when the query only returns the column.
Included columns still increase index size. Inserts, deletes, and updates must maintain their stored values. Wide payload such as large text can make the index expensive or exceed index tuple size limits. Test the read improvement against write rate, storage, and maintenance cost.
Exercise
Exercise
In CREATE UNIQUE INDEX ix ON t (tenant_id, username) INCLUDE (display_name), which columns determine whether two rows conflict, and which column is payload?
Show answer
tenant_id and username determine uniqueness and are the search keys. display_name is payload only, so changing it does not create a uniqueness conflict.