Skip to content

Commit e49c1a9

Browse files
committed
Fix ALTER TABLE ... ADD COLUMN for inheritance cases.
Alvaro Herrera
1 parent b47c359 commit e49c1a9

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

src/backend/commands/tablecmds.c

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.48 2002/10/19 03:01:09 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.49 2002/10/21 20:31:51 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1584,7 +1584,6 @@ update_ri_trigger_args(Oid relid,
15841584
void
15851585
AlterTableAddColumn(Oid myrelid,
15861586
bool recurse,
1587-
bool recursing,
15881587
ColumnDef *colDef)
15891588
{
15901589
Relation rel,
@@ -1643,22 +1642,50 @@ AlterTableAddColumn(Oid myrelid,
16431642
colDefChild->inhcount = 1;
16441643
colDefChild->is_local = false;
16451644

1646-
/* this routine is actually in the planner */
1647-
children = find_all_inheritors(myrelid);
1645+
/* We only want direct inheritors */
1646+
children = find_inheritance_children(myrelid);
16481647

1649-
/*
1650-
* find_all_inheritors does the recursive search of the
1651-
* inheritance hierarchy, so all we have to do is process all of
1652-
* the relids in the list that it returns.
1653-
*/
16541648
foreach(child, children)
16551649
{
16561650
Oid childrelid = lfirsti(child);
1651+
HeapTuple tuple;
1652+
Form_pg_attribute childatt;
1653+
Relation childrel;
16571654

16581655
if (childrelid == myrelid)
16591656
continue;
16601657

1661-
AlterTableAddColumn(childrelid, false, true, colDefChild);
1658+
attrdesc = heap_openr(AttributeRelationName, RowExclusiveLock);
1659+
tuple = SearchSysCacheCopyAttName(childrelid, colDef->colname);
1660+
if (!HeapTupleIsValid(tuple))
1661+
{
1662+
heap_close(attrdesc, RowExclusiveLock);
1663+
AlterTableAddColumn(childrelid, true, colDefChild);
1664+
continue;
1665+
}
1666+
childatt = (Form_pg_attribute) GETSTRUCT(tuple);
1667+
1668+
typeTuple = typenameType(colDef->typename);
1669+
1670+
if (HeapTupleGetOid(typeTuple) != childatt->atttypid ||
1671+
colDef->typename->typmod != childatt->atttypmod)
1672+
elog(ERROR, "ALTER TABLE: child table %u has different "
1673+
"type for column \"%s\"",
1674+
childrelid, colDef->colname);
1675+
1676+
childatt->attinhcount++;
1677+
simple_heap_update(attrdesc, &tuple->t_self, tuple);
1678+
CatalogUpdateIndexes(attrdesc, tuple);
1679+
1680+
childrel = RelationIdGetRelation(childrelid);
1681+
elog(NOTICE, "ALTER TABLE: merging definition of column "
1682+
"\"%s\" for child %s", colDef->colname,
1683+
RelationGetRelationName(childrel));
1684+
RelationClose(childrel);
1685+
1686+
heap_close(attrdesc, RowExclusiveLock);
1687+
heap_freetuple(tuple);
1688+
ReleaseSysCache(typeTuple);
16621689
}
16631690
}
16641691
else
@@ -1667,8 +1694,7 @@ AlterTableAddColumn(Oid myrelid,
16671694
* If we are told not to recurse, there had better not be any
16681695
* child tables; else the addition would put them out of step.
16691696
*/
1670-
if (!recursing &&
1671-
find_inheritance_children(myrelid) != NIL)
1697+
if (find_inheritance_children(myrelid) != NIL)
16721698
elog(ERROR, "Attribute must be added to child tables too");
16731699
}
16741700

src/backend/tcop/utility.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.179 2002/10/08 17:17:19 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.180 2002/10/21 20:31:52 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -475,7 +475,6 @@ ProcessUtility(Node *parsetree,
475475
*/
476476
AlterTableAddColumn(relid,
477477
interpretInhOption(stmt->relation->inhOpt),
478-
false,
479478
(ColumnDef *) stmt->def);
480479
break;
481480
case 'T': /* ALTER COLUMN DEFAULT */

src/include/commands/tablecmds.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: tablecmds.h,v 1.7 2002/09/04 20:31:42 momjian Exp $
10+
* $Id: tablecmds.h,v 1.8 2002/10/21 20:31:52 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -16,8 +16,7 @@
1616

1717
#include "nodes/parsenodes.h"
1818

19-
extern void AlterTableAddColumn(Oid myrelid, bool recurse, bool recursing,
20-
ColumnDef *colDef);
19+
extern void AlterTableAddColumn(Oid myrelid, bool recurse, ColumnDef *colDef);
2120

2221
extern void AlterTableAlterColumnDropNotNull(Oid myrelid, bool recurse,
2322
const char *colName);

src/test/regress/output/misc.source

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ SELECT * FROM e_star*;
373373
(23 rows)
374374

375375
ALTER TABLE a_star* ADD COLUMN a text;
376+
NOTICE: ALTER TABLE: merging definition of column "a" for child d_star
376377
--UPDATE b_star*
377378
-- SET a = text 'gazpacho'
378379
-- WHERE aa > 4;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy