未验证 提交 9ebc0423 编写于 作者: ( (Jerome)Junfeng Yang 提交者: GitHub

Error out when changing datatype of column with constraint. (#10712)

Raise a meaningful error message for this case.
GPDB doesn't support alter type on primary key and unique
constraint column. Because it requires to drop - recreate logic.
The drop currently only performs on master which lead error when
recreating index (since recreate index will dispatch to segments and
there still an old constraint index exists).

This fixes the issue https://github.com/greenplum-db/gpdb/issues/10561.
Reviewed-by: NHubert Zhang <hzhang@pivotal.io>
(cherry picked from commit 32446a32)
上级 c9f2a816
...@@ -8689,6 +8689,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab) ...@@ -8689,6 +8689,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab)
{ {
ObjectAddress obj; ObjectAddress obj;
ListCell *l; ListCell *l;
ListCell *oid_item;
/* /*
* Re-parse the index and constraint definitions, and attach them to the * Re-parse the index and constraint definitions, and attach them to the
...@@ -8711,8 +8712,39 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab) ...@@ -8711,8 +8712,39 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab)
/*ATPostAlterTypeParse((char *) lfirst(l), wqueue);*/ /*ATPostAlterTypeParse((char *) lfirst(l), wqueue);*/
} }
foreach(l, tab->changedConstraintDefs) forboth(oid_item, tab->changedConstraintOids,
l, tab->changedConstraintDefs)
{
Oid oldId = lfirst_oid(oid_item);
HeapTuple tup;
Form_pg_constraint con;
char contype;
tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for constraint %u", oldId);
con = (Form_pg_constraint) GETSTRUCT(tup);
contype = con->contype;
ReleaseSysCache(tup);
if (contype == CONSTRAINT_PRIMARY || contype == CONSTRAINT_UNIQUE)
{
/*
* Currently, GPDB doesn't support alter type on primary key and unique
* constraint column. Because it requires drop - recreate logic.
* The drop currently only performs on master which lead error when
* recreating index (since recreate index will dispatch to segments and
* there still old constraint index exists)
* Related issue: https://github.com/greenplum-db/gpdb/issues/10561.
*/
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot alter column with primary key or unique constraint"),
errhint("DROP the constraint first, and recreate it after the ALTER")));
}
ATPostAlterTypeParse((char *) lfirst(l), wqueue); ATPostAlterTypeParse((char *) lfirst(l), wqueue);
}
/* /*
* Now we can drop the existing constraints and indexes --- constraints * Now we can drop the existing constraints and indexes --- constraints
......
...@@ -43,6 +43,22 @@ ERROR: constraint "test" for relation "dupconstr" already exists ...@@ -43,6 +43,22 @@ ERROR: constraint "test" for relation "dupconstr" already exists
-- cleanup -- cleanup
drop table dupconstr; drop table dupconstr;
-- --
-- Alter datatype of column with constraint should raise meaningful error
-- See github issue: https://github.com/greenplum-db/gpdb/issues/10561
--
create table contype (i int4 primary key, j int check (j < 100));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "contype_pkey" for table "contype"
alter table contype alter i type numeric; --error
ERROR: cannot alter column with primary key or unique constraint
HINT: DROP the constraint first, and recreate it after the ALTER
insert into contype values (1, 1), (2, 2), (3, 3);
-- after insert data, alter primary key/unique column's type will go through a special check logic
alter table contype alter i type numeric; --error
ERROR: Changing the type of a column that is used in a UNIQUE or PRIMARY KEY constraint is not allowed
alter table contype alter j type numeric;
-- cleanup
drop table contype;
--
-- Test ALTER COLUMN TYPE after dropped column with text datatype (see MPP-19146) -- Test ALTER COLUMN TYPE after dropped column with text datatype (see MPP-19146)
-- --
create domain mytype as text; create domain mytype as text;
......
...@@ -38,6 +38,20 @@ alter table dupconstr add constraint test primary key (i); ...@@ -38,6 +38,20 @@ alter table dupconstr add constraint test primary key (i);
-- cleanup -- cleanup
drop table dupconstr; drop table dupconstr;
--
-- Alter datatype of column with constraint should raise meaningful error
-- See github issue: https://github.com/greenplum-db/gpdb/issues/10561
--
create table contype (i int4 primary key, j int check (j < 100));
alter table contype alter i type numeric; --error
insert into contype values (1, 1), (2, 2), (3, 3);
-- after insert data, alter primary key/unique column's type will go through a special check logic
alter table contype alter i type numeric; --error
alter table contype alter j type numeric;
-- cleanup
drop table contype;
-- --
-- Test ALTER COLUMN TYPE after dropped column with text datatype (see MPP-19146) -- Test ALTER COLUMN TYPE after dropped column with text datatype (see MPP-19146)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册