Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit328c3f6

Browse files
committed
Disallow changing an inherited column's type if not all parents changed.
If a table inherits from multiple unrelated parents, we must disallowchanging the type of a column inherited from multiple such parents, elseit would be out of step with the other parents. However, it's possiblefor the column to ultimately be inherited from just one common ancestor,in which case a change starting from that ancestor should still beallowed. (I would not be excited about preserving that option, wereit not that we have regression test cases exercising it already ...)It's slightly annoying that this patch looks different from the logicwith the same end goal in renameatt(), and more annoying that itrequires an extra syscache lookup to make the test. However, therecursion logic is quite different in the two functions, and aback-patched bug fix is no place to be trying to unify them.Per report from Manuel Rigger. Back-patch to 9.5. The bug exists in9.4 too (and doubtless much further back); but the way the recursionis done in 9.4 is a good bit different, so that substantial refactoringwould be needed to fix it in 9.4. I'm disinclined to do that, or riskintroducing new bugs, for a bug that has escaped notice for this long.Discussion:https://postgr.es/m/CA+u7OA4qogDv9rz1HAb-ADxttXYPqQdUdPY_yd4kCzywNxRQXA@mail.gmail.com
1 parent3a5c08a commit328c3f6

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10280,7 +10280,11 @@ ATPrepAlterColumnType(List **wqueue,
1028010280
errmsg("cannot alter system column \"%s\"",
1028110281
colName)));
1028210282

10283-
/* Don't alter inherited columns */
10283+
/*
10284+
* Don't alter inherited columns. At outer level, there had better not be
10285+
* any inherited definition; when recursing, we assume this was checked at
10286+
* the parent level (see below).
10287+
*/
1028410288
if (attTup->attinhcount > 0 && !recursing)
1028510289
ereport(ERROR,
1028610290
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
@@ -10406,20 +10410,26 @@ ATPrepAlterColumnType(List **wqueue,
1040610410
if (recurse)
1040710411
{
1040810412
Oidrelid = RelationGetRelid(rel);
10409-
ListCell *child;
10410-
List *children;
10413+
List *child_oids,
10414+
*child_numparents;
10415+
ListCell *lo,
10416+
*li;
1041110417

10412-
children = find_all_inheritors(relid, lockmode, NULL);
10418+
child_oids = find_all_inheritors(relid, lockmode,
10419+
&child_numparents);
1041310420

1041410421
/*
1041510422
* find_all_inheritors does the recursive search of the inheritance
1041610423
* hierarchy, so all we have to do is process all of the relids in the
1041710424
* list that it returns.
1041810425
*/
10419-
foreach(child, children)
10426+
forboth(lo, child_oids, li, child_numparents)
1042010427
{
10421-
Oidchildrelid = lfirst_oid(child);
10428+
Oidchildrelid = lfirst_oid(lo);
10429+
intnumparents = lfirst_int(li);
1042210430
Relationchildrel;
10431+
HeapTuplechildtuple;
10432+
Form_pg_attribute childattTup;
1042310433

1042410434
if (childrelid == relid)
1042510435
continue;
@@ -10428,6 +10438,29 @@ ATPrepAlterColumnType(List **wqueue,
1042810438
childrel = relation_open(childrelid, NoLock);
1042910439
CheckTableNotInUse(childrel, "ALTER TABLE");
1043010440

10441+
/*
10442+
* Verify that the child doesn't have any inherited definitions of
10443+
* this column that came from outside this inheritance hierarchy.
10444+
* (renameatt makes a similar test, though in a different way
10445+
* because of its different recursion mechanism.)
10446+
*/
10447+
childtuple = SearchSysCacheAttName(RelationGetRelid(childrel),
10448+
colName);
10449+
if (!HeapTupleIsValid(childtuple))
10450+
ereport(ERROR,
10451+
(errcode(ERRCODE_UNDEFINED_COLUMN),
10452+
errmsg("column \"%s\" of relation \"%s\" does not exist",
10453+
colName, RelationGetRelationName(childrel))));
10454+
childattTup = (Form_pg_attribute) GETSTRUCT(childtuple);
10455+
10456+
if (childattTup->attinhcount > numparents)
10457+
ereport(ERROR,
10458+
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
10459+
errmsg("cannot alter inherited column \"%s\" of relation \"%s\"",
10460+
colName, RelationGetRelationName(childrel))));
10461+
10462+
ReleaseSysCache(childtuple);
10463+
1043110464
/*
1043210465
* Remap the attribute numbers. If no USING expression was
1043310466
* specified, there is no need for this step.

‎src/test/regress/expected/inherit.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,16 @@ select * from d;
723723
32 | one | two | three
724724
(1 row)
725725

726+
-- The above verified that we can change the type of a multiply-inherited
727+
-- column; but we should reject that if any definition was inherited from
728+
-- an unrelated parent.
729+
create temp table parent1(f1 int, f2 int);
730+
create temp table parent2(f1 int, f3 bigint);
731+
create temp table childtab(f4 int) inherits(parent1, parent2);
732+
NOTICE: merging multiple inherited definitions of column "f1"
733+
alter table parent1 alter column f1 type bigint; -- fail, conflict w/parent2
734+
ERROR: cannot alter inherited column "f1" of relation "childtab"
735+
alter table parent1 alter column f2 type bigint; -- ok
726736
-- Test non-inheritable parent constraints
727737
create table p1(ff1 int);
728738
alter table p1 add constraint p1chk check (ff1 > 0) no inherit;

‎src/test/regress/sql/inherit.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ insert into d values('test','one','two','three');
208208
altertable a alter column aa typeinteger using bit_length(aa);
209209
select*from d;
210210

211+
-- The above verified that we can change the type of a multiply-inherited
212+
-- column; but we should reject that if any definition was inherited from
213+
-- an unrelated parent.
214+
create temp table parent1(f1int, f2int);
215+
create temp table parent2(f1int, f3bigint);
216+
create temp table childtab(f4int) inherits(parent1, parent2);
217+
altertable parent1 alter column f1 typebigint;-- fail, conflict w/parent2
218+
altertable parent1 alter column f2 typebigint;-- ok
219+
211220
-- Test non-inheritable parent constraints
212221
createtablep1(ff1int);
213222
altertable p1 addconstraint p1chkcheck (ff1>0) no inherit;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp