aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJeff Davis <jdavis@postgresql.org>2025-02-20 22:31:22 -0800
committerJeff Davis <jdavis@postgresql.org>2025-02-20 22:31:22 -0800
commitb50a554cc84066577f0f0a3baafe2f1fac302006 (patch)
treee89ca68df18ae4afe0e421fb3097fd1d30a37fa2 /src
parent41625ab8ea3d0a2656dd0f067f1f0b61df63af97 (diff)
downloadpostgresql-b50a554cc84066577f0f0a3baafe2f1fac302006.tar.gz
postgresql-b50a554cc84066577f0f0a3baafe2f1fac302006.zip
Fix for pg_restore_attribute_stats().
Use RelationGetIndexExpressions() rather than rd_indexprs directly. Author: Corey Huinker <corey.huinker@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/backend/statistics/attribute_stats.c42
1 files changed, 28 insertions, 14 deletions
diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index 94f7dd63a03..c0c398a4bb2 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -480,23 +480,37 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
static Node *
get_attr_expr(Relation rel, int attnum)
{
- if ((rel->rd_rel->relkind == RELKIND_INDEX
- || (rel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX))
- && (rel->rd_indexprs != NIL)
- && (rel->rd_index->indkey.values[attnum - 1] == 0))
- {
- ListCell *indexpr_item = list_head(rel->rd_indexprs);
+ List *index_exprs;
+ ListCell *indexpr_item;
- for (int i = 0; i < attnum - 1; i++)
- if (rel->rd_index->indkey.values[i] == 0)
- indexpr_item = lnext(rel->rd_indexprs, indexpr_item);
+ /* relation is not an index */
+ if (rel->rd_rel->relkind != RELKIND_INDEX &&
+ rel->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
+ return NULL;
- if (indexpr_item == NULL) /* shouldn't happen */
- elog(ERROR, "too few entries in indexprs list");
+ index_exprs = RelationGetIndexExpressions(rel);
- return (Node *) lfirst(indexpr_item);
- }
- return NULL;
+ /* index has no expressions to give */
+ if (index_exprs == NIL)
+ return NULL;
+
+ /*
+ * The index attnum points directly to a relation attnum, then it's no an
+ * expression attribute.
+ */
+ if (rel->rd_index->indkey.values[attnum - 1] != 0)
+ return NULL;
+
+ indexpr_item = list_head(rel->rd_indexprs);
+
+ for (int i = 0; i < attnum - 1; i++)
+ if (rel->rd_index->indkey.values[i] == 0)
+ indexpr_item = lnext(rel->rd_indexprs, indexpr_item);
+
+ if (indexpr_item == NULL) /* shouldn't happen */
+ elog(ERROR, "too few entries in indexprs list");
+
+ return (Node *) lfirst(indexpr_item);
}
/*