@ -6734,6 +6734,7 @@ ATExecDropConstraint(Relation rel, const char *constrName,
{
{
Oid childrelid = lfirst_oid ( child ) ;
Oid childrelid = lfirst_oid ( child ) ;
Relation childrel ;
Relation childrel ;
HeapTuple copy_tuple ;
/* find_inheritance_children already got lock */
/* find_inheritance_children already got lock */
childrel = heap_open ( childrelid , NoLock ) ;
childrel = heap_open ( childrelid , NoLock ) ;
@ -6746,83 +6747,79 @@ ATExecDropConstraint(Relation rel, const char *constrName,
scan = systable_beginscan ( conrel , ConstraintRelidIndexId ,
scan = systable_beginscan ( conrel , ConstraintRelidIndexId ,
true , SnapshotNow , 1 , & key ) ;
true , SnapshotNow , 1 , & key ) ;
found = false ;
/* scan for matching tuple - there should only be one */
while ( HeapTupleIsValid ( tuple = systable_getnext ( scan ) ) )
while ( HeapTupleIsValid ( tuple = systable_getnext ( scan ) ) )
{
{
HeapTuple copy_tuple ;
con = ( Form_pg_constraint ) GETSTRUCT ( tuple ) ;
con = ( Form_pg_constraint ) GETSTRUCT ( tuple ) ;
/* Right now only CHECK constraints can be inherited */
/* Right now only CHECK constraints can be inherited */
if ( con - > contype ! = CONSTRAINT_CHECK )
if ( con - > contype ! = CONSTRAINT_CHECK )
continue ;
continue ;
if ( strcmp ( NameStr ( con - > conname ) , constrName ) ! = 0 )
if ( strcmp ( NameStr ( con - > conname ) , constrName ) = = 0 )
continue ;
break ;
}
found = true ;
if ( ! HeapTupleIsValid ( tuple ) )
ereport ( ERROR ,
( errcode ( ERRCODE_UNDEFINED_OBJECT ) ,
errmsg ( " constraint \" %s \" of relation \" %s \" does not exist " ,
constrName ,
RelationGetRelationName ( childrel ) ) ) ) ;
if ( con - > coninhcount < = 0 ) /* shouldn't happen */
copy_tuple = heap_copytuple ( tuple ) ;
elog ( ERROR , " relation %u has non-inherited constraint \" %s \" " ,
childrelid , constrName ) ;
copy_tuple = heap_copytuple ( tuple ) ;
systable_endscan ( scan ) ;
con = ( Form_pg_constraint ) GETSTRUCT ( copy_tuple ) ;
if ( recurse )
con = ( Form_pg_constraint ) GETSTRUCT ( copy_tuple ) ;
{
/*
* If the child constraint has other definition sources , just
* decrement its inheritance count ; if not , recurse to delete
* it .
*/
if ( con - > coninhcount = = 1 & & ! con - > conislocal )
{
/* Time to delete this child constraint, too */
ATExecDropConstraint ( childrel , constrName , behavior ,
true , true ,
false , lockmode ) ;
}
else
{
/* Child constraint must survive my deletion */
con - > coninhcount - - ;
simple_heap_update ( conrel , & copy_tuple - > t_self , copy_tuple ) ;
CatalogUpdateIndexes ( conrel , copy_tuple ) ;
/* Make update visible */
if ( con - > coninhcount < = 0 ) /* shouldn't happen */
CommandCounterIncrement ( ) ;
elog ( ERROR , " relation %u has non-inherited constraint \" %s \" " ,
}
childrelid , constrName ) ;
if ( recurse )
{
/*
* If the child constraint has other definition sources , just
* decrement its inheritance count ; if not , recurse to delete
* it .
*/
if ( con - > coninhcount = = 1 & & ! con - > conislocal )
{
/* Time to delete this child constraint, too */
ATExecDropConstraint ( childrel , constrName , behavior ,
true , true ,
false , lockmode ) ;
}
}
else
else
{
{
/*
/* Child constraint must survive my deletion */
* If we were told to drop ONLY in this table ( no recursion ) ,
* we need to mark the inheritors ' constraints as locally
* defined rather than inherited .
*/
con - > coninhcount - - ;
con - > coninhcount - - ;
con - > conislocal = true ;
simple_heap_update ( conrel , & copy_tuple - > t_self , copy_tuple ) ;
simple_heap_update ( conrel , & copy_tuple - > t_self , copy_tuple ) ;
CatalogUpdateIndexes ( conrel , copy_tuple ) ;
CatalogUpdateIndexes ( conrel , copy_tuple ) ;
/* Make update visible */
/* Make update visible */
CommandCounterIncrement ( ) ;
CommandCounterIncrement ( ) ;
}
}
heap_freetuple ( copy_tuple ) ;
}
}
else
{
/*
* If we were told to drop ONLY in this table ( no recursion ) ,
* we need to mark the inheritors ' constraints as locally
* defined rather than inherited .
*/
con - > coninhcount - - ;
con - > conislocal = true ;
systable_endscan ( scan ) ;
simple_heap_update ( conrel , & copy_tuple - > t_self , copy_tuple ) ;
CatalogUpdateIndexes ( conrel , copy_tuple ) ;
if ( ! found )
/* Make update visible */
ereport ( ERROR ,
CommandCounterIncrement ( ) ;
( errcode ( ERRCODE_UNDEFINED_OBJECT ) ,
}
errmsg ( " constraint \" %s \" of relation \" %s \" does not exist " ,
constrName ,
heap_freetuple ( copy_tuple ) ;
RelationGetRelationName ( childrel ) ) ) ) ;
heap_close ( childrel , NoLock ) ;
heap_close ( childrel , NoLock ) ;
}
}