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

Commit2a01b05

Browse files
committed
Code review for recent patch to allow ALTER TABLE ADD COLUMN when
a child table already has a matching column. Acquire appropriatelock on child table; do the right thing with any CHECK constraintsattached to the new parent column.
1 parente02d1ab commit2a01b05

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 32 additions & 15 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.50 2002/10/21 22:06:19 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.51 2002/11/02 22:02:08 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1649,6 +1649,7 @@ AlterTableAddColumn(Oid myrelid,
16491649
*children;
16501650
ColumnDef*colDefChild=copyObject(colDef);
16511651

1652+
/* Child should see column as singly inherited */
16521653
colDefChild->inhcount=1;
16531654
colDefChild->is_local= false;
16541655

@@ -1665,37 +1666,53 @@ AlterTableAddColumn(Oid myrelid,
16651666
if (childrelid==myrelid)
16661667
continue;
16671668

1669+
childrel=heap_open(childrelid,AccessExclusiveLock);
1670+
1671+
/* Does child already have a column by this name? */
16681672
attrdesc=heap_openr(AttributeRelationName,RowExclusiveLock);
16691673
tuple=SearchSysCacheCopyAttName(childrelid,colDef->colname);
16701674
if (!HeapTupleIsValid(tuple))
16711675
{
1676+
/* No, recurse to add it normally */
16721677
heap_close(attrdesc,RowExclusiveLock);
1678+
heap_close(childrel,NoLock);
16731679
AlterTableAddColumn(childrelid, true,colDefChild);
16741680
continue;
16751681
}
16761682
childatt= (Form_pg_attribute)GETSTRUCT(tuple);
16771683

1678-
typeTuple=typenameType(colDef->typename);
1684+
/* Okay if child matches by type */
1685+
if (typenameTypeId(colDef->typename)!=childatt->atttypid||
1686+
colDef->typename->typmod!=childatt->atttypmod)
1687+
elog(ERROR,"ALTER TABLE: child table \"%s\" has different type for column \"%s\"",
1688+
get_rel_name(childrelid),colDef->colname);
16791689

1680-
if (HeapTupleGetOid(typeTuple)!=childatt->atttypid||
1681-
colDef->typename->typmod!=childatt->atttypmod)
1682-
elog(ERROR,"ALTER TABLE: child table %u has different "
1683-
"type for column \"%s\"",
1684-
childrelid,colDef->colname);
1690+
/*
1691+
* XXX if we supported NOT NULL or defaults, would need to do
1692+
* more work here to verify child matches
1693+
*/
16851694

1695+
elog(NOTICE,"ALTER TABLE: merging definition of column \"%s\" for child %s",
1696+
colDef->colname,get_rel_name(childrelid));
1697+
1698+
/* Bump the existing child att's inhcount */
16861699
childatt->attinhcount++;
16871700
simple_heap_update(attrdesc,&tuple->t_self,tuple);
16881701
CatalogUpdateIndexes(attrdesc,tuple);
1689-
1690-
childrel=RelationIdGetRelation(childrelid);
1691-
elog(NOTICE,"ALTER TABLE: merging definition of column "
1692-
"\"%s\" for child %s",colDef->colname,
1693-
RelationGetRelationName(childrel));
1694-
RelationClose(childrel);
16951702

1696-
heap_close(attrdesc,RowExclusiveLock);
1703+
/*
1704+
* Propagate any new CHECK constraints into the child table
1705+
* and its descendants
1706+
*/
1707+
if (colDef->constraints!=NIL)
1708+
{
1709+
CommandCounterIncrement();
1710+
AlterTableAddConstraint(childrelid, true,colDef->constraints);
1711+
}
1712+
16971713
heap_freetuple(tuple);
1698-
ReleaseSysCache(typeTuple);
1714+
heap_close(attrdesc,RowExclusiveLock);
1715+
heap_close(childrel,NoLock);
16991716
}
17001717
}
17011718
else

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp