From 81574ca50b4702b1450f995ecbd4a2ea472a73b0 Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Thu, 4 Aug 2022 18:29:12 +0800 Subject: [PATCH v2] fix bug 17570 --- src/backend/statistics/extended_stats.c | 31 ++++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index d2aa8d0ca3..f200751e3a 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -1347,7 +1347,9 @@ statext_is_compatible_clause_internal(PlannerInfo *root, Node *clause, if (!AttrNumberIsForUserDefinedAttr(var->varattno)) return false; - *attnums = bms_add_member(*attnums, var->varattno); + *attnums = + bms_add_member(*attnums, + var->varattno - FirstLowInvalidHeapAttributeNumber); return true; } @@ -1615,31 +1617,32 @@ statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid, if (pg_class_aclcheck(rte->relid, userid, ACL_SELECT) != ACLCHECK_OK) { Bitmapset *clause_attnums = NULL; + int attnum = -1; /* Don't have table privilege, must check individual columns */ if (*exprs != NIL) { - pull_varattnos((Node *) exprs, relid, &clause_attnums); + pull_varattnos((Node *) *exprs, relid, &clause_attnums); clause_attnums = bms_add_members(clause_attnums, *attnums); } else clause_attnums = *attnums; - if (bms_is_member(InvalidAttrNumber, clause_attnums)) + while ((attnum = bms_next_member(clause_attnums, attnum)) >= 0) { - /* Have a whole-row reference, must have access to all columns */ - if (pg_attribute_aclcheck_all(rte->relid, userid, ACL_SELECT, - ACLMASK_ALL) != ACLCHECK_OK) - return false; - } - else - { - /* Check the columns referenced by the clause */ - int attnum = -1; + /* attnum is offset by FirstLowInvalidHeapAttributeNumber */ + AttrNumber attno = attnum + FirstLowInvalidHeapAttributeNumber; - while ((attnum = bms_next_member(clause_attnums, attnum)) >= 0) + if (attno == InvalidAttrNumber) + { + /* Have a whole-row reference, must have access to all columns */ + if (pg_attribute_aclcheck_all(rte->relid, userid, ACL_SELECT, + ACLMASK_ALL) != ACLCHECK_OK) + return false; + } + else { - if (pg_attribute_aclcheck(rte->relid, attnum, userid, + if (pg_attribute_aclcheck(rte->relid, attno, userid, ACL_SELECT) != ACLCHECK_OK) return false; } -- 2.31.0