Skip to content

Commit cabad37

Browse files
committed
> Huh, I don't know where I got the idea you were (or someone else was?)
> in the position that attislocal should be reset. I'll clean everything > up and submit the patch I had originally made. All right, this is it. This patch merely checks if child tables have the column. If atttypid and atttypmod are the same, the attributes' attinhcount is incremented; else the operation is aborted. If child tables don't have the column, recursively add it. attislocal is not touched in any case. Alvaro Herrera
1 parent bab3d29 commit cabad37

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/backend/commands/tablecmds.c

Lines changed: 38 additions & 7 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.45 2002/09/28 20:00:19 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.46 2002/10/19 02:09:45 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1643,22 +1643,53 @@ AlterTableAddColumn(Oid myrelid,
16431643
colDefChild->inhcount = 1;
16441644
colDefChild->is_local = false;
16451645

1646-
/* this routine is actually in the planner */
1647-
children = find_all_inheritors(myrelid);
1646+
/* we only need direct inheritors */
1647+
children = find_inheritance_children(myrelid);
16481648

16491649
/*
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.
1650+
* If the child has a column with same name and type,
1651+
* increment its attinhcount and continue. If it has
1652+
* different type, abort. If it doesn't have a column
1653+
* with the same name, add it.
16531654
*/
16541655
foreach(child, children)
16551656
{
16561657
Oid childrelid = lfirsti(child);
1658+
HeapTuple tuple;
1659+
Form_pg_attribute childatt;
16571660

16581661
if (childrelid == myrelid)
16591662
continue;
16601663

1661-
AlterTableAddColumn(childrelid, false, true, colDefChild);
1664+
attrdesc = heap_openr(AttributeRelationName, RowExclusiveLock);
1665+
tuple = SearchSysCacheCopyAttName(childrelid, colDef->colname);
1666+
if (!HeapTupleIsValid(tuple))
1667+
{
1668+
heap_close(attrdesc, RowExclusiveLock);
1669+
AlterTableAddColumn(childrelid, false, true, colDefChild);
1670+
continue;
1671+
}
1672+
childatt = (Form_pg_attribute) GETSTRUCT(tuple);
1673+
1674+
typeTuple = typenameType(colDef->typename);
1675+
tform = (Form_pg_type) GETSTRUCT(typeTuple);
1676+
1677+
if (HeapTupleGetOid(typeTuple) != childatt->atttypid ||
1678+
colDef->typename->typemod != childatt->atttypmod)
1679+
elog(ERROR, "ALTER TABLE: child table %u has different "
1680+
"type for column \"%s\"",
1681+
childrelid, colDef->colname);
1682+
1683+
childatt->attinhcount++;
1684+
simple_heap_update(attrdesc, &tuple->t_self, tuple);
1685+
CatalogUpdateIndexes(attrdesc, tuple);
1686+
1687+
elog(NOTICE, "ALTER TABLE: merging definition of column "
1688+
"\"%s\" for child %u", colDef->colname, childrelid);
1689+
1690+
heap_close(attrdesc, RowExclusiveLock);
1691+
heap_freetuple(tuple);
1692+
ReleaseSysCache(typeTuple);
16621693
}
16631694
}
16641695
else

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