Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit eddad67

Browse files
committed
Clean up MergeAttributesIntoExisting()
Make variable naming clearer and more consistent. Move some variables to smaller scope. Remove some unnecessary intermediate variables. Try to save some vertical space. Apply analogous changes to nearby MergeConstraintsIntoExisting() and RemoveInheritance() for consistency. Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da%40eisentraut.org
1 parent eb36c6a commit eddad67

File tree

1 file changed

+48
-75
lines changed

1 file changed

+48
-75
lines changed

src/backend/commands/tablecmds.c

Lines changed: 48 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15695,94 +15695,78 @@ static void
1569515695
MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1569615696
{
1569715697
Relation attrrel;
15698-
AttrNumber parent_attno;
15699-
int parent_natts;
15700-
TupleDesc tupleDesc;
15701-
HeapTuple tuple;
15702-
bool child_is_partition = false;
15698+
TupleDesc parent_desc;
1570315699

1570415700
attrrel = 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_attribute attribute = TupleDescAttr(tupleDesc,
15716-
parent_attno - 1);
15717-
char *attributeName = NameStr(attribute->attname);
15705+
Form_pg_attribute parent_att = TupleDescAttr(parent_desc, parent_attno - 1);
15706+
char *parent_attname = NameStr(parent_att->attname);
15707+
HeapTuple tuple;
1571815708

1571915709
/* Ignore dropped columns in the parent. */
15720-
if (attribute->attisdropped)
15710+
if (parent_att->attisdropped)
1572115711
continue;
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);
1572615715
if (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)
1573315721
ereport(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)
1574015727
ereport(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
{
1575415740
HeapTuple contup;
1575515741

1575615742
contup = findNotNullConstraintAttnum(RelationGetRelid(parent_rel),
15757-
attribute->attnum);
15743+
parent_att->attnum);
1575815744
if (HeapTupleIsValid(contup) &&
1575915745
!((Form_pg_constraint) GETSTRUCT(contup))->connoinherit)
1576015746
ereport(ERROR,
1576115747
errcode(ERRCODE_DATATYPE_MISMATCH),
1576215748
errmsg("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)
1577015756
ereport(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)
1577515760
ereport(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)
1578615770
ereport(ERROR,
1578715771
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1578815772
errmsg("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

1580115785
CatalogTupleUpdate(attrrel, &tuple->t_self, tuple);
@@ -15805,8 +15789,7 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1580515789
{
1580615790
ereport(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)
1583315816
static void
1583415817
MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1583515818
{
15836-
Relation catalog_relation;
15837-
TupleDesc tuple_desc;
15819+
Relation constraintrel;
1583815820
SysScanDesc parent_scan;
1583915821
ScanKeyData parent_key;
1584015822
HeapTuple parent_tuple;
1584115823
Oid parent_relid = RelationGetRelid(parent_rel);
15842-
bool child_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 */
1585215828
ScanKeyInit(&parent_key,
1585315829
Anum_pg_constraint_conrelid,
1585415830
BTEqualStrategyNumber, F_OIDEQ,
1585515831
ObjectIdGetDatum(parent_relid));
15856-
parent_scan = systable_beginscan(catalog_relation, ConstraintRelidTypidNameIndexId,
15832+
parent_scan = systable_beginscan(constraintrel, ConstraintRelidTypidNameIndexId,
1585715833
true, NULL, 1, &parent_key);
1585815834

1585915835
while (HeapTupleIsValid(parent_tuple = systable_getnext(parent_scan)))
@@ -15877,7 +15853,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1587715853
Anum_pg_constraint_conrelid,
1587815854
BTEqualStrategyNumber, F_OIDEQ,
1587915855
ObjectIdGetDatum(RelationGetRelid(child_rel)));
15880-
child_scan = systable_beginscan(catalog_relation, ConstraintRelidTypidNameIndexId,
15856+
child_scan = systable_beginscan(constraintrel, ConstraintRelidTypidNameIndexId,
1588115857
true, NULL, 1, &child_key);
1588215858

1588315859
while (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+
}
1589915877
else if (child_con->contype == CONSTRAINT_NOTNULL)
1590015878
{
1590115879
AttrNumber parent_attno = extractNotNullColumn(parent_tuple);
@@ -15908,12 +15886,11 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1590815886
}
1590915887

1591015888
if (child_con->contype == CONSTRAINT_CHECK &&
15911-
!constraints_equivalent(parent_tuple, child_tuple, tuple_desc))
15889+
!constraints_equivalent(parent_tuple, child_tuple, RelationGetDescr(constraintrel)))
1591215890
ereport(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)
1593115908
ereport(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)
1594215918
ereport(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
{
1597015945
Assert(child_con->coninhcount == 1);
1597115946
child_con->conislocal = false;
1597215947
}
1597315948

15974-
CatalogTupleUpdate(catalog_relation, &child_copy->t_self, child_copy);
15949+
CatalogTupleUpdate(constraintrel, &child_copy->t_self, child_copy);
1597515950
heap_freetuple(child_copy);
1597615951

1597715952
found = true;
@@ -15998,7 +15973,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1599815973
}
1599915974

1600015975
systable_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)
1615416129
List *connames;
1615516130
List *nncolumns;
1615616131
bool found;
16157-
bool child_is_partition = false;
16132+
bool is_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

1616316136
found = DeleteInheritsTuple(RelationGetRelid(child_rel),
1616416137
RelationGetRelid(parent_rel),
1616516138
expect_detached,
1616616139
RelationGetRelationName(child_rel));
1616716140
if (!found)
1616816141
{
16169-
if (child_is_partition)
16142+
if (is_partitioning)
1617016143
ereport(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)
1632016293
drop_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

0 commit comments

Comments
 (0)