@@ -15695,94 +15695,78 @@ static void
15695
15695
MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
15696
15696
{
15697
15697
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;
15703
15699
15704
15700
attrrel = table_open(AttributeRelationId, RowExclusiveLock);
15701
+ parent_desc = RelationGetDescr(parent_rel);
15705
15702
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++)
15714
15704
{
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 ;
15718
15708
15719
15709
/* Ignore dropped columns in the parent. */
15720
- if (attribute ->attisdropped)
15710
+ if (parent_att ->attisdropped)
15721
15711
continue;
15722
15712
15723
15713
/* 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);
15726
15715
if (HeapTupleIsValid(tuple))
15727
15716
{
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);
15730
15718
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)
15733
15721
ereport(ERROR,
15734
15722
(errcode(ERRCODE_DATATYPE_MISMATCH),
15735
15723
errmsg("child table \"%s\" has different type for column \"%s\"",
15736
- RelationGetRelationName(child_rel),
15737
- attributeName)));
15724
+ RelationGetRelationName(child_rel), parent_attname)));
15738
15725
15739
- if (attribute ->attcollation != childatt ->attcollation)
15726
+ if (parent_att ->attcollation != child_att ->attcollation)
15740
15727
ereport(ERROR,
15741
15728
(errcode(ERRCODE_COLLATION_MISMATCH),
15742
15729
errmsg("child table \"%s\" has different collation for column \"%s\"",
15743
- RelationGetRelationName(child_rel),
15744
- attributeName)));
15730
+ RelationGetRelationName(child_rel), parent_attname)));
15745
15731
15746
15732
/*
15747
15733
* If the parent has a not-null constraint that's not NO INHERIT,
15748
15734
* make sure the child has one too.
15749
15735
*
15750
15736
* Other constraints are checked elsewhere.
15751
15737
*/
15752
- if (attribute ->attnotnull && !childatt ->attnotnull)
15738
+ if (parent_att ->attnotnull && !child_att ->attnotnull)
15753
15739
{
15754
15740
HeapTuple contup;
15755
15741
15756
15742
contup = findNotNullConstraintAttnum(RelationGetRelid(parent_rel),
15757
- attribute ->attnum);
15743
+ parent_att ->attnum);
15758
15744
if (HeapTupleIsValid(contup) &&
15759
15745
!((Form_pg_constraint) GETSTRUCT(contup))->connoinherit)
15760
15746
ereport(ERROR,
15761
15747
errcode(ERRCODE_DATATYPE_MISMATCH),
15762
15748
errmsg("column \"%s\" in child table must be marked NOT NULL",
15763
- attributeName ));
15749
+ parent_attname ));
15764
15750
}
15765
15751
15766
15752
/*
15767
15753
* Child column must be generated if and only if parent column is.
15768
15754
*/
15769
- if (attribute ->attgenerated && !childatt ->attgenerated)
15755
+ if (parent_att ->attgenerated && !child_att ->attgenerated)
15770
15756
ereport(ERROR,
15771
15757
(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)
15775
15760
ereport(ERROR,
15776
15761
(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)));
15779
15763
15780
15764
/*
15781
15765
* OK, bump the child column's inheritance count. (If we fail
15782
15766
* later on, this change will just roll back.)
15783
15767
*/
15784
- childatt ->attinhcount++;
15785
- if (childatt ->attinhcount < 0)
15768
+ child_att ->attinhcount++;
15769
+ if (child_att ->attinhcount < 0)
15786
15770
ereport(ERROR,
15787
15771
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
15788
15772
errmsg("too many inheritance parents"));
@@ -15792,10 +15776,10 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
15792
15776
* is same in all partitions. (Note: there are only inherited
15793
15777
* attributes in partitions)
15794
15778
*/
15795
- if (child_is_partition )
15779
+ if (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE )
15796
15780
{
15797
- Assert(childatt ->attinhcount == 1);
15798
- childatt ->attislocal = false;
15781
+ Assert(child_att ->attinhcount == 1);
15782
+ child_att ->attislocal = false;
15799
15783
}
15800
15784
15801
15785
CatalogTupleUpdate(attrrel, &tuple->t_self, tuple);
@@ -15805,8 +15789,7 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
15805
15789
{
15806
15790
ereport(ERROR,
15807
15791
(errcode(ERRCODE_DATATYPE_MISMATCH),
15808
- errmsg("child table is missing column \"%s\"",
15809
- attributeName)));
15792
+ errmsg("child table is missing column \"%s\"", parent_attname)));
15810
15793
}
15811
15794
}
15812
15795
@@ -15833,27 +15816,20 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
15833
15816
static void
15834
15817
MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
15835
15818
{
15836
- Relation catalog_relation;
15837
- TupleDesc tuple_desc;
15819
+ Relation constraintrel;
15838
15820
SysScanDesc parent_scan;
15839
15821
ScanKeyData parent_key;
15840
15822
HeapTuple parent_tuple;
15841
15823
Oid parent_relid = RelationGetRelid(parent_rel);
15842
- bool child_is_partition = false;
15843
15824
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);
15850
15826
15851
15827
/* Outer loop scans through the parent's constraint definitions */
15852
15828
ScanKeyInit(&parent_key,
15853
15829
Anum_pg_constraint_conrelid,
15854
15830
BTEqualStrategyNumber, F_OIDEQ,
15855
15831
ObjectIdGetDatum(parent_relid));
15856
- parent_scan = systable_beginscan(catalog_relation , ConstraintRelidTypidNameIndexId,
15832
+ parent_scan = systable_beginscan(constraintrel , ConstraintRelidTypidNameIndexId,
15857
15833
true, NULL, 1, &parent_key);
15858
15834
15859
15835
while (HeapTupleIsValid(parent_tuple = systable_getnext(parent_scan)))
@@ -15877,7 +15853,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
15877
15853
Anum_pg_constraint_conrelid,
15878
15854
BTEqualStrategyNumber, F_OIDEQ,
15879
15855
ObjectIdGetDatum(RelationGetRelid(child_rel)));
15880
- child_scan = systable_beginscan(catalog_relation , ConstraintRelidTypidNameIndexId,
15856
+ child_scan = systable_beginscan(constraintrel , ConstraintRelidTypidNameIndexId,
15881
15857
true, NULL, 1, &child_key);
15882
15858
15883
15859
while (HeapTupleIsValid(child_tuple = systable_getnext(child_scan)))
@@ -15892,10 +15868,12 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
15892
15868
* CHECK constraint are matched by name, NOT NULL ones by
15893
15869
* attribute number
15894
15870
*/
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
+ }
15899
15877
else if (child_con->contype == CONSTRAINT_NOTNULL)
15900
15878
{
15901
15879
AttrNumber parent_attno = extractNotNullColumn(parent_tuple);
@@ -15908,12 +15886,11 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
15908
15886
}
15909
15887
15910
15888
if (child_con->contype == CONSTRAINT_CHECK &&
15911
- !constraints_equivalent(parent_tuple, child_tuple, tuple_desc ))
15889
+ !constraints_equivalent(parent_tuple, child_tuple, RelationGetDescr(constraintrel) ))
15912
15890
ereport(ERROR,
15913
15891
(errcode(ERRCODE_DATATYPE_MISMATCH),
15914
15892
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))));
15917
15894
15918
15895
/*
15919
15896
* If the CHECK child constraint is "no inherit" then cannot
@@ -15931,8 +15908,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
15931
15908
ereport(ERROR,
15932
15909
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
15933
15910
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))));
15936
15912
15937
15913
/*
15938
15914
* If the child constraint is "not valid" then cannot merge with a
@@ -15942,8 +15918,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
15942
15918
ereport(ERROR,
15943
15919
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
15944
15920
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))));
15947
15922
15948
15923
/*
15949
15924
* OK, bump the child constraint's inheritance count. (If we fail
@@ -15965,13 +15940,13 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
15965
15940
* inherited only once since it cannot have multiple parents and
15966
15941
* it is never considered local.
15967
15942
*/
15968
- if (child_is_partition )
15943
+ if (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE )
15969
15944
{
15970
15945
Assert(child_con->coninhcount == 1);
15971
15946
child_con->conislocal = false;
15972
15947
}
15973
15948
15974
- CatalogTupleUpdate(catalog_relation , &child_copy->t_self, child_copy);
15949
+ CatalogTupleUpdate(constraintrel , &child_copy->t_self, child_copy);
15975
15950
heap_freetuple(child_copy);
15976
15951
15977
15952
found = true;
@@ -15998,7 +15973,7 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
15998
15973
}
15999
15974
16000
15975
systable_endscan(parent_scan);
16001
- table_close(catalog_relation , RowExclusiveLock);
15976
+ table_close(constraintrel , RowExclusiveLock);
16002
15977
}
16003
15978
16004
15979
/*
@@ -16154,19 +16129,17 @@ RemoveInheritance(Relation child_rel, Relation parent_rel, bool expect_detached)
16154
16129
List *connames;
16155
16130
List *nncolumns;
16156
16131
bool found;
16157
- bool child_is_partition = false ;
16132
+ bool is_partitioning ;
16158
16133
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);
16162
16135
16163
16136
found = DeleteInheritsTuple(RelationGetRelid(child_rel),
16164
16137
RelationGetRelid(parent_rel),
16165
16138
expect_detached,
16166
16139
RelationGetRelationName(child_rel));
16167
16140
if (!found)
16168
16141
{
16169
- if (child_is_partition )
16142
+ if (is_partitioning )
16170
16143
ereport(ERROR,
16171
16144
(errcode(ERRCODE_UNDEFINED_TABLE),
16172
16145
errmsg("relation \"%s\" is not a partition of relation \"%s\"",
@@ -16320,7 +16293,7 @@ RemoveInheritance(Relation child_rel, Relation parent_rel, bool expect_detached)
16320
16293
drop_parent_dependency(RelationGetRelid(child_rel),
16321
16294
RelationRelationId,
16322
16295
RelationGetRelid(parent_rel),
16323
- child_dependency_type(child_is_partition ));
16296
+ child_dependency_type(is_partitioning ));
16324
16297
16325
16298
/*
16326
16299
* Post alter hook of this inherits. Since object_access_hook doesn't take
0 commit comments