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

Commit 4d4c66a

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 7e78c87 commit 4d4c66a

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
@@ -10279,7 +10279,11 @@ ATPrepAlterColumnType(List **wqueue,
1027910279
errmsg("cannot alter system column \"%s\"",
1028010280
colName)));
1028110281

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

10411-
children = find_all_inheritors(relid, lockmode, NULL);
10417+
child_oids = find_all_inheritors(relid, lockmode,
10418+
&child_numparents);
1041210419

1041310420
/*
1041410421
* find_all_inheritors does the recursive search of the inheritance
1041510422
* hierarchy, so all we have to do is process all of the relids in the
1041610423
* list that it returns.
1041710424
*/
10418-
foreach(child, children)
10425+
forboth(lo, child_oids, li, child_numparents)
1041910426
{
10420-
Oid childrelid = lfirst_oid(child);
10427+
Oid childrelid = lfirst_oid(lo);
10428+
int numparents = lfirst_int(li);
1042110429
Relation childrel;
10430+
HeapTuple childtuple;
10431+
Form_pg_attribute childattTup;
1042210432

1042310433
if (childrelid == relid)
1042410434
continue;
@@ -10427,6 +10437,29 @@ ATPrepAlterColumnType(List **wqueue,
1042710437
childrel = relation_open(childrelid, NoLock);
1042810438
CheckTableNotInUse(childrel, "ALTER TABLE");
1042910439

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