* CreateInheritance * Catalog manipulation portion of creating inheritance between a child * table and a parent table. * * Common to ATExecAddInherit() and ATExecAttachPartition(). */
| 17012 | * Common to ATExecAddInherit() and ATExecAttachPartition(). |
| 17013 | */ |
| 17014 | static void |
| 17015 | CreateInheritance(Relation child_rel, Relation parent_rel) |
| 17016 | { |
| 17017 | Relation catalogRelation; |
| 17018 | SysScanDesc scan; |
| 17019 | ScanKeyData key; |
| 17020 | HeapTuple inheritsTuple; |
| 17021 | int32 inhseqno; |
| 17022 | |
| 17023 | /* Note: get RowExclusiveLock because we will write pg_inherits below. */ |
| 17024 | catalogRelation = table_open(InheritsRelationId, RowExclusiveLock); |
| 17025 | |
| 17026 | /* |
| 17027 | * Check for duplicates in the list of parents, and determine the highest |
| 17028 | * inhseqno already present; we'll use the next one for the new parent. |
| 17029 | * Also, if proposed child is a partition, it cannot already be |
| 17030 | * inheriting. |
| 17031 | * |
| 17032 | * Note: we do not reject the case where the child already inherits from |
| 17033 | * the parent indirectly; CREATE TABLE doesn't reject comparable cases. |
| 17034 | */ |
| 17035 | ScanKeyInit(&key, |
| 17036 | Anum_pg_inherits_inhrelid, |
| 17037 | BTEqualStrategyNumber, F_OIDEQ, |
| 17038 | ObjectIdGetDatum(RelationGetRelid(child_rel))); |
| 17039 | scan = systable_beginscan(catalogRelation, InheritsRelidSeqnoIndexId, |
| 17040 | true, NULL, 1, &key); |
| 17041 | |
| 17042 | /* inhseqno sequences start at 1 */ |
| 17043 | inhseqno = 0; |
| 17044 | while (HeapTupleIsValid(inheritsTuple = systable_getnext(scan))) |
| 17045 | { |
| 17046 | Form_pg_inherits inh = (Form_pg_inherits) GETSTRUCT(inheritsTuple); |
| 17047 | |
| 17048 | if (inh->inhparent == RelationGetRelid(parent_rel)) |
| 17049 | ereport(ERROR, |
| 17050 | (errcode(ERRCODE_DUPLICATE_TABLE), |
| 17051 | errmsg("relation \"%s\" would be inherited from more than once", |
| 17052 | RelationGetRelationName(parent_rel)))); |
| 17053 | |
| 17054 | if (inh->inhseqno > inhseqno) |
| 17055 | inhseqno = inh->inhseqno; |
| 17056 | } |
| 17057 | systable_endscan(scan); |
| 17058 | |
| 17059 | /* Match up the columns and bump attinhcount and attislocal */ |
| 17060 | MergeAttributesIntoExisting(child_rel, parent_rel); |
| 17061 | |
| 17062 | /* Match up the constraints and bump coninhcount as needed */ |
| 17063 | MergeConstraintsIntoExisting(child_rel, parent_rel); |
| 17064 | |
| 17065 | /* |
| 17066 | * OK, it looks valid. Make the catalog entries that show inheritance. |
| 17067 | */ |
| 17068 | StoreCatalogInheritance1(RelationGetRelid(child_rel), |
| 17069 | RelationGetRelid(parent_rel), |
| 17070 | inhseqno + 1, |
| 17071 | catalogRelation, |
no test coverage detected