|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | +:imagesdir: ./_images |
| 5 | + |
| 6 | += Default Logical Replication Support for Tables Without Primary Key |
| 7 | + |
| 8 | +== Overview |
| 9 | + |
| 10 | +In PostgreSQL/IvorySQL logical replication, UPDATE and DELETE operations rely on the Replica Identity to locate target rows on the subscriber side. By default, tables use `REPLICA IDENTITY DEFAULT`, where the system relies on the Primary Key to locate rows. If a table has no primary key, the replication strategy falls back to `REPLICA IDENTITY NOTHING`, causing UPDATE and DELETE operations to fail because the target rows cannot be located. |
| 11 | + |
| 12 | +IvorySQL introduces the GUC parameter `logical_replication_fallback_to_full_identity`. When this parameter is enabled, if a table's replica identity is `DEFAULT` and it has no primary key, the system automatically falls back to `REPLICA IDENTITY FULL` — recording the complete old row data in the WAL, allowing UPDATE and DELETE operations on tables without a primary key to work correctly through logical replication. |
| 13 | + |
| 14 | +This feature only takes effect on the publisher side; no additional configuration is required on the subscriber side. |
| 15 | + |
| 16 | +== Parameter Description |
| 17 | + |
| 18 | +[source,sql] |
| 19 | +---- |
| 20 | +# postgresql.conf |
| 21 | +logical_replication_fallback_to_full_identity = on |
| 22 | +---- |
| 23 | + |
| 24 | +Parameter description: |
| 25 | + |
| 26 | +- Type: `boolean`; |
| 27 | +- Default value: `off`; |
| 28 | +- Scope: `sighup`; takes effect by modifying `postgresql.conf` and executing `SELECT pg_reload_conf();`, no database restart required; |
| 29 | +- Applicable node: only takes effect on the publisher side; no configuration needed on the subscriber side. |
| 30 | + |
| 31 | +== Test Cases |
| 32 | + |
| 33 | +=== Without the Parameter Enabled, UPDATE and DELETE Replication Fails for Tables Without Primary Key |
| 34 | + |
| 35 | +[source,sql] |
| 36 | +---- |
| 37 | +-- Publisher: create a table without a primary key |
| 38 | +CREATE TABLE test_no_pk (id int, name text); |
| 39 | +
|
| 40 | +-- Subscriber: create the same table structure |
| 41 | +CREATE TABLE test_no_pk (id int, name text); |
| 42 | +
|
| 43 | +-- Publisher: create a publication and add the table |
| 44 | +CREATE PUBLICATION tap_pub FOR TABLE test_no_pk; |
| 45 | +
|
| 46 | +-- Subscriber: create a subscription |
| 47 | +CREATE SUBSCRIPTION tap_sub CONNECTION 'host=publisher dbname=postgres' PUBLICATION tap_pub; |
| 48 | +
|
| 49 | +-- Publisher: INSERT operations always work (do not depend on replica identity) |
| 50 | +INSERT INTO test_no_pk VALUES (1, 'alice'); |
| 51 | +
|
| 52 | +-- Publisher: UPDATE fails (no primary key, cannot locate the target row) |
| 53 | +UPDATE test_no_pk SET name = 'bob' WHERE id = 1; |
| 54 | +-- ERROR: cannot update table "test_no_pk" because it does not have a replica identity and publishes updates |
| 55 | +
|
| 56 | +-- Publisher: DELETE also fails |
| 57 | +DELETE FROM test_no_pk WHERE id = 1; |
| 58 | +-- ERROR: cannot delete from table "test_no_pk" because it does not have a replica identity and publishes deletes |
| 59 | +---- |
| 60 | + |
| 61 | +=== With the Parameter Enabled, UPDATE and DELETE Replication Works for Tables Without Primary Key |
| 62 | + |
| 63 | +[source,sql] |
| 64 | +---- |
| 65 | +-- Publisher: enable the parameter and reload the configuration |
| 66 | +ALTER SYSTEM SET logical_replication_fallback_to_full_identity = on; |
| 67 | +SELECT pg_reload_conf(); |
| 68 | +
|
| 69 | +-- Publisher: INSERT works |
| 70 | +INSERT INTO test_no_pk VALUES (1, 'alice'); |
| 71 | +
|
| 72 | +-- Publisher: UPDATE works (automatically records the complete old row data in FULL mode) |
| 73 | +UPDATE test_no_pk SET name = 'bob' WHERE id = 1; |
| 74 | +
|
| 75 | +-- Publisher: DELETE works |
| 76 | +DELETE FROM test_no_pk WHERE id = 1; |
| 77 | +---- |
| 78 | + |
| 79 | +=== The Parameter Does Not Affect Tables With a Primary Key |
| 80 | + |
| 81 | +[source,sql] |
| 82 | +---- |
| 83 | +-- Tables with a primary key always use the primary key to locate rows, regardless of this parameter |
| 84 | +CREATE TABLE test_with_pk (id int PRIMARY KEY, name text); |
| 85 | +
|
| 86 | +-- Whether the parameter is enabled or not, UPDATE and DELETE work normally |
| 87 | +INSERT INTO test_with_pk VALUES (1, 'alice'); |
| 88 | +UPDATE test_with_pk SET name = 'bob' WHERE id = 1; |
| 89 | +DELETE FROM test_with_pk WHERE id = 1; |
| 90 | +---- |
| 91 | + |
| 92 | +=== The Parameter Does Not Affect Tables Explicitly Set to REPLICA IDENTITY NOTHING |
| 93 | + |
| 94 | +[source,sql] |
| 95 | +---- |
| 96 | +-- Create a table and explicitly set it to REPLICA IDENTITY NOTHING |
| 97 | +CREATE TABLE test_nothing (id int, data text); |
| 98 | +ALTER TABLE test_nothing REPLICA IDENTITY NOTHING; |
| 99 | +
|
| 100 | +-- Even with logical_replication_fallback_to_full_identity enabled, |
| 101 | +-- UPDATE and DELETE still fail (the parameter does not override an explicit NOTHING setting) |
| 102 | +INSERT INTO test_nothing VALUES (1, 'test'); |
| 103 | +UPDATE test_nothing SET data = 'modified' WHERE id = 1; |
| 104 | +-- ERROR: cannot update table "test_nothing" because it does not have a replica identity and publishes updates |
| 105 | +
|
| 106 | +DELETE FROM test_nothing WHERE id = 1; |
| 107 | +-- ERROR: cannot delete from table "test_nothing" because it does not have a replica identity and publishes deletes |
| 108 | +---- |
| 109 | + |
| 110 | +=== Dynamically Switching the Parameter at Runtime |
| 111 | + |
| 112 | +[source,sql] |
| 113 | +---- |
| 114 | +-- Disable the parameter: UPDATE/DELETE on tables without a primary key will revert to error behavior |
| 115 | +ALTER SYSTEM SET logical_replication_fallback_to_full_identity = off; |
| 116 | +SELECT pg_reload_conf(); |
| 117 | +
|
| 118 | +-- Enable the parameter: UPDATE/DELETE on tables without a primary key will work normally |
| 119 | +ALTER SYSTEM SET logical_replication_fallback_to_full_identity = on; |
| 120 | +SELECT pg_reload_conf(); |
| 121 | +---- |
| 122 | + |
| 123 | +== Limitations |
| 124 | + |
| 125 | +. This parameter only takes effect for tables whose replica identity is `REPLICA IDENTITY DEFAULT` and that have no primary key; tables explicitly set to `FULL`, `USING INDEX`, or `NOTHING` are not affected; |
| 126 | +. When this parameter is enabled, UPDATE and DELETE on tables without a primary key will record the complete old row data in the WAL (same effect as `REPLICA IDENTITY FULL`), which increases WAL size and network traffic compared to recording only primary key columns when a primary key exists; |
| 127 | +. This parameter only takes effect on the publisher side; no configuration is needed on the subscriber side; |
| 128 | +. INSERT operations do not depend on replica identity and can always be replicated normally, regardless of whether this parameter is enabled; |
| 129 | +. This parameter does not replace an explicit `ALTER TABLE ... REPLICA IDENTITY FULL` setting, nor does it override tables explicitly set to `NOTHING`. |
0 commit comments