@@ -480,23 +480,37 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
480
480
static Node *
481
481
get_attr_expr (Relation rel , int attnum )
482
482
{
483
- if ((rel -> rd_rel -> relkind == RELKIND_INDEX
484
- || (rel -> rd_rel -> relkind == RELKIND_PARTITIONED_INDEX ))
485
- && (rel -> rd_indexprs != NIL )
486
- && (rel -> rd_index -> indkey .values [attnum - 1 ] == 0 ))
487
- {
488
- ListCell * indexpr_item = list_head (rel -> rd_indexprs );
483
+ List * index_exprs ;
484
+ ListCell * indexpr_item ;
489
485
490
- for (int i = 0 ; i < attnum - 1 ; i ++ )
491
- if (rel -> rd_index -> indkey .values [i ] == 0 )
492
- indexpr_item = lnext (rel -> rd_indexprs , indexpr_item );
486
+ /* relation is not an index */
487
+ if (rel -> rd_rel -> relkind != RELKIND_INDEX &&
488
+ rel -> rd_rel -> relkind != RELKIND_PARTITIONED_INDEX )
489
+ return NULL ;
493
490
494
- if (indexpr_item == NULL ) /* shouldn't happen */
495
- elog (ERROR , "too few entries in indexprs list" );
491
+ index_exprs = RelationGetIndexExpressions (rel );
496
492
497
- return (Node * ) lfirst (indexpr_item );
498
- }
499
- return NULL ;
493
+ /* index has no expressions to give */
494
+ if (index_exprs == NIL )
495
+ return NULL ;
496
+
497
+ /*
498
+ * The index attnum points directly to a relation attnum, then it's no an
499
+ * expression attribute.
500
+ */
501
+ if (rel -> rd_index -> indkey .values [attnum - 1 ] != 0 )
502
+ return NULL ;
503
+
504
+ indexpr_item = list_head (rel -> rd_indexprs );
505
+
506
+ for (int i = 0 ; i < attnum - 1 ; i ++ )
507
+ if (rel -> rd_index -> indkey .values [i ] == 0 )
508
+ indexpr_item = lnext (rel -> rd_indexprs , indexpr_item );
509
+
510
+ if (indexpr_item == NULL ) /* shouldn't happen */
511
+ elog (ERROR , "too few entries in indexprs list" );
512
+
513
+ return (Node * ) lfirst (indexpr_item );
500
514
}
501
515
502
516
/*
0 commit comments