diff --git a/src/gausskernel/optimizer/commands/tablecmds.cpp b/src/gausskernel/optimizer/commands/tablecmds.cpp index 721eb8b5d..88e3c4214 100755 --- a/src/gausskernel/optimizer/commands/tablecmds.cpp +++ b/src/gausskernel/optimizer/commands/tablecmds.cpp @@ -13806,6 +13806,7 @@ static ObjectAddress ATExecDropNotNull(Relation rel, const char* colName, LOCKMO List* indexoidlist = NIL; ListCell* indexoidscan = NULL; ObjectAddress address; + Oid replidindex; /* * lookup the attribute @@ -13833,6 +13834,9 @@ static ObjectAddress ATExecDropNotNull(Relation rel, const char* colName, LOCKMO /* Loop over all indexes on the relation */ indexoidlist = RelationGetIndexList(rel); + /* replica identity index */ + replidindex = rel->rd_replidindex; + foreach (indexoidscan, indexoidlist) { Oid indexoid = lfirst_oid(indexoidscan); HeapTuple indexTuple; @@ -13863,6 +13867,16 @@ static ObjectAddress ATExecDropNotNull(Relation rel, const char* colName, LOCKMO } } + /* REPLICA IDENTIFY can't drop not null */ + if (replidindex == indexoid) { + for (i = 0; i < indnkeyatts; i++) { + if (indexStruct->indkey.values[i] == attnum) + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("column \"%s\" used as replica identity can't drop not null", colName))); + } + } + ReleaseSysCache(indexTuple); } diff --git a/src/test/regress/expected/replica_identity.out b/src/test/regress/expected/replica_identity.out index ab2698531..ab27a6101 100644 --- a/src/test/regress/expected/replica_identity.out +++ b/src/test/regress/expected/replica_identity.out @@ -80,6 +80,13 @@ SELECT count(*) FROM pg_index WHERE indrelid = 'test_replica_identity'::regclass 1 (1 row) +---- +-- Make sure can't alter replica identity index alown NULL +---- +CREATE UNIQUE INDEX uni_idx_keya ON test_replica_identity (keya); +ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX uni_idx_keya; +ALTER TABLE test_replica_identity MODIFY keya NULL; -- fail +ERROR: column "keya" used as replica identity can't drop not null ---- -- Make sure non index cases work ---- @@ -113,6 +120,7 @@ SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass; nonkey | text | Indexes: "test_replica_identity_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default + "uni_idx_keya" UNIQUE, btree (keya) TABLESPACE pg_default "test_replica_identity_hash" hash (nonkey) TABLESPACE pg_default "test_replica_identity_keyab" btree (keya, keyb) TABLESPACE pg_default Replica Identity: FULL diff --git a/src/test/regress/sql/replica_identity.sql b/src/test/regress/sql/replica_identity.sql index 0bdbfb17e..6565ef329 100644 --- a/src/test/regress/sql/replica_identity.sql +++ b/src/test/regress/sql/replica_identity.sql @@ -41,6 +41,13 @@ SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass; SELECT count(*) FROM pg_index WHERE indrelid = 'test_replica_identity'::regclass AND indisreplident; +---- +-- Make sure can't alter replica identity index alown NULL +---- +CREATE UNIQUE INDEX uni_idx_keya ON test_replica_identity (keya); +ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX uni_idx_keya; +ALTER TABLE test_replica_identity MODIFY keya NULL; -- fail + ---- -- Make sure non index cases work ----