Heuristic:2ndQuadrant Pglogical Replication Identity Requirement
| Knowledge Sources | |
|---|---|
| Domains | Replication, Database |
| Last Updated | 2026-10-13 03:51 GMT |
Overview
Tables must have a `PRIMARY KEY` (or explicit `REPLICA IDENTITY`) for `UPDATE` and `DELETE` operations to be replicated; tables without a primary key can only use `INSERT`-only replication.
Description
pglogical uses the PostgreSQL REPLICA IDENTITY index (usually the primary key) to locate tuples for `UPDATE` and `DELETE` operations during change application. Without a primary key or replica identity index, pglogical has no way to find the tuple that should be updated or deleted since there is no unique identifier. `REPLICA IDENTITY FULL` is not supported by pglogical. Tables without a primary key should be added to the `default_insert_only` replication set, which only replicates `INSERT` operations.
Usage
Apply this heuristic when setting up replication for any table. If a table lacks a `PRIMARY KEY`, either add one or assign the table to the `default_insert_only` replication set. When using multiple upstreams, ensure only one `UNIQUE` index exists on downstream replicated tables to avoid conflict resolution failures.
The Insight (Rule of Thumb)
- Action: Ensure every replicated table has a `PRIMARY KEY`. If not possible, use the `default_insert_only` replication set.
- Value: `ALTER TABLE mytable ADD PRIMARY KEY (...)` or `ALTER TABLE mytable REPLICA IDENTITY USING INDEX myindex`
- Trade-off: Tables without a primary key can only have `INSERT`s replicated — `UPDATE`s and `DELETE`s are silently skipped.
- Compatibility: `REPLICA IDENTITY FULL` is not supported. Only index-based replica identity works.
Reasoning
pglogical's apply worker locates target tuples using the REPLICA IDENTITY index. The function `pglogical_tuple_find_replidx` calls `RelationGetReplicaIndex()` to find this index, and raises an error if none exists. The `default_insert_only` replication set exists specifically for tables that lack primary keys, replicating only `INSERT` operations since no tuple lookup is needed. With multiple upstreams, conflict resolution uses one index at a time, so a row satisfying the `PRIMARY KEY` but violating another `UNIQUE` constraint will cause replication to stop.
Code Evidence
REPLICA IDENTITY index lookup — from `pglogical_conflict.c:279-287`:
/* Open REPLICA IDENTITY index.*/
idxoid = RelationGetReplicaIndex(relinfo->ri_RelationDesc);
if (!OidIsValid(idxoid))
{
ereport(ERROR,
(errmsg("could not find REPLICA IDENTITY index for table %s with oid %u",
get_rel_name(RelationGetRelid(relinfo->ri_RelationDesc)),
RelationGetRelid(relinfo->ri_RelationDesc)),
errhint("The REPLICA IDENTITY index is usually the PRIMARY KEY. See the PostgreSQL docs for ALTER TABLE ... REPLICA IDENTITY")));
}
REPLICA IDENTITY columns must be replicated — from `pglogical_functions.c:1475`:
errmsg("REPLICA IDENTITY columns must be replicated")
IS_REPLICA_IDENTITY flag in protocol — from `pglogical_proto_native.c:33,146`:
#define IS_REPLICA_IDENTITY 1
// ...
flags |= IS_REPLICA_IDENTITY;
`REPLICA IDENTITY FULL` not supported — from `pglogical_proto_native.c:290`:
/*
* We can't use REPLICA IDENTITY FULL for this, since that makes
* ...
*/