@@ -1323,6 +1323,59 @@ inheritance_planner(PlannerInfo *root)
1323
1323
child_rte -> securityQuals = parent_rte -> securityQuals ;
1324
1324
parent_rte -> securityQuals = NIL ;
1325
1325
1326
+ /*
1327
+ * Mark whether we're planning a query to a partitioned table or an
1328
+ * inheritance parent.
1329
+ */
1330
+ subroot -> inhTargetKind =
1331
+ partitioned_relids ? INHKIND_PARTITIONED : INHKIND_INHERITED ;
1332
+
1333
+ /*
1334
+ * If this child is further partitioned, remember it as a parent.
1335
+ * Since a partitioned table does not have any data, we don't need to
1336
+ * create a plan for it, and we can stop processing it here. We do,
1337
+ * however, need to remember its modified PlannerInfo for use when
1338
+ * processing its children, since we'll update their varnos based on
1339
+ * the delta from immediate parent to child, not from top to child.
1340
+ *
1341
+ * Note: a very non-obvious point is that we have not yet added
1342
+ * duplicate subquery RTEs to the subroot's rtable. We mustn't,
1343
+ * because then its children would have two sets of duplicates,
1344
+ * confusing matters.
1345
+ */
1346
+ if (child_rte -> inh )
1347
+ {
1348
+ Assert (child_rte -> relkind == RELKIND_PARTITIONED_TABLE );
1349
+ parent_relids = bms_add_member (parent_relids , appinfo -> child_relid );
1350
+ parent_roots [appinfo -> child_relid ] = subroot ;
1351
+
1352
+ continue ;
1353
+ }
1354
+
1355
+ /*
1356
+ * Set the nominal target relation of the ModifyTable node if not
1357
+ * already done. We use the inheritance parent RTE as the nominal
1358
+ * target relation if it's a partitioned table (see just above this
1359
+ * loop). In the non-partitioned parent case, we'll use the first
1360
+ * child relation (even if it's excluded) as the nominal target
1361
+ * relation. Because of the way expand_inherited_rtentry works, the
1362
+ * latter should be the RTE representing the parent table in its role
1363
+ * as a simple member of the inheritance set.
1364
+ *
1365
+ * It would be logically cleaner to *always* use the inheritance
1366
+ * parent RTE as the nominal relation; but that RTE is not otherwise
1367
+ * referenced in the plan in the non-partitioned inheritance case.
1368
+ * Instead the duplicate child RTE created by expand_inherited_rtentry
1369
+ * is used elsewhere in the plan, so using the original parent RTE
1370
+ * would give rise to confusing use of multiple aliases in EXPLAIN
1371
+ * output for what the user will think is the "same" table. OTOH,
1372
+ * it's not a problem in the partitioned inheritance case, because the
1373
+ * duplicate child RTE added for the parent does not appear anywhere
1374
+ * else in the plan tree.
1375
+ */
1376
+ if (nominalRelation < 0 )
1377
+ nominalRelation = appinfo -> child_relid ;
1378
+
1326
1379
/*
1327
1380
* The rowMarks list might contain references to subquery RTEs, so
1328
1381
* make a copy that we can apply ChangeVarNodes to. (Fortunately, the
@@ -1426,56 +1479,9 @@ inheritance_planner(PlannerInfo *root)
1426
1479
/* and we haven't created PlaceHolderInfos, either */
1427
1480
Assert (subroot -> placeholder_list == NIL );
1428
1481
1429
- /*
1430
- * Mark if we're planning a query to a partitioned table or an
1431
- * inheritance parent.
1432
- */
1433
- subroot -> inhTargetKind =
1434
- partitioned_relids ? INHKIND_PARTITIONED : INHKIND_INHERITED ;
1435
-
1436
- /*
1437
- * If the child is further partitioned, remember it as a parent. Since
1438
- * a partitioned table does not have any data, we don't need to create
1439
- * a plan for it. We do, however, need to remember the PlannerInfo for
1440
- * use when processing its children.
1441
- */
1442
- if (child_rte -> inh )
1443
- {
1444
- Assert (child_rte -> relkind == RELKIND_PARTITIONED_TABLE );
1445
- parent_relids =
1446
- bms_add_member (parent_relids , appinfo -> child_relid );
1447
- parent_roots [appinfo -> child_relid ] = subroot ;
1448
-
1449
- continue ;
1450
- }
1451
-
1452
1482
/* Generate Path(s) for accessing this result relation */
1453
1483
grouping_planner (subroot , true, 0.0 /* retrieve all tuples */ );
1454
1484
1455
- /*
1456
- * Set the nomimal target relation of the ModifyTable node if not
1457
- * already done. We use the inheritance parent RTE as the nominal
1458
- * target relation if it's a partitioned table (see just above this
1459
- * loop). In the non-partitioned parent case, we'll use the first
1460
- * child relation (even if it's excluded) as the nominal target
1461
- * relation. Because of the way expand_inherited_rtentry works, the
1462
- * latter should be the RTE representing the parent table in its role
1463
- * as a simple member of the inheritance set.
1464
- *
1465
- * It would be logically cleaner to *always* use the inheritance
1466
- * parent RTE as the nominal relation; but that RTE is not otherwise
1467
- * referenced in the plan in the non-partitioned inheritance case.
1468
- * Instead the duplicate child RTE created by expand_inherited_rtentry
1469
- * is used elsewhere in the plan, so using the original parent RTE
1470
- * would give rise to confusing use of multiple aliases in EXPLAIN
1471
- * output for what the user will think is the "same" table. OTOH,
1472
- * it's not a problem in the partitioned inheritance case, because the
1473
- * duplicate child RTE added for the parent does not appear anywhere
1474
- * else in the plan tree.
1475
- */
1476
- if (nominalRelation < 0 )
1477
- nominalRelation = appinfo -> child_relid ;
1478
-
1479
1485
/*
1480
1486
* Select cheapest path in case there's more than one. We always run
1481
1487
* modification queries to conclusion, so we care only for the
@@ -1493,9 +1499,9 @@ inheritance_planner(PlannerInfo *root)
1493
1499
continue ;
1494
1500
1495
1501
/*
1496
- * Add the current parent's RT index to the partitione_rels set if
1497
- * we're going to create the ModifyTable path for a partitioned root
1498
- * table.
1502
+ * Add the current parent's RT index to the partitioned_relids set if
1503
+ * we're creating the ModifyTable path for a partitioned root table.
1504
+ * (We only care about parents of non-excluded children.)
1499
1505
*/
1500
1506
if (partitioned_relids )
1501
1507
partitioned_relids = bms_add_member (partitioned_relids ,
0 commit comments