From 81a43f6436b15e5d001a9283420ec57b0cdbd296 Mon Sep 17 00:00:00 2001 From: zhubin79 <18784715772@163.com> Date: Sat, 31 Aug 2024 17:49:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=97=E4=B8=BA=E5=85=81?= =?UTF-8?q?=E8=AE=B8NULL=E5=80=BC=E6=97=B6=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E6=98=AF=E5=90=A6=E4=B8=BA=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E8=A7=A3=E7=A0=81=E5=88=97=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gausskernel/optimizer/commands/tablecmds.cpp | 14 ++++++++++++++ src/test/regress/expected/replica_identity.out | 8 ++++++++ src/test/regress/sql/replica_identity.sql | 7 +++++++ 3 files changed, 29 insertions(+) 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 ----