@@ -15695,94 +15695,78 @@ static void
1569515695MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1569615696{
1569715697Relationattrrel;
15698- AttrNumberparent_attno;
15699- intparent_natts;
15700- TupleDesctupleDesc;
15701- HeapTupletuple;
15702- boolchild_is_partition = false;
15698+ TupleDescparent_desc;
1570315699
1570415700attrrel = table_open(AttributeRelationId, RowExclusiveLock);
15701+ parent_desc = RelationGetDescr(parent_rel);
1570515702
15706- tupleDesc = RelationGetDescr(parent_rel);
15707- parent_natts = tupleDesc->natts;
15708-
15709- /* If parent_rel is a partitioned table, child_rel must be a partition */
15710- if (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
15711- child_is_partition = true;
15712-
15713- for (parent_attno = 1; parent_attno <= parent_natts; parent_attno++)
15703+ for (AttrNumber parent_attno = 1; parent_attno <= parent_desc->natts; parent_attno++)
1571415704{
15715- Form_pg_attributeattribute = TupleDescAttr(tupleDesc,
15716- parent_attno - 1 );
15717- char *attributeName = NameStr(attribute->attname) ;
15705+ Form_pg_attributeparent_att = TupleDescAttr(parent_desc, parent_attno - 1);
15706+ char *parent_attname = NameStr(parent_att->attname );
15707+ HeapTupletuple ;
1571815708
1571915709/* Ignore dropped columns in the parent. */
15720- if (attribute ->attisdropped)
15710+ if (parent_att ->attisdropped)
1572115711continue;
1572215712
1572315713/* Find same column in child (matching on column name). */
15724- tuple = SearchSysCacheCopyAttName(RelationGetRelid(child_rel),
15725- attributeName);
15714+ tuple = SearchSysCacheCopyAttName(RelationGetRelid(child_rel), parent_attname);
1572615715if (HeapTupleIsValid(tuple))
1572715716{
15728- /* Check they are same type, typmod, and collation */
15729- Form_pg_attribute childatt = (Form_pg_attribute) GETSTRUCT(tuple);
15717+ Form_pg_attribute child_att = (Form_pg_attribute) GETSTRUCT(tuple);
1573015718
15731- if (attribute ->atttypid !=childatt ->atttypid ||
15732- attribute ->atttypmod !=childatt ->atttypmod)
15719+ if (parent_att ->atttypid !=child_att ->atttypid ||
15720+ parent_att ->atttypmod !=child_att ->atttypmod)
1573315721ereport(ERROR,
1573415722(errcode(ERRCODE_DATATYPE_MISMATCH),
1573515723 errmsg("child table \"%s\" has different type for column \"%s\"",
15736- RelationGetRelationName(child_rel),
15737- attributeName)));
15724+ RelationGetRelationName(child_rel), parent_attname)));
1573815725
15739- if (attribute ->attcollation !=childatt ->attcollation)
15726+ if (parent_att ->attcollation !=child_att ->attcollation)
1574015727ereport(ERROR,
1574115728(errcode(ERRCODE_COLLATION_MISMATCH),
1574215729 errmsg("child table \"%s\" has different collation for column \"%s\"",
15743- RelationGetRelationName(child_rel),
15744- attributeName)));
15730+ RelationGetRelationName(child_rel), parent_attname)));
1574515731
1574615732/*
1574715733 * If the parent has a not-null constraint that's not NO INHERIT,
1574815734 * make sure the child has one too.
1574915735 *
1575015736 * Other constraints are checked elsewhere.
1575115737 */
15752- if (attribute ->attnotnull && !childatt ->attnotnull)
15738+ if (parent_att ->attnotnull && !child_att ->attnotnull)
1575315739{
1575415740HeapTuplecontup;
1575515741
1575615742contup = findNotNullConstraintAttnum(RelationGetRelid(parent_rel),
15757- attribute ->attnum);
15743+ parent_att ->attnum);
1575815744if (HeapTupleIsValid(contup) &&
1575915745!((Form_pg_constraint) GETSTRUCT(contup))->connoinherit)
1576015746ereport(ERROR,
1576115747errcode(ERRCODE_DATATYPE_MISMATCH),
1576215748errmsg("column \"%s\" in child table must be marked NOT NULL",
15763- attributeName ));
15749+ parent_attname ));
1576415750}
1576515751
1576615752/*
1576715753 * Child column must be generated if and only if parent column is.
1576815754 */
15769- if (attribute ->attgenerated && !childatt ->attgenerated)
15755+ if (parent_att ->attgenerated && !child_att ->attgenerated)
1577015756ereport(ERROR,
1577115757(errcode(ERRCODE_DATATYPE_MISMATCH),
15772- errmsg("column \"%s\" in child table must be a generated column",
15773- attributeName)));
15774- if (childatt->attgenerated && !attribute->attgenerated)
15758+ errmsg("column \"%s\" in child table must be a generated column", parent_attname)));
15759+ if (child_att->attgenerated && !parent_att->attgenerated)
1577515760ereport(ERROR,
1577615761(errcode(ERRCODE_DATATYPE_MISMATCH),
15777- errmsg("column \"%s\" in child table must not be a generated column",
15778- attributeName)));
15762+ errmsg("column \"%s\" in child table must not be a generated column", parent_attname)));
1577915763
1578015764/*
1578115765 * OK, bump the child column's inheritance count. (If we fail
1578215766 * later on, this change will just roll back.)
1578315767 */
15784- childatt ->attinhcount++;
15785- if (childatt ->attinhcount < 0)
15768+ child_att ->attinhcount++;
15769+ if (child_att ->attinhcount < 0)
1578615770ereport(ERROR,
1578715771errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1578815772errmsg("too many inheritance parents"));
@@ -15792,10 +15776,10 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1579215776 * is same in all partitions. (Note: there are only inherited
1579315777 * attributes in partitions)
1579415778 */
15795- if (child_is_partition )
15779+ if (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE )
1579615780{
15797- Assert(childatt ->attinhcount == 1);
15798- childatt ->attislocal = false;
15781+ Assert(child_att ->attinhcount == 1);
15782+ child_att ->attislocal = false;
1579915783}
1580015784
1580115785CatalogTupleUpdate(attrrel, &tuple->t_self, tuple);
@@ -15805,8 +15789,7 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1580515789{
1580615790ereport(ERROR,
1580715791(errcode(ERRCODE_DATATYPE_MISMATCH),
15808- errmsg("child table is missing column \"%s\"",
15809- attributeName)));
15792+ errmsg("child table is missing column \"%s\"", parent_attname)));
1581015793}
1581115794}
1581215795
@@ -15833,27 +15816,20 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1583315816static void
1583415817MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1583515818{
15836- Relationcatalog_relation;
15837- TupleDesctuple_desc;
15819+ Relationconstraintrel;
1583815820SysScanDesc parent_scan;
1583915821ScanKeyData parent_key;
1584015822HeapTupleparent_tuple;
1584115823Oidparent_relid = RelationGetRelid(parent_rel);
15842- boolchild_is_partition = false;
1584315824
15844- catalog_relation = table_open(ConstraintRelationId, RowExclusiveLock);
15845- tuple_desc = RelationGetDescr(catalog_relation);
15846-
15847- /* If parent_rel is a partitioned table, child_rel must be a partition */
15848- if (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
15849- child_is_partition = true;
15825+ constraintrel = table_open(ConstraintRelationId, RowExclusiveLock);
1585015826
1585115827/* Outer loop scans through the parent's constraint definitions */
1585215828ScanKeyInit(&parent_key,
1585315829Anum_pg_constraint_conrelid,
1585415830BTEqualStrategyNumber, F_OIDEQ,
1585515831ObjectIdGetDatum(parent_relid));
15856- parent_scan = systable_beginscan(catalog_relation , ConstraintRelidTypidNameIndexId,
15832+ parent_scan = systable_beginscan(constraintrel , ConstraintRelidTypidNameIndexId,
1585715833 true, NULL, 1, &parent_key);
1585815834
1585915835while (HeapTupleIsValid(parent_tuple = systable_getnext(parent_scan)))
@@ -15877,7 +15853,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1587715853Anum_pg_constraint_conrelid,
1587815854BTEqualStrategyNumber, F_OIDEQ,
1587915855ObjectIdGetDatum(RelationGetRelid(child_rel)));
15880- child_scan = systable_beginscan(catalog_relation , ConstraintRelidTypidNameIndexId,
15856+ child_scan = systable_beginscan(constraintrel , ConstraintRelidTypidNameIndexId,
1588115857true, NULL, 1, &child_key);
1588215858
1588315859while (HeapTupleIsValid(child_tuple = systable_getnext(child_scan)))
@@ -15892,10 +15868,12 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1589215868 * CHECK constraint are matched by name, NOT NULL ones by
1589315869 * attribute number
1589415870 */
15895- if (child_con->contype == CONSTRAINT_CHECK &&
15896- strcmp(NameStr(parent_con->conname),
15897- NameStr(child_con->conname)) != 0)
15898- continue;
15871+ if (child_con->contype == CONSTRAINT_CHECK)
15872+ {
15873+ if (strcmp(NameStr(parent_con->conname),
15874+ NameStr(child_con->conname)) != 0)
15875+ continue;
15876+ }
1589915877else if (child_con->contype == CONSTRAINT_NOTNULL)
1590015878{
1590115879AttrNumberparent_attno = extractNotNullColumn(parent_tuple);
@@ -15908,12 +15886,11 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1590815886}
1590915887
1591015888if (child_con->contype == CONSTRAINT_CHECK &&
15911- !constraints_equivalent(parent_tuple, child_tuple,tuple_desc ))
15889+ !constraints_equivalent(parent_tuple, child_tuple,RelationGetDescr(constraintrel) ))
1591215890ereport(ERROR,
1591315891(errcode(ERRCODE_DATATYPE_MISMATCH),
1591415892 errmsg("child table \"%s\" has different definition for check constraint \"%s\"",
15915- RelationGetRelationName(child_rel),
15916- NameStr(parent_con->conname))));
15893+ RelationGetRelationName(child_rel), NameStr(parent_con->conname))));
1591715894
1591815895/*
1591915896 * If the CHECK child constraint is "no inherit" then cannot
@@ -15931,8 +15908,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1593115908ereport(ERROR,
1593215909(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1593315910 errmsg("constraint \"%s\" conflicts with non-inherited constraint on child table \"%s\"",
15934- NameStr(child_con->conname),
15935- RelationGetRelationName(child_rel))));
15911+ NameStr(child_con->conname), RelationGetRelationName(child_rel))));
1593615912
1593715913/*
1593815914 * If the child constraint is "not valid" then cannot merge with a
@@ -15942,8 +15918,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1594215918ereport(ERROR,
1594315919(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1594415920 errmsg("constraint \"%s\" conflicts with NOT VALID constraint on child table \"%s\"",
15945- NameStr(child_con->conname),
15946- RelationGetRelationName(child_rel))));
15921+ NameStr(child_con->conname), RelationGetRelationName(child_rel))));
1594715922
1594815923/*
1594915924 * OK, bump the child constraint's inheritance count. (If we fail
@@ -15965,13 +15940,13 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1596515940 * inherited only once since it cannot have multiple parents and
1596615941 * it is never considered local.
1596715942 */
15968- if (child_is_partition )
15943+ if (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE )
1596915944{
1597015945Assert(child_con->coninhcount == 1);
1597115946child_con->conislocal = false;
1597215947}
1597315948
15974- CatalogTupleUpdate(catalog_relation , &child_copy->t_self, child_copy);
15949+ CatalogTupleUpdate(constraintrel , &child_copy->t_self, child_copy);
1597515950heap_freetuple(child_copy);
1597615951
1597715952found = true;
@@ -15998,7 +15973,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1599815973}
1599915974
1600015975systable_endscan(parent_scan);
16001- table_close(catalog_relation , RowExclusiveLock);
15976+ table_close(constraintrel , RowExclusiveLock);
1600215977}
1600315978
1600415979/*
@@ -16154,19 +16129,17 @@ RemoveInheritance(Relation child_rel, Relation parent_rel, bool expect_detached)
1615416129List *connames;
1615516130List *nncolumns;
1615616131boolfound;
16157- boolchild_is_partition = false ;
16132+ boolis_partitioning ;
1615816133
16159- /* If parent_rel is a partitioned table, child_rel must be a partition */
16160- if (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
16161- child_is_partition = true;
16134+ is_partitioning = (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
1616216135
1616316136found = DeleteInheritsTuple(RelationGetRelid(child_rel),
1616416137RelationGetRelid(parent_rel),
1616516138expect_detached,
1616616139RelationGetRelationName(child_rel));
1616716140if (!found)
1616816141{
16169- if (child_is_partition )
16142+ if (is_partitioning )
1617016143ereport(ERROR,
1617116144(errcode(ERRCODE_UNDEFINED_TABLE),
1617216145 errmsg("relation \"%s\" is not a partition of relation \"%s\"",
@@ -16320,7 +16293,7 @@ RemoveInheritance(Relation child_rel, Relation parent_rel, bool expect_detached)
1632016293drop_parent_dependency(RelationGetRelid(child_rel),
1632116294 RelationRelationId,
1632216295 RelationGetRelid(parent_rel),
16323- child_dependency_type(child_is_partition ));
16296+ child_dependency_type(is_partitioning ));
1632416297
1632516298/*
1632616299 * Post alter hook of this inherits. Since object_access_hook doesn't take