From df7b8c780fad226b9635b7530018f391acfe7055 Mon Sep 17 00:00:00 2001 From: amit Date: Wed, 9 Aug 2017 15:52:36 +0900 Subject: [PATCH 2/2] Teach expand_inherited_rtentry to use partition bound order After locking the child tables using find_all_inheritors, we discard the list of child table OIDs that it generates and rebuild the same using the information returned by RelationGetPartitionDispatchInfo. --- src/backend/optimizer/prep/prepunion.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c index e73c819901..68d0d8efa3 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -33,6 +33,7 @@ #include "access/heapam.h" #include "access/htup_details.h" #include "access/sysattr.h" +#include "catalog/partition.h" #include "catalog/pg_inherits_fn.h" #include "catalog/pg_type.h" #include "miscadmin.h" @@ -1452,6 +1453,38 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti) */ oldrelation = heap_open(parentOID, NoLock); + /* + * For partitioned tables, we arrange the child table OIDs such that they + * appear in the partition bound order. + */ + if (rte->relkind == RELKIND_PARTITIONED_TABLE) + { + List *leaf_part_oids, + *ptinfos; + + /* Discard the original list. */ + list_free(inhOIDs); + inhOIDs = NIL; + + /* Request partitioning information. */ + RelationGetPartitionDispatchInfo(oldrelation, &ptinfos, + &leaf_part_oids); + + /* + * First collect the partitioned child table OIDs, which includes the + * root parent at the head. + */ + foreach(l, ptinfos) + { + PartitionedTableInfo *ptinfo = lfirst(l); + + inhOIDs = lappend_oid(inhOIDs, ptinfo->relid); + } + + /* Concatenate the leaf partition OIDs. */ + inhOIDs = list_concat(inhOIDs, leaf_part_oids); + } + /* Scan the inheritance set and expand it */ appinfos = NIL; has_child = false; -- 2.11.0