Potential Issue with Redundant Restriction Clauses in get_parameterized_baserel_size for PARTITIONED_REL

From: huyajun <hu_yajun(at)qq(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: feichanghong(at)qq(dot)com
Subject: Potential Issue with Redundant Restriction Clauses in get_parameterized_baserel_size for PARTITIONED_REL
Date: 2024-11-26 06:26:49
Message-ID: tencent_0B4BDFF59D371F2000BAA93B76FB5BED0206@qq.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,
The get_parameterized_baserel_size function does not differentiate for PARTITIONED_REL and always appends the rel's own restriction clauses. However, for PARTITIONED_REL, rel->tuples is computed in set_append_rel_size which comes from the sum of lived childrel->rows. It is important to note that childrel->rows is the estimated number of result tuples, meaning it already includes the effect of the rel's own restriction clauses.
Therefore, get_parameterized_baserel_size should not append the rel's own restriction clauses again for PARTITIONED_REL. It seems there might be a mistake here. Although I have not found any SQL that causes issues, this looks like a potential problem. The correct code should handle this situation appropriately like this.

--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -5365,7 +5365,10 @@ get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel,
* restriction clauses. Note that we force the clauses to be treated as
* non-join clauses during selectivity estimation.
*/
- allclauses = list_concat_copy(param_clauses, rel->baserestrictinfo);
+ if (rel->reloptkind == RELOPT_BASEREL && IS_PARTITIONED_REL(rel))
+ allclauses = list_copy(param_clauses);
+ else
+ allclauses = list_concat_copy(param_clauses, rel->baserestrictinfo);
nrows = rel->tuples *
clauselist_selectivity(root,
allclauses,

Regards,
Yajun Hu

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2024-11-26 06:27:11 Re: Improve the error message for logical replication of regular column to generated column.
Previous Message Marina Polyakova 2024-11-26 06:24:05 Fix meson uuid header check so it works with MSVC in REL_16_STABLE