Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b50a554

Browse files
committed
Fix for pg_restore_attribute_stats().
Use RelationGetIndexExpressions() rather than rd_indexprs directly. Author: Corey Huinker <corey.huinker@gmail.com>
1 parent 41625ab commit b50a554

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/backend/statistics/attribute_stats.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -480,23 +480,37 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
480480
static Node *
481481
get_attr_expr(Relation rel, int attnum)
482482
{
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;
489485

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;
493490

494-
if (indexpr_item == NULL) /* shouldn't happen */
495-
elog(ERROR, "too few entries in indexprs list");
491+
index_exprs = RelationGetIndexExpressions(rel);
496492

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);
500514
}
501515

502516
/*

0 commit comments

Comments
 (0)