8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.320 2010/01/28 23:21:11 petere Exp $
11
+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.321 2010/02/01 19:28:56 rhaas Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -824,7 +824,7 @@ ExecuteTruncate(TruncateStmt *stmt)
824
824
ListCell * child ;
825
825
List * children ;
826
826
827
- children = find_all_inheritors (myrelid , AccessExclusiveLock );
827
+ children = find_all_inheritors (myrelid , AccessExclusiveLock , NULL );
828
828
829
829
foreach (child , children )
830
830
{
@@ -1943,7 +1943,7 @@ renameatt(Oid myrelid,
1943
1943
const char * oldattname ,
1944
1944
const char * newattname ,
1945
1945
bool recurse ,
1946
- bool recursing )
1946
+ int expected_parents )
1947
1947
{
1948
1948
Relation targetrelation ;
1949
1949
Relation attrelation ;
@@ -1987,33 +1987,42 @@ renameatt(Oid myrelid,
1987
1987
*/
1988
1988
if (recurse )
1989
1989
{
1990
- ListCell * child ;
1991
- List * children ;
1990
+ List * child_oids , * child_numparents ;
1991
+ ListCell * lo , * li ;
1992
1992
1993
- children = find_all_inheritors (myrelid , AccessExclusiveLock );
1993
+ /*
1994
+ * we need the number of parents for each child so that the recursive
1995
+ * calls to renameatt() can determine whether there are any parents
1996
+ * outside the inheritance hierarchy being processed.
1997
+ */
1998
+ child_oids = find_all_inheritors (myrelid , AccessExclusiveLock ,
1999
+ & child_numparents );
1994
2000
1995
2001
/*
1996
2002
* find_all_inheritors does the recursive search of the inheritance
1997
2003
* hierarchy, so all we have to do is process all of the relids in the
1998
2004
* list that it returns.
1999
2005
*/
2000
- foreach ( child , children )
2006
+ forboth ( lo , child_oids , li , child_numparents )
2001
2007
{
2002
- Oid childrelid = lfirst_oid (child );
2008
+ Oid childrelid = lfirst_oid (lo );
2009
+ int numparents = lfirst_int (li );
2003
2010
2004
2011
if (childrelid == myrelid )
2005
2012
continue ;
2006
2013
/* note we need not recurse again */
2007
- renameatt (childrelid , oldattname , newattname , false, true );
2014
+ renameatt (childrelid , oldattname , newattname , false, numparents );
2008
2015
}
2009
2016
}
2010
2017
else
2011
2018
{
2012
2019
/*
2013
2020
* If we are told not to recurse, there had better not be any child
2014
2021
* tables; else the rename would put them out of step.
2022
+ *
2023
+ * expected_parents will only be 0 if we are not already recursing.
2015
2024
*/
2016
- if (! recursing &&
2025
+ if (expected_parents == 0 &&
2017
2026
find_inheritance_children (myrelid , NoLock ) != NIL )
2018
2027
ereport (ERROR ,
2019
2028
(errcode (ERRCODE_INVALID_TABLE_DEFINITION ),
@@ -2039,10 +2048,15 @@ renameatt(Oid myrelid,
2039
2048
oldattname )));
2040
2049
2041
2050
/*
2042
- * if the attribute is inherited, forbid the renaming, unless we are
2043
- * already inside a recursive rename.
2051
+ * if the attribute is inherited, forbid the renaming. if this is a
2052
+ * top-level call to renameatt(), then expected_parents will be 0, so the
2053
+ * effect of this code will be to prohibit the renaming if the attribute
2054
+ * is inherited at all. if this is a recursive call to renameatt(),
2055
+ * expected_parents will be the number of parents the current relation has
2056
+ * within the inheritance hierarchy being processed, so we'll prohibit
2057
+ * the renaming only if there are additional parents from elsewhere.
2044
2058
*/
2045
- if (attform -> attinhcount > 0 && ! recursing )
2059
+ if (attform -> attinhcount > expected_parents )
2046
2060
ereport (ERROR ,
2047
2061
(errcode (ERRCODE_INVALID_TABLE_DEFINITION ),
2048
2062
errmsg ("cannot rename inherited column \"%s\"" ,
@@ -3410,7 +3424,7 @@ ATSimpleRecursion(List **wqueue, Relation rel,
3410
3424
ListCell * child ;
3411
3425
List * children ;
3412
3426
3413
- children = find_all_inheritors (relid , AccessExclusiveLock );
3427
+ children = find_all_inheritors (relid , AccessExclusiveLock , NULL );
3414
3428
3415
3429
/*
3416
3430
* find_all_inheritors does the recursive search of the inheritance
@@ -7233,7 +7247,7 @@ ATExecAddInherit(Relation child_rel, RangeVar *parent)
7233
7247
* We use weakest lock we can on child's children, namely AccessShareLock.
7234
7248
*/
7235
7249
children = find_all_inheritors (RelationGetRelid (child_rel ),
7236
- AccessShareLock );
7250
+ AccessShareLock , NULL );
7237
7251
7238
7252
if (list_member_oid (children , RelationGetRelid (parent_rel )))
7239
7253
ereport (ERROR ,
0 commit comments