mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
Preserve replica identity index across ALTER TABLE rewrite
If an index was explicitly set as replica identity index, this setting was lost when a table was rewritten by ALTER TABLE. Because this setting is part of pg_index but actually controlled by ALTER TABLE (not part of CREATE INDEX, say), we have to do some extra work to restore it. Based-on-patch-by: Quan Zongliang <quanzongliang@gmail.com> Reviewed-by: Euler Taveira <euler.taveira@2ndquadrant.com> Discussion: https://www.postgresql.org/message-id/flat/c70fcab2-4866-0d9f-1d01-e75e189db342@gmail.com
This commit is contained in:
@ -179,5 +179,51 @@ SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
||||
n
|
||||
(1 row)
|
||||
|
||||
---
|
||||
-- Test that ALTER TABLE rewrite preserves nondefault replica identity
|
||||
---
|
||||
-- constraint variant
|
||||
CREATE TABLE test_replica_identity2 (id int UNIQUE NOT NULL);
|
||||
ALTER TABLE test_replica_identity2 REPLICA IDENTITY USING INDEX test_replica_identity2_id_key;
|
||||
\d test_replica_identity2
|
||||
Table "public.test_replica_identity2"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
--------+---------+-----------+----------+---------
|
||||
id | integer | | not null |
|
||||
Indexes:
|
||||
"test_replica_identity2_id_key" UNIQUE CONSTRAINT, btree (id) REPLICA IDENTITY
|
||||
|
||||
ALTER TABLE test_replica_identity2 ALTER COLUMN id TYPE bigint;
|
||||
\d test_replica_identity2
|
||||
Table "public.test_replica_identity2"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
--------+--------+-----------+----------+---------
|
||||
id | bigint | | not null |
|
||||
Indexes:
|
||||
"test_replica_identity2_id_key" UNIQUE CONSTRAINT, btree (id) REPLICA IDENTITY
|
||||
|
||||
-- straight index variant
|
||||
CREATE TABLE test_replica_identity3 (id int NOT NULL);
|
||||
CREATE UNIQUE INDEX test_replica_identity3_id_key ON test_replica_identity3 (id);
|
||||
ALTER TABLE test_replica_identity3 REPLICA IDENTITY USING INDEX test_replica_identity3_id_key;
|
||||
\d test_replica_identity3
|
||||
Table "public.test_replica_identity3"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
--------+---------+-----------+----------+---------
|
||||
id | integer | | not null |
|
||||
Indexes:
|
||||
"test_replica_identity3_id_key" UNIQUE, btree (id) REPLICA IDENTITY
|
||||
|
||||
ALTER TABLE test_replica_identity3 ALTER COLUMN id TYPE bigint;
|
||||
\d test_replica_identity3
|
||||
Table "public.test_replica_identity3"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
--------+--------+-----------+----------+---------
|
||||
id | bigint | | not null |
|
||||
Indexes:
|
||||
"test_replica_identity3_id_key" UNIQUE, btree (id) REPLICA IDENTITY
|
||||
|
||||
DROP TABLE test_replica_identity;
|
||||
DROP TABLE test_replica_identity2;
|
||||
DROP TABLE test_replica_identity3;
|
||||
DROP TABLE test_replica_identity_othertable;
|
||||
|
@ -75,5 +75,26 @@ SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
||||
ALTER TABLE test_replica_identity REPLICA IDENTITY NOTHING;
|
||||
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
||||
|
||||
---
|
||||
-- Test that ALTER TABLE rewrite preserves nondefault replica identity
|
||||
---
|
||||
|
||||
-- constraint variant
|
||||
CREATE TABLE test_replica_identity2 (id int UNIQUE NOT NULL);
|
||||
ALTER TABLE test_replica_identity2 REPLICA IDENTITY USING INDEX test_replica_identity2_id_key;
|
||||
\d test_replica_identity2
|
||||
ALTER TABLE test_replica_identity2 ALTER COLUMN id TYPE bigint;
|
||||
\d test_replica_identity2
|
||||
|
||||
-- straight index variant
|
||||
CREATE TABLE test_replica_identity3 (id int NOT NULL);
|
||||
CREATE UNIQUE INDEX test_replica_identity3_id_key ON test_replica_identity3 (id);
|
||||
ALTER TABLE test_replica_identity3 REPLICA IDENTITY USING INDEX test_replica_identity3_id_key;
|
||||
\d test_replica_identity3
|
||||
ALTER TABLE test_replica_identity3 ALTER COLUMN id TYPE bigint;
|
||||
\d test_replica_identity3
|
||||
|
||||
DROP TABLE test_replica_identity;
|
||||
DROP TABLE test_replica_identity2;
|
||||
DROP TABLE test_replica_identity3;
|
||||
DROP TABLE test_replica_identity_othertable;
|
||||
|
Reference in New Issue
Block a user