* Check constraints in child table match up with constraints in parent, * and increment their coninhcount. * * Constraints that are marked ONLY in the parent are ignored. * * Called by CreateInheritance * * Currently all constraints in parent must be present in the child. One day we * may consider adding new constraints like CREATE TABLE does. * * XXX This is O(N^2) which may be an issue
| 17306 | * XXX See MergeWithExistingConstraint too if you change this code. |
| 17307 | */ |
| 17308 | static void |
| 17309 | MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel) |
| 17310 | { |
| 17311 | Relation catalog_relation; |
| 17312 | TupleDesc tuple_desc; |
| 17313 | SysScanDesc parent_scan; |
| 17314 | ScanKeyData parent_key; |
| 17315 | HeapTuple parent_tuple; |
| 17316 | bool child_is_partition = false; |
| 17317 | |
| 17318 | catalog_relation = table_open(ConstraintRelationId, RowExclusiveLock); |
| 17319 | tuple_desc = RelationGetDescr(catalog_relation); |
| 17320 | |
| 17321 | /* If parent_rel is a partitioned table, child_rel must be a partition */ |
| 17322 | if (parent_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) |
| 17323 | child_is_partition = true; |
| 17324 | |
| 17325 | /* Outer loop scans through the parent's constraint definitions */ |
| 17326 | ScanKeyInit(&parent_key, |
| 17327 | Anum_pg_constraint_conrelid, |
| 17328 | BTEqualStrategyNumber, F_OIDEQ, |
| 17329 | ObjectIdGetDatum(RelationGetRelid(parent_rel))); |
| 17330 | parent_scan = systable_beginscan(catalog_relation, ConstraintRelidTypidNameIndexId, |
| 17331 | true, NULL, 1, &parent_key); |
| 17332 | |
| 17333 | while (HeapTupleIsValid(parent_tuple = systable_getnext(parent_scan))) |
| 17334 | { |
| 17335 | Form_pg_constraint parent_con = (Form_pg_constraint) GETSTRUCT(parent_tuple); |
| 17336 | SysScanDesc child_scan; |
| 17337 | ScanKeyData child_key; |
| 17338 | HeapTuple child_tuple; |
| 17339 | bool found = false; |
| 17340 | |
| 17341 | if (parent_con->contype != CONSTRAINT_CHECK) |
| 17342 | continue; |
| 17343 | |
| 17344 | /* if the parent's constraint is marked NO INHERIT, it's not inherited */ |
| 17345 | if (parent_con->connoinherit) |
| 17346 | continue; |
| 17347 | |
| 17348 | /* Search for a child constraint matching this one */ |
| 17349 | ScanKeyInit(&child_key, |
| 17350 | Anum_pg_constraint_conrelid, |
| 17351 | BTEqualStrategyNumber, F_OIDEQ, |
| 17352 | ObjectIdGetDatum(RelationGetRelid(child_rel))); |
| 17353 | child_scan = systable_beginscan(catalog_relation, ConstraintRelidTypidNameIndexId, |
| 17354 | true, NULL, 1, &child_key); |
| 17355 | |
| 17356 | while (HeapTupleIsValid(child_tuple = systable_getnext(child_scan))) |
| 17357 | { |
| 17358 | Form_pg_constraint child_con = (Form_pg_constraint) GETSTRUCT(child_tuple); |
| 17359 | HeapTuple child_copy; |
| 17360 | |
| 17361 | if (child_con->contype != CONSTRAINT_CHECK) |
| 17362 | continue; |
| 17363 | |
| 17364 | if (strcmp(NameStr(parent_con->conname), |
| 17365 | NameStr(child_con->conname)) != 0) |
no test coverage detected