summary refs log tree commit diff
diff options
context:
space:
mode:
authorreivilibre <oliverw@matrix.org>2023-12-04 14:57:28 +0000
committerGitHub <noreply@github.com>2023-12-04 14:57:28 +0000
commit51e4e35653f98c3f61222fbdbdb1dcb8864f7fca (patch)
treeb732cd0784e3540ddc03c9af5ba034a79d7d8009
parentSwitch UNIX socket paths to /run, and add a UNIX socket example for HAProxy (... (diff)
downloadsynapse-51e4e35653f98c3f61222fbdbdb1dcb8864f7fca.tar.xz
Add a Postgres `REPLICA IDENTITY` to tables that do not have an implicit one. This should allow use of Postgres logical replication. (take 2, now with no added deadlocks!) (#16658)
* Add `ALTER TABLE ... REPLICA IDENTITY ...` for individual tables

We can't combine them into one file as it makes it likely to hit a deadlock

if Synapse is running, as it only takes one other transaction to access two

tables in a different order to the schema delta.

* Add notes

* Newsfile

Signed-off-by: Olivier Wilkinson (reivilibre) <oliverw@matrix.org>

* Re-introduce REPLICA IDENTITY test

---------

Signed-off-by: Olivier Wilkinson (reivilibre) <oliverw@matrix.org>
-rw-r--r--changelog.d/16658.misc1
-rw-r--r--synapse/storage/schema/common/delta/83/10_replica_identity_applied_module_schemas.sql.postgres1
-rw-r--r--synapse/storage/schema/common/delta/83/10_replica_identity_applied_schema_deltas.sql.postgres1
-rw-r--r--synapse/storage/schema/common/delta/83/10_replica_identity_background_updates.sql.postgres1
-rw-r--r--synapse/storage/schema/common/delta/83/10_replica_identity_schema_compat_version.sql.postgres1
-rw-r--r--synapse/storage/schema/common/delta/83/10_replica_identity_schema_version.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_README.md13
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_account_data.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_application_services_txns.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_appservice_room_list.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_appservice_stream_position.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_batch_events.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_blocked_rooms.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_cache_invalidation_stream_by_instance.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_current_state_delta_stream.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_current_state_events.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_deleted_pushers.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_auth_providers.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_federation_inbox.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_federation_outbox.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_inbox.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_changes_converted_stream_position.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_changes_in_room.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_outbound_last_success.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_outbound_pokes.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_cache.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_extremeties.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_resync.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_stream.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_devices.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_e2e_cross_signing_keys.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_e2e_cross_signing_signatures.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_e2e_device_keys_json.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_e2e_fallback_keys_json.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_e2e_one_time_keys_json.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_e2e_room_keys.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_e2e_room_keys_versions.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_erased_users.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_auth.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_auth_chain_links.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_backward_extremities.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_edges.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_forward_extremities.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_json.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_push_actions.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_push_actions_staging.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary_last_receipt_stream_id.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary_stream_ordering.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_relations.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_search.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_to_state_groups.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_txn_id.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_event_txn_id_device_id.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_events.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_federation_inbound_events_staging.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_federation_stream_position.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_ignored_users.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_insertion_event_edges.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_insertion_event_extremities.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_insertion_events.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_local_current_membership.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository_thumbnails.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository_url_cache.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_monthly_active_users.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_partial_state_events.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_partial_state_rooms_servers.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_presence_stream.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_profiles.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_push_rules_stream.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_ratelimit_override.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_receipts_graph.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_receipts_linearized.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_received_transactions.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_redactions.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_registration_tokens.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_rejections.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_remote_media_cache.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_remote_media_cache_thumbnails.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_account_data.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_alias_servers.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_aliases.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_depth.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_forgetter_stream_pos.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_memberships.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_stats_earliest_token.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_stats_state.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_tags.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_room_tags_revisions.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_server_keys_json.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_server_signature_keys.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_sessions.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_state_events.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_stats_incremental_position.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_stream_ordering_to_exterm.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_stream_positions.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_threads.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_threepid_guest_access_tokens.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_timeline_gaps.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions_credentials.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions_ips.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_daily_visits.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_directory.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_directory_search.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_directory_stream_pos.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_external_ids.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_filters.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_ips.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_signature_stream.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_threepid_id_server.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_user_threepids.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_users.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_users_in_public_rooms.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_users_pending_deactivation.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_users_who_share_private_rooms.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_worker_locks.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_worker_read_write_locks.sql.postgres1
-rw-r--r--synapse/storage/schema/main/delta/83/10_replica_identity_worker_read_write_locks_mode.sql.postgres1
-rw-r--r--synapse/storage/schema/state/delta/83/10_replica_identity_state_group_edges.sql.postgres1
-rw-r--r--synapse/storage/schema/state/delta/83/10_replica_identity_state_groups_state.sql.postgres1
-rw-r--r--tests/storage/test_database.py85
123 files changed, 218 insertions, 1 deletions
diff --git a/changelog.d/16658.misc b/changelog.d/16658.misc
new file mode 100644
index 0000000000..baee042f2f
--- /dev/null
+++ b/changelog.d/16658.misc
@@ -0,0 +1 @@
+Add a Postgres `REPLICA IDENTITY` to tables that do not have an implicit one. This should allow use of Postgres logical replication.
\ No newline at end of file
diff --git a/synapse/storage/schema/common/delta/83/10_replica_identity_applied_module_schemas.sql.postgres b/synapse/storage/schema/common/delta/83/10_replica_identity_applied_module_schemas.sql.postgres
new file mode 100644
index 0000000000..57c35b6430
--- /dev/null
+++ b/synapse/storage/schema/common/delta/83/10_replica_identity_applied_module_schemas.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE applied_module_schemas REPLICA IDENTITY USING INDEX applied_module_schemas_module_name_file_key;
diff --git a/synapse/storage/schema/common/delta/83/10_replica_identity_applied_schema_deltas.sql.postgres b/synapse/storage/schema/common/delta/83/10_replica_identity_applied_schema_deltas.sql.postgres
new file mode 100644
index 0000000000..1426f9a9e4
--- /dev/null
+++ b/synapse/storage/schema/common/delta/83/10_replica_identity_applied_schema_deltas.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE applied_schema_deltas REPLICA IDENTITY USING INDEX applied_schema_deltas_version_file_key;
diff --git a/synapse/storage/schema/common/delta/83/10_replica_identity_background_updates.sql.postgres b/synapse/storage/schema/common/delta/83/10_replica_identity_background_updates.sql.postgres
new file mode 100644
index 0000000000..f04937b3db
--- /dev/null
+++ b/synapse/storage/schema/common/delta/83/10_replica_identity_background_updates.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE background_updates REPLICA IDENTITY USING INDEX background_updates_uniqueness;
diff --git a/synapse/storage/schema/common/delta/83/10_replica_identity_schema_compat_version.sql.postgres b/synapse/storage/schema/common/delta/83/10_replica_identity_schema_compat_version.sql.postgres
new file mode 100644
index 0000000000..e3a6f7ca4f
--- /dev/null
+++ b/synapse/storage/schema/common/delta/83/10_replica_identity_schema_compat_version.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE schema_compat_version REPLICA IDENTITY USING INDEX schema_compat_version_lock_key;
diff --git a/synapse/storage/schema/common/delta/83/10_replica_identity_schema_version.sql.postgres b/synapse/storage/schema/common/delta/83/10_replica_identity_schema_version.sql.postgres
new file mode 100644
index 0000000000..c10efe0563
--- /dev/null
+++ b/synapse/storage/schema/common/delta/83/10_replica_identity_schema_version.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE schema_version REPLICA IDENTITY USING INDEX schema_version_lock_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_README.md b/synapse/storage/schema/main/delta/83/10_replica_identity_README.md
new file mode 100644
index 0000000000..fd7bdfc7ad
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_README.md
@@ -0,0 +1,13 @@
+The `10_replica_identity_xxx.sql.postgres` series of schema deltas adds replica identities for tables that do not have one implicitly as a result of having a primary key.
+
+This is needed to use logical replication with Synapse (at least without `UPDATE` and `DELETE` statements failing!).
+
+Where possible, we use an existing `UNIQUE` index on `NOT NULL` columns as the replica identity. Otherwise, we have to fall back to using the full row as a replica identity.
+
+Unfortunately, by running all the `ALTER TABLE` statements in one schema delta per database, it was too likely to hit a deadlock as it would only take
+one other transaction from a running Synapse worker to access the tables out of order and trigger a deadlock.
+
+By having each statement in its own delta file, each one is run in its own transaction and only needs to take a very brief (instant) lock on the table but no other tables,
+so there should be no chance of deadlock.
+
+Like many schema deltas we already apply to Synapse, it is probably blocked by an ongoing `pg_dump`.
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_account_data.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_account_data.sql.postgres
new file mode 100644
index 0000000000..1bf720e80e
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_account_data.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE account_data REPLICA IDENTITY USING INDEX account_data_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_application_services_txns.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_application_services_txns.sql.postgres
new file mode 100644
index 0000000000..4ec052ebeb
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_application_services_txns.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE application_services_txns REPLICA IDENTITY USING INDEX application_services_txns_as_id_txn_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_appservice_room_list.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_appservice_room_list.sql.postgres
new file mode 100644
index 0000000000..232a628ca2
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_appservice_room_list.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE appservice_room_list REPLICA IDENTITY USING INDEX appservice_room_list_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_appservice_stream_position.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_appservice_stream_position.sql.postgres
new file mode 100644
index 0000000000..e870a68723
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_appservice_stream_position.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE appservice_stream_position REPLICA IDENTITY USING INDEX appservice_stream_position_lock_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_batch_events.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_batch_events.sql.postgres
new file mode 100644
index 0000000000..cd4471f4d7
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_batch_events.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE batch_events REPLICA IDENTITY USING INDEX chunk_events_event_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_blocked_rooms.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_blocked_rooms.sql.postgres
new file mode 100644
index 0000000000..e3fce4045f
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_blocked_rooms.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE blocked_rooms REPLICA IDENTITY USING INDEX blocked_rooms_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_cache_invalidation_stream_by_instance.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_cache_invalidation_stream_by_instance.sql.postgres
new file mode 100644
index 0000000000..4fd87faaf6
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_cache_invalidation_stream_by_instance.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE cache_invalidation_stream_by_instance REPLICA IDENTITY USING INDEX cache_invalidation_stream_by_instance_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_current_state_delta_stream.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_current_state_delta_stream.sql.postgres
new file mode 100644
index 0000000000..b618286e8a
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_current_state_delta_stream.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE current_state_delta_stream REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_current_state_events.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_current_state_events.sql.postgres
new file mode 100644
index 0000000000..b058a9f06e
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_current_state_events.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE current_state_events REPLICA IDENTITY USING INDEX current_state_events_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_deleted_pushers.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_deleted_pushers.sql.postgres
new file mode 100644
index 0000000000..0b4fcc5ae1
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_deleted_pushers.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE deleted_pushers REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_auth_providers.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_auth_providers.sql.postgres
new file mode 100644
index 0000000000..8e02438b47
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_auth_providers.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_auth_providers REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_federation_inbox.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_federation_inbox.sql.postgres
new file mode 100644
index 0000000000..547e485f00
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_federation_inbox.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_federation_inbox REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_federation_outbox.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_federation_outbox.sql.postgres
new file mode 100644
index 0000000000..809afe4dc5
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_federation_outbox.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_federation_outbox REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_inbox.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_inbox.sql.postgres
new file mode 100644
index 0000000000..07652038a2
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_inbox.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_inbox REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_changes_converted_stream_position.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_changes_converted_stream_position.sql.postgres
new file mode 100644
index 0000000000..8474b8c842
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_changes_converted_stream_position.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_lists_changes_converted_stream_position REPLICA IDENTITY USING INDEX device_lists_changes_converted_stream_position_lock_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_changes_in_room.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_changes_in_room.sql.postgres
new file mode 100644
index 0000000000..712a54bc0f
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_changes_in_room.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_lists_changes_in_room REPLICA IDENTITY USING INDEX device_lists_changes_in_stream_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_outbound_last_success.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_outbound_last_success.sql.postgres
new file mode 100644
index 0000000000..4357690e8d
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_outbound_last_success.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_lists_outbound_last_success REPLICA IDENTITY USING INDEX device_lists_outbound_last_success_unique_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_outbound_pokes.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_outbound_pokes.sql.postgres
new file mode 100644
index 0000000000..cc724877cf
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_outbound_pokes.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_lists_outbound_pokes REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_cache.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_cache.sql.postgres
new file mode 100644
index 0000000000..ea4351c654
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_cache.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_lists_remote_cache REPLICA IDENTITY USING INDEX device_lists_remote_cache_unique_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_extremeties.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_extremeties.sql.postgres
new file mode 100644
index 0000000000..9dc3bec6fa
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_extremeties.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_lists_remote_extremeties REPLICA IDENTITY USING INDEX device_lists_remote_extremeties_unique_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_resync.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_resync.sql.postgres
new file mode 100644
index 0000000000..df3cecfb94
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_remote_resync.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_lists_remote_resync REPLICA IDENTITY USING INDEX device_lists_remote_resync_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_stream.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_stream.sql.postgres
new file mode 100644
index 0000000000..30a7922613
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_device_lists_stream.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE device_lists_stream REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_devices.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_devices.sql.postgres
new file mode 100644
index 0000000000..57d54a0dc0
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_devices.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE devices REPLICA IDENTITY USING INDEX device_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_cross_signing_keys.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_cross_signing_keys.sql.postgres
new file mode 100644
index 0000000000..f3f2693631
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_cross_signing_keys.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE e2e_cross_signing_keys REPLICA IDENTITY USING INDEX e2e_cross_signing_keys_stream_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_cross_signing_signatures.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_cross_signing_signatures.sql.postgres
new file mode 100644
index 0000000000..d2347b7153
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_cross_signing_signatures.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE e2e_cross_signing_signatures REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_device_keys_json.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_device_keys_json.sql.postgres
new file mode 100644
index 0000000000..66dafdc8e1
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_device_keys_json.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE e2e_device_keys_json REPLICA IDENTITY USING INDEX e2e_device_keys_json_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_fallback_keys_json.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_fallback_keys_json.sql.postgres
new file mode 100644
index 0000000000..8bb4dfc78b
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_fallback_keys_json.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE e2e_fallback_keys_json REPLICA IDENTITY USING INDEX e2e_fallback_keys_json_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_one_time_keys_json.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_one_time_keys_json.sql.postgres
new file mode 100644
index 0000000000..b6609cfdfb
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_one_time_keys_json.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE e2e_one_time_keys_json REPLICA IDENTITY USING INDEX e2e_one_time_keys_json_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_room_keys.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_room_keys.sql.postgres
new file mode 100644
index 0000000000..9b98924165
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_room_keys.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE e2e_room_keys REPLICA IDENTITY USING INDEX e2e_room_keys_with_version_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_room_keys_versions.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_room_keys_versions.sql.postgres
new file mode 100644
index 0000000000..ac7aaba44d
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_e2e_room_keys_versions.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE e2e_room_keys_versions REPLICA IDENTITY USING INDEX e2e_room_keys_versions_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_erased_users.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_erased_users.sql.postgres
new file mode 100644
index 0000000000..c775f81333
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_erased_users.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE erased_users REPLICA IDENTITY USING INDEX erased_users_user;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_auth.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_auth.sql.postgres
new file mode 100644
index 0000000000..d8014d123b
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_auth.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_auth REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_auth_chain_links.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_auth_chain_links.sql.postgres
new file mode 100644
index 0000000000..c2f9c90d42
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_auth_chain_links.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_auth_chain_links REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_backward_extremities.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_backward_extremities.sql.postgres
new file mode 100644
index 0000000000..751541a929
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_backward_extremities.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_backward_extremities REPLICA IDENTITY USING INDEX event_backward_extremities_event_id_room_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_edges.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_edges.sql.postgres
new file mode 100644
index 0000000000..066ea763f1
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_edges.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_edges REPLICA IDENTITY USING INDEX event_edges_event_id_prev_event_id_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_forward_extremities.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_forward_extremities.sql.postgres
new file mode 100644
index 0000000000..d70d6fd55c
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_forward_extremities.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_forward_extremities REPLICA IDENTITY USING INDEX event_forward_extremities_event_id_room_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_json.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_json.sql.postgres
new file mode 100644
index 0000000000..b614b96422
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_json.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_json REPLICA IDENTITY USING INDEX event_json_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_actions.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_actions.sql.postgres
new file mode 100644
index 0000000000..7a92620270
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_actions.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_push_actions REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_actions_staging.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_actions_staging.sql.postgres
new file mode 100644
index 0000000000..292ed9787d
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_actions_staging.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_push_actions_staging REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary.sql.postgres
new file mode 100644
index 0000000000..42a9b6b676
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_push_summary REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary_last_receipt_stream_id.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary_last_receipt_stream_id.sql.postgres
new file mode 100644
index 0000000000..707e498a6e
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary_last_receipt_stream_id.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_push_summary_last_receipt_stream_id REPLICA IDENTITY USING INDEX event_push_summary_last_receipt_stream_id_lock_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary_stream_ordering.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary_stream_ordering.sql.postgres
new file mode 100644
index 0000000000..1240e3e1b5
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_push_summary_stream_ordering.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_push_summary_stream_ordering REPLICA IDENTITY USING INDEX event_push_summary_stream_ordering_lock_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_relations.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_relations.sql.postgres
new file mode 100644
index 0000000000..6b62a6bc20
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_relations.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_relations REPLICA IDENTITY USING INDEX event_relations_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_search.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_search.sql.postgres
new file mode 100644
index 0000000000..e8467fc3d3
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_search.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_search REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_to_state_groups.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_to_state_groups.sql.postgres
new file mode 100644
index 0000000000..4206975c35
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_to_state_groups.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_to_state_groups REPLICA IDENTITY USING INDEX event_to_state_groups_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_txn_id.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_txn_id.sql.postgres
new file mode 100644
index 0000000000..a2bacbc96d
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_txn_id.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_txn_id REPLICA IDENTITY USING INDEX event_txn_id_event_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_event_txn_id_device_id.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_event_txn_id_device_id.sql.postgres
new file mode 100644
index 0000000000..05fce1e6b0
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_event_txn_id_device_id.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE event_txn_id_device_id REPLICA IDENTITY USING INDEX event_txn_id_device_id_event_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_events.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_events.sql.postgres
new file mode 100644
index 0000000000..9046b8eb08
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_events.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE events REPLICA IDENTITY USING INDEX events_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_federation_inbound_events_staging.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_federation_inbound_events_staging.sql.postgres
new file mode 100644
index 0000000000..d9d2f88f67
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_federation_inbound_events_staging.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE federation_inbound_events_staging REPLICA IDENTITY USING INDEX federation_inbound_events_staging_instance_event;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_federation_stream_position.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_federation_stream_position.sql.postgres
new file mode 100644
index 0000000000..5f9df14d7d
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_federation_stream_position.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE federation_stream_position REPLICA IDENTITY USING INDEX federation_stream_position_instance;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_ignored_users.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_ignored_users.sql.postgres
new file mode 100644
index 0000000000..dd082b7f9e
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_ignored_users.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE ignored_users REPLICA IDENTITY USING INDEX ignored_users_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_event_edges.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_event_edges.sql.postgres
new file mode 100644
index 0000000000..8322722b1a
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_event_edges.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE insertion_event_edges REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_event_extremities.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_event_extremities.sql.postgres
new file mode 100644
index 0000000000..1816ce5a49
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_event_extremities.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE insertion_event_extremities REPLICA IDENTITY USING INDEX insertion_event_extremities_event_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_events.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_events.sql.postgres
new file mode 100644
index 0000000000..1fdbd597c7
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_insertion_events.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE insertion_events REPLICA IDENTITY USING INDEX insertion_events_event_id;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_local_current_membership.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_local_current_membership.sql.postgres
new file mode 100644
index 0000000000..f36bad701a
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_local_current_membership.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE local_current_membership REPLICA IDENTITY USING INDEX local_current_membership_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository.sql.postgres
new file mode 100644
index 0000000000..78a5128af3
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE local_media_repository REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository_thumbnails.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository_thumbnails.sql.postgres
new file mode 100644
index 0000000000..6cd53f8e15
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository_thumbnails.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE local_media_repository_thumbnails REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository_url_cache.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository_url_cache.sql.postgres
new file mode 100644
index 0000000000..a41590ce6b
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_local_media_repository_url_cache.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE local_media_repository_url_cache REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_monthly_active_users.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_monthly_active_users.sql.postgres
new file mode 100644
index 0000000000..326fc279d3
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_monthly_active_users.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE monthly_active_users REPLICA IDENTITY USING INDEX monthly_active_users_users;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_partial_state_events.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_partial_state_events.sql.postgres
new file mode 100644
index 0000000000..c6acd84315
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_partial_state_events.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE partial_state_events REPLICA IDENTITY USING INDEX partial_state_events_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_partial_state_rooms_servers.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_partial_state_rooms_servers.sql.postgres
new file mode 100644
index 0000000000..c618660b5f
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_partial_state_rooms_servers.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE partial_state_rooms_servers REPLICA IDENTITY USING INDEX partial_state_rooms_servers_room_id_server_name_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_presence_stream.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_presence_stream.sql.postgres
new file mode 100644
index 0000000000..4e5d8d7a68
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_presence_stream.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE presence_stream REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_profiles.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_profiles.sql.postgres
new file mode 100644
index 0000000000..bb25e7a67a
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_profiles.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE profiles REPLICA IDENTITY USING INDEX profiles_user_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_push_rules_stream.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_push_rules_stream.sql.postgres
new file mode 100644
index 0000000000..f305d9a73a
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_push_rules_stream.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE push_rules_stream REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_ratelimit_override.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_ratelimit_override.sql.postgres
new file mode 100644
index 0000000000..f10f359155
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_ratelimit_override.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE ratelimit_override REPLICA IDENTITY USING INDEX ratelimit_override_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_receipts_graph.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_receipts_graph.sql.postgres
new file mode 100644
index 0000000000..954dd92667
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_receipts_graph.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE receipts_graph REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_receipts_linearized.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_receipts_linearized.sql.postgres
new file mode 100644
index 0000000000..302d887531
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_receipts_linearized.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE receipts_linearized REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_received_transactions.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_received_transactions.sql.postgres
new file mode 100644
index 0000000000..3b8c227717
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_received_transactions.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE received_transactions REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_redactions.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_redactions.sql.postgres
new file mode 100644
index 0000000000..ffa3444b95
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_redactions.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE redactions REPLICA IDENTITY USING INDEX redactions_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_registration_tokens.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_registration_tokens.sql.postgres
new file mode 100644
index 0000000000..3fdb5bbd54
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_registration_tokens.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE registration_tokens REPLICA IDENTITY USING INDEX registration_tokens_token_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_rejections.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_rejections.sql.postgres
new file mode 100644
index 0000000000..c7a7218578
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_rejections.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE rejections REPLICA IDENTITY USING INDEX rejections_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_remote_media_cache.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_remote_media_cache.sql.postgres
new file mode 100644
index 0000000000..d0ecfe1a75
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_remote_media_cache.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE remote_media_cache REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_remote_media_cache_thumbnails.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_remote_media_cache_thumbnails.sql.postgres
new file mode 100644
index 0000000000..9d7b63dfb4
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_remote_media_cache_thumbnails.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE remote_media_cache_thumbnails REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_account_data.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_account_data.sql.postgres
new file mode 100644
index 0000000000..36e756f6b1
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_account_data.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_account_data REPLICA IDENTITY USING INDEX room_account_data_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_alias_servers.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_alias_servers.sql.postgres
new file mode 100644
index 0000000000..df550076ac
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_alias_servers.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_alias_servers REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_aliases.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_aliases.sql.postgres
new file mode 100644
index 0000000000..bfe8db5375
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_aliases.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_aliases REPLICA IDENTITY USING INDEX room_aliases_room_alias_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_depth.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_depth.sql.postgres
new file mode 100644
index 0000000000..77c78ee4ab
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_depth.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_depth REPLICA IDENTITY USING INDEX room_depth_room_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_forgetter_stream_pos.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_forgetter_stream_pos.sql.postgres
new file mode 100644
index 0000000000..51de67b549
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_forgetter_stream_pos.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_forgetter_stream_pos REPLICA IDENTITY USING INDEX room_forgetter_stream_pos_lock_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_memberships.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_memberships.sql.postgres
new file mode 100644
index 0000000000..5819409058
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_memberships.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_memberships REPLICA IDENTITY USING INDEX room_memberships_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_stats_earliest_token.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_stats_earliest_token.sql.postgres
new file mode 100644
index 0000000000..bacd9587d2
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_stats_earliest_token.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_stats_earliest_token REPLICA IDENTITY USING INDEX room_stats_earliest_token_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_stats_state.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_stats_state.sql.postgres
new file mode 100644
index 0000000000..a64db05f44
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_stats_state.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_stats_state REPLICA IDENTITY USING INDEX room_stats_state_room;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_tags.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_tags.sql.postgres
new file mode 100644
index 0000000000..cc69704751
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_tags.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_tags REPLICA IDENTITY USING INDEX room_tag_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_room_tags_revisions.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_room_tags_revisions.sql.postgres
new file mode 100644
index 0000000000..d31063277c
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_room_tags_revisions.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE room_tags_revisions REPLICA IDENTITY USING INDEX room_tag_revisions_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_server_keys_json.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_server_keys_json.sql.postgres
new file mode 100644
index 0000000000..9c0bfb5623
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_server_keys_json.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE server_keys_json REPLICA IDENTITY USING INDEX server_keys_json_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_server_signature_keys.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_server_signature_keys.sql.postgres
new file mode 100644
index 0000000000..1b3d5e68e5
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_server_signature_keys.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE server_signature_keys REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_sessions.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_sessions.sql.postgres
new file mode 100644
index 0000000000..eb8d621795
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_sessions.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE sessions REPLICA IDENTITY USING INDEX sessions_session_type_session_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_state_events.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_state_events.sql.postgres
new file mode 100644
index 0000000000..7e23c8a054
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_state_events.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE state_events REPLICA IDENTITY USING INDEX state_events_event_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_stats_incremental_position.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_stats_incremental_position.sql.postgres
new file mode 100644
index 0000000000..a4a700a3a4
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_stats_incremental_position.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE stats_incremental_position REPLICA IDENTITY USING INDEX stats_incremental_position_lock_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_stream_ordering_to_exterm.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_stream_ordering_to_exterm.sql.postgres
new file mode 100644
index 0000000000..a700741896
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_stream_ordering_to_exterm.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE stream_ordering_to_exterm REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_stream_positions.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_stream_positions.sql.postgres
new file mode 100644
index 0000000000..83ff1c3825
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_stream_positions.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE stream_positions REPLICA IDENTITY USING INDEX stream_positions_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_threads.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_threads.sql.postgres
new file mode 100644
index 0000000000..a3891e4208
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_threads.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE threads REPLICA IDENTITY USING INDEX threads_uniqueness;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_threepid_guest_access_tokens.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_threepid_guest_access_tokens.sql.postgres
new file mode 100644
index 0000000000..df31cf4f1d
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_threepid_guest_access_tokens.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE threepid_guest_access_tokens REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_timeline_gaps.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_timeline_gaps.sql.postgres
new file mode 100644
index 0000000000..8fb41226c1
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_timeline_gaps.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE timeline_gaps REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions.sql.postgres
new file mode 100644
index 0000000000..5d2af97f72
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE ui_auth_sessions REPLICA IDENTITY USING INDEX ui_auth_sessions_session_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions_credentials.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions_credentials.sql.postgres
new file mode 100644
index 0000000000..efb2359faa
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions_credentials.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE ui_auth_sessions_credentials REPLICA IDENTITY USING INDEX ui_auth_sessions_credentials_session_id_stage_type_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions_ips.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions_ips.sql.postgres
new file mode 100644
index 0000000000..a4b86eaf07
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_ui_auth_sessions_ips.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE ui_auth_sessions_ips REPLICA IDENTITY USING INDEX ui_auth_sessions_ips_session_id_ip_user_agent_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_daily_visits.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_daily_visits.sql.postgres
new file mode 100644
index 0000000000..bb22ec5335
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_daily_visits.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_daily_visits REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory.sql.postgres
new file mode 100644
index 0000000000..947f8c5c0f
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_directory REPLICA IDENTITY USING INDEX user_directory_user_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory_search.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory_search.sql.postgres
new file mode 100644
index 0000000000..d421779944
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory_search.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_directory_search REPLICA IDENTITY USING INDEX user_directory_search_user_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory_stream_pos.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory_stream_pos.sql.postgres
new file mode 100644
index 0000000000..59201750b0
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_directory_stream_pos.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_directory_stream_pos REPLICA IDENTITY USING INDEX user_directory_stream_pos_lock_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_external_ids.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_external_ids.sql.postgres
new file mode 100644
index 0000000000..219ee16dde
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_external_ids.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_external_ids REPLICA IDENTITY USING INDEX user_external_ids_auth_provider_external_id_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_filters.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_filters.sql.postgres
new file mode 100644
index 0000000000..5d3e0ae475
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_filters.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_filters REPLICA IDENTITY FULL; -- sadly the `CHECK` constraint is not enough here
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_ips.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_ips.sql.postgres
new file mode 100644
index 0000000000..5a1d8770d0
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_ips.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_ips REPLICA IDENTITY USING INDEX user_ips_user_token_ip_unique_index;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_signature_stream.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_signature_stream.sql.postgres
new file mode 100644
index 0000000000..8bc27df00a
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_signature_stream.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_signature_stream REPLICA IDENTITY USING INDEX user_signature_stream_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_threepid_id_server.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_threepid_id_server.sql.postgres
new file mode 100644
index 0000000000..a0ba8e6773
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_threepid_id_server.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_threepid_id_server REPLICA IDENTITY USING INDEX user_threepid_id_server_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_user_threepids.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_user_threepids.sql.postgres
new file mode 100644
index 0000000000..9c70c9011e
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_user_threepids.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE user_threepids REPLICA IDENTITY USING INDEX medium_address;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_users.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_users.sql.postgres
new file mode 100644
index 0000000000..725e6cdc76
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_users.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE users REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_users_in_public_rooms.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_users_in_public_rooms.sql.postgres
new file mode 100644
index 0000000000..86988a5550
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_users_in_public_rooms.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE users_in_public_rooms REPLICA IDENTITY USING INDEX users_in_public_rooms_u_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_users_pending_deactivation.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_users_pending_deactivation.sql.postgres
new file mode 100644
index 0000000000..7e72946d14
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_users_pending_deactivation.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE users_pending_deactivation REPLICA IDENTITY FULL;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_users_who_share_private_rooms.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_users_who_share_private_rooms.sql.postgres
new file mode 100644
index 0000000000..6c7213920a
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_users_who_share_private_rooms.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE users_who_share_private_rooms REPLICA IDENTITY USING INDEX users_who_share_private_rooms_u_idx;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_worker_locks.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_worker_locks.sql.postgres
new file mode 100644
index 0000000000..1bba45e05c
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_worker_locks.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE worker_locks REPLICA IDENTITY USING INDEX worker_locks_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_worker_read_write_locks.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_worker_read_write_locks.sql.postgres
new file mode 100644
index 0000000000..b9d35eb4f6
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_worker_read_write_locks.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE worker_read_write_locks REPLICA IDENTITY USING INDEX worker_read_write_locks_key;
diff --git a/synapse/storage/schema/main/delta/83/10_replica_identity_worker_read_write_locks_mode.sql.postgres b/synapse/storage/schema/main/delta/83/10_replica_identity_worker_read_write_locks_mode.sql.postgres
new file mode 100644
index 0000000000..5871c98dee
--- /dev/null
+++ b/synapse/storage/schema/main/delta/83/10_replica_identity_worker_read_write_locks_mode.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE worker_read_write_locks_mode REPLICA IDENTITY USING INDEX worker_read_write_locks_mode_key;
diff --git a/synapse/storage/schema/state/delta/83/10_replica_identity_state_group_edges.sql.postgres b/synapse/storage/schema/state/delta/83/10_replica_identity_state_group_edges.sql.postgres
new file mode 100644
index 0000000000..aa8fc2514c
--- /dev/null
+++ b/synapse/storage/schema/state/delta/83/10_replica_identity_state_group_edges.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE state_group_edges REPLICA IDENTITY USING INDEX state_group_edges_unique_idx;
diff --git a/synapse/storage/schema/state/delta/83/10_replica_identity_state_groups_state.sql.postgres b/synapse/storage/schema/state/delta/83/10_replica_identity_state_groups_state.sql.postgres
new file mode 100644
index 0000000000..4386031977
--- /dev/null
+++ b/synapse/storage/schema/state/delta/83/10_replica_identity_state_groups_state.sql.postgres
@@ -0,0 +1 @@
+ALTER TABLE state_groups_state REPLICA IDENTITY FULL;
diff --git a/tests/storage/test_database.py b/tests/storage/test_database.py
index 4d0ebb550d..aa8c76f187 100644
--- a/tests/storage/test_database.py
+++ b/tests/storage/test_database.py
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from typing import Callable, Tuple
+from typing import Callable, List, Tuple
 from unittest.mock import Mock, call
 
 from twisted.internet import defer
@@ -29,6 +29,7 @@ from synapse.storage.database import (
 from synapse.util import Clock
 
 from tests import unittest
+from tests.utils import USE_POSTGRES_FOR_TESTS
 
 
 class TupleComparisonClauseTestCase(unittest.TestCase):
@@ -279,3 +280,85 @@ class CancellationTestCase(unittest.HomeserverTestCase):
             ]
         )
         self.assertEqual(exception_callback.call_count, 6)  # no additional calls
+
+
+class PostgresReplicaIdentityTestCase(unittest.HomeserverTestCase):
+    if not USE_POSTGRES_FOR_TESTS:
+        skip = "Requires Postgres"
+
+    def prepare(
+        self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
+    ) -> None:
+        self.db_pools = homeserver.get_datastores().databases
+
+    def test_all_tables_have_postgres_replica_identity(self) -> None:
+        """
+        Tests that all tables have a Postgres REPLICA IDENTITY.
+        (See https://github.com/matrix-org/synapse/issues/16224).
+
+        Tables with a PRIMARY KEY have an implied REPLICA IDENTITY and are fine.
+        Other tables need them to be set with `ALTER TABLE`.
+
+        A REPLICA IDENTITY is required for Postgres logical replication to work
+        properly without blocking updates and deletes.
+        """
+
+        sql = """
+            -- Select tables that have no primary key and use the default replica identity rule
+            -- (the default is to use the primary key)
+            WITH tables_no_pkey AS (
+                SELECT tbl.table_schema, tbl.table_name
+                FROM information_schema.tables tbl
+                WHERE table_type = 'BASE TABLE'
+                    AND table_schema not in ('pg_catalog', 'information_schema')
+                    AND NOT EXISTS (
+                        SELECT 1
+                        FROM information_schema.table_constraints tc
+                        WHERE tc.constraint_type = 'PRIMARY KEY'
+                            AND tc.table_schema = tbl.table_schema
+                            AND tc.table_name = tbl.table_name
+                    )
+            )
+            SELECT pg_class.oid::regclass FROM tables_no_pkey INNER JOIN pg_class ON pg_class.oid::regclass = table_name::regclass
+            WHERE relreplident = 'd'
+
+            UNION
+
+            -- Also select tables that use an index as a replica identity
+            -- but where the index doesn't exist
+            -- (e.g. it could have been deleted)
+            SELECT pg_class.oid::regclass
+                FROM information_schema.tables tbl
+                INNER JOIN pg_class ON pg_class.oid::regclass = table_name::regclass
+                WHERE table_type = 'BASE TABLE'
+                    AND table_schema not in ('pg_catalog', 'information_schema')
+
+                    -- 'i' means an index is used as the replica identity
+                    AND relreplident = 'i'
+
+                    -- look for indices that are marked as the replica identity
+                    AND NOT EXISTS (
+                        SELECT indexrelid::regclass
+                        FROM pg_index
+                        WHERE indrelid = pg_class.oid::regclass AND indisreplident
+                    )
+        """
+
+        def _list_tables_with_missing_replica_identities_txn(
+            txn: LoggingTransaction,
+        ) -> List[str]:
+            txn.execute(sql)
+            return [table_name for table_name, in txn]
+
+        for pool in self.db_pools:
+            missing = self.get_success(
+                pool.runInteraction(
+                    "test_list_missing_replica_identities",
+                    _list_tables_with_missing_replica_identities_txn,
+                )
+            )
+            self.assertEqual(
+                len(missing),
+                0,
+                f"The following tables in the {pool.name()!r} database are missing REPLICA IDENTITIES: {missing!r}.",
+            )