@@ -67,46 +67,39 @@ static void build_child_join_reltarget(PlannerInfo *root,
67
67
68
68
/*
69
69
* setup_simple_rel_arrays
70
- * Prepare the arrays we use for quickly accessing base relations.
70
+ * Prepare the arrays we use for quickly accessing base relations
71
+ * and AppendRelInfos.
71
72
*/
72
73
void
73
74
setup_simple_rel_arrays (PlannerInfo * root )
74
75
{
76
+ int size ;
75
77
Index rti ;
76
78
ListCell * lc ;
77
79
78
80
/* Arrays are accessed using RT indexes (1..N) */
79
- root -> simple_rel_array_size = list_length (root -> parse -> rtable ) + 1 ;
81
+ size = list_length (root -> parse -> rtable ) + 1 ;
82
+ root -> simple_rel_array_size = size ;
80
83
81
- /* simple_rel_array is initialized to all NULLs */
84
+ /*
85
+ * simple_rel_array is initialized to all NULLs, since no RelOptInfos
86
+ * exist yet. It'll be filled by later calls to build_simple_rel().
87
+ */
82
88
root -> simple_rel_array = (RelOptInfo * * )
83
- palloc0 (root -> simple_rel_array_size * sizeof (RelOptInfo * ));
89
+ palloc0 (size * sizeof (RelOptInfo * ));
84
90
85
91
/* simple_rte_array is an array equivalent of the rtable list */
86
92
root -> simple_rte_array = (RangeTblEntry * * )
87
- palloc0 (root -> simple_rel_array_size * sizeof (RangeTblEntry * ));
93
+ palloc0 (size * sizeof (RangeTblEntry * ));
88
94
rti = 1 ;
89
95
foreach (lc , root -> parse -> rtable )
90
96
{
91
97
RangeTblEntry * rte = (RangeTblEntry * ) lfirst (lc );
92
98
93
99
root -> simple_rte_array [rti ++ ] = rte ;
94
100
}
95
- }
96
-
97
- /*
98
- * setup_append_rel_array
99
- * Populate the append_rel_array to allow direct lookups of
100
- * AppendRelInfos by child relid.
101
- *
102
- * The array remains unallocated if there are no AppendRelInfos.
103
- */
104
- void
105
- setup_append_rel_array (PlannerInfo * root )
106
- {
107
- ListCell * lc ;
108
- int size = list_length (root -> parse -> rtable ) + 1 ;
109
101
102
+ /* append_rel_array is not needed if there are no AppendRelInfos */
110
103
if (root -> append_rel_list == NIL )
111
104
{
112
105
root -> append_rel_array = NULL ;
@@ -116,6 +109,12 @@ setup_append_rel_array(PlannerInfo *root)
116
109
root -> append_rel_array = (AppendRelInfo * * )
117
110
palloc0 (size * sizeof (AppendRelInfo * ));
118
111
112
+ /*
113
+ * append_rel_array is filled with any already-existing AppendRelInfos,
114
+ * which currently could only come from UNION ALL flattening. We might
115
+ * add more later during inheritance expansion, but it's the
116
+ * responsibility of the expansion code to update the array properly.
117
+ */
119
118
foreach (lc , root -> append_rel_list )
120
119
{
121
120
AppendRelInfo * appinfo = lfirst_node (AppendRelInfo , lc );
@@ -135,6 +134,10 @@ setup_append_rel_array(PlannerInfo *root)
135
134
* expand_planner_arrays
136
135
* Expand the PlannerInfo's per-RTE arrays by add_size members
137
136
* and initialize the newly added entries to NULLs
137
+ *
138
+ * Note: this causes the append_rel_array to become allocated even if
139
+ * it was not before. This is okay for current uses, because we only call
140
+ * this when adding child relations, which always have AppendRelInfos.
138
141
*/
139
142
void
140
143
expand_planner_arrays (PlannerInfo * root , int add_size )
@@ -145,18 +148,18 @@ expand_planner_arrays(PlannerInfo *root, int add_size)
145
148
146
149
new_size = root -> simple_rel_array_size + add_size ;
147
150
148
- root -> simple_rte_array = (RangeTblEntry * * )
149
- repalloc (root -> simple_rte_array ,
150
- sizeof (RangeTblEntry * ) * new_size );
151
- MemSet (root -> simple_rte_array + root -> simple_rel_array_size ,
152
- 0 , sizeof (RangeTblEntry * ) * add_size );
153
-
154
151
root -> simple_rel_array = (RelOptInfo * * )
155
152
repalloc (root -> simple_rel_array ,
156
153
sizeof (RelOptInfo * ) * new_size );
157
154
MemSet (root -> simple_rel_array + root -> simple_rel_array_size ,
158
155
0 , sizeof (RelOptInfo * ) * add_size );
159
156
157
+ root -> simple_rte_array = (RangeTblEntry * * )
158
+ repalloc (root -> simple_rte_array ,
159
+ sizeof (RangeTblEntry * ) * new_size );
160
+ MemSet (root -> simple_rte_array + root -> simple_rel_array_size ,
161
+ 0 , sizeof (RangeTblEntry * ) * add_size );
162
+
160
163
if (root -> append_rel_array )
161
164
{
162
165
root -> append_rel_array = (AppendRelInfo * * )
0 commit comments