Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 328c3f6

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 disallow changing the type of a column inherited from multiple such parents, else it would be out of step with the other parents. However, it's possible for the column to ultimately be inherited from just one common ancestor, in which case a change starting from that ancestor should still be allowed. (I would not be excited about preserving that option, were it not that we have regression test cases exercising it already ...) It's slightly annoying that this patch looks different from the logic with the same end goal in renameatt(), and more annoying that it requires an extra syscache lookup to make the test. However, the recursion logic is quite different in the two functions, and a back-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 in 9.4 too (and doubtless much further back); but the way the recursion is done in 9.4 is a good bit different, so that substantial refactoring would be needed to fix it in 9.4. I'm disinclined to do that, or risk introducing 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 parent 3a5c08a commit 328c3f6

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
Oid relid = 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-
Oid childrelid = lfirst_oid(child);
10428+
Oid childrelid = lfirst_oid(lo);
10429+
int numparents = lfirst_int(li);
1042210430
Relation childrel;
10431+
HeapTuple childtuple;
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
alter table a alter column aa type integer 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(f1 int, f2 int);
215+
create temp table parent2(f1 int, f3 bigint);
216+
create temp table childtab(f4 int) inherits(parent1, parent2);
217+
alter table parent1 alter column f1 type bigint; -- fail, conflict w/parent2
218+
alter table parent1 alter column f2 type bigint; -- ok
219+
211220
-- Test non-inheritable parent constraints
212221
create table p1(ff1 int);
213222
alter table p1 add constraint p1chk check (ff1 > 0) no inherit;

0 commit comments

Comments
 (0)