Sub topic 8
Replica identity: how updates and deletes find rows
This chapter assumes you know basic SQL, primary keys, PostgreSQL roles, and the difference between a publisher and a subscriber. It also assumes that a publication and subscription already exist.
When PostgreSQL logically replicates an INSERT, the new row is enough for the subscriber to insert it. An UPDATE or DELETE also needs to identify the old row. Replica identity is the rule that tells PostgreSQL which old column values to send for that purpose.
The subscriber uses those values to find the matching row in its local table.
The default: the primary key
For most tables, PostgreSQL uses the primary key as the replica identity. This is called REPLICA IDENTITY DEFAULT.
CREATE TABLE customers (
customer_id bigint PRIMARY KEY,
email text NOT NULL,
status text NOT NULL
);
CREATE PUBLICATION customers_pub
FOR TABLE customers;
UPDATE customers
SET status = 'inactive'
WHERE customer_id = 42;For this update, the logical change contains the old primary-key value, customer_id = 42, and the new value, status = 'inactive'. The subscriber can find the row with that key and apply the update.
The primary key must exist on the subscriber as well. Logical replication does not create constraints automatically.
Choosing another identity
A table can use a suitable unique index instead of its primary key. The index must be unique and suitable for identifying exactly one row. In practice, its columns should also be non-null.
CREATE TABLE customer_emails (
customer_id bigint NOT NULL,
email text NOT NULL,
address_type text NOT NULL,
UNIQUE (email)
);
ALTER TABLE customer_emails
REPLICA IDENTITY USING INDEX customer_emails_email_key;A unique email address can identify a row, so PostgreSQL sends the old email value for updates and deletes. Use this choice only when the indexed value is stable and guaranteed to remain unique. A business value that can change often is usually a poor identity.
You can return to the primary-key default with:
ALTER TABLE customer_emails
REPLICA IDENTITY DEFAULT;Tables without a usable key
REPLICA IDENTITY FULL tells PostgreSQL to use all old column values to find the row.
CREATE TABLE audit_log (
event_time timestamptz NOT NULL,
actor text NOT NULL,
action text NOT NULL,
details jsonb
);
ALTER TABLE audit_log
REPLICA IDENTITY FULL;This option can make updates and deletes possible without a primary key. It has costs:
- More old-row data must be written to and sent through the logical stream.
- The subscriber must compare more columns.
- Duplicate rows can make identification ambiguous or inefficient.
- Large rows can increase network traffic and apply time.
Treat FULL as a compatibility option, not a substitute for a well-designed key.
REPLICA IDENTITY NOTHING records no old-row identity. It is appropriate only for tables that never need replicated updates or deletes. Tables that receive those operations should have a primary key, a suitable unique index, or FULL.
How to choose
| Table condition | Recommended identity |
|---|---|
| Stable primary key exists | `DEFAULT` |
| No primary key, but a stable unique index exists | `USING INDEX` |
| No suitable key and updates or deletes are required | `FULL` |
| Table is insert-only | `NOTHING` may be acceptable |
Exercise
A table has no primary key, but its non-null email column is guaranteed to be unique. Which replica identity is usually preferable to FULL?
Show answer
Create a suitable unique index and use REPLICA IDENTITY USING INDEX. It sends less data and identifies rows more efficiently.