@@ -205,36 +205,28 @@ pick_suitable_index(Oid relation, AttrNumber column)
205
205
index_am = index -> rd_rel -> relam ;
206
206
index_close (index , AccessShareLock );
207
207
208
- /* check if this is a valid GIST index with no predicates */
208
+ /*
209
+ * check if this is a valid GIST index with no predicates and
210
+ * its first column is the required spoint with opclass 'spoint2'.
211
+ */
209
212
if (index_am == GIST_AM_OID && pg_ind -> indisvalid &&
210
- heap_attisnull (htup , Anum_pg_index_indpred ))
213
+ heap_attisnull (htup , Anum_pg_index_indpred ) &&
214
+ pg_ind -> indkey .dim1 >= 1 && pg_ind -> indkey .values [0 ] == column )
211
215
{
212
- int i ;
216
+ int64 cur_index_size = get_index_size ( pg_ind -> indexrelid ) ;
213
217
214
- for ( i = 0 ; i < pg_ind -> indkey . dim1 ; i ++ )
218
+ if ( found_index == InvalidOid || cur_index_size < found_index_size )
215
219
{
216
- int64 cur_index_size = 0 ;
220
+ bool is_null ;
221
+ Datum indclass = heap_getattr (htup , Anum_pg_index_indclass ,
222
+ pg_index -> rd_att , & is_null );
223
+ oidvector * indclasses = (oidvector * ) DatumGetPointer (indclass );
217
224
218
- if (pg_ind -> indkey .values [i ] == column )
225
+ /* column must use 'spoint2' opclass */
226
+ if (!is_null && indclasses -> values [0 ] == spoint2_opclass )
219
227
{
220
- cur_index_size = get_index_size (pg_ind -> indexrelid );
221
-
222
- if (found_index == InvalidOid || cur_index_size < found_index_size )
223
- {
224
- bool is_null ;
225
- Datum indclass = heap_getattr (htup , Anum_pg_index_indclass ,
226
- pg_index -> rd_att , & is_null );
227
- oidvector * indclasses = (oidvector * ) DatumGetPointer (indclass );
228
-
229
- /* column must use 'spoint2' opclass */
230
- if (!is_null && indclasses -> values [i ] == spoint2_opclass )
231
- {
232
- found_index = pg_ind -> indexrelid ;
233
- found_index_size = cur_index_size ;
234
- }
235
- }
236
-
237
- break ; /* no need to scan 'indkey' further */
228
+ found_index = pg_ind -> indexrelid ;
229
+ found_index_size = cur_index_size ;
238
230
}
239
231
}
240
232
}
0 commit comments