From 2681bb230a68400ea34c68f9df76e24f51b4bb25 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Tue, 20 Oct 2020 00:04:52 +1300 Subject: [PATCH] Relax some asserts in merge join costing code In the planner, it was possible, given an extreme enough case containing a large number of joins for the number of estimated rows to become infinite. This could cause problems in initial_cost_mergejoin() where we perform some calculations based on those row estimates. A problem case, presented by Onder Kalaci showed an Assert failure from an Assert checking outerstartsel <= outerendsel. In his test case this was effectively NaN <= Inf, which is false. The NaN outerstartsel came from multiplying the infinite outer_path_rows by 0.0. In master, this problem was fixed by a90c950fc, however, that fix was too invasive for the backbranches. Here we just relax the Asserts to allow them to pass. The worst that appears to happen from this is that we show NaN cost values and infinite row estimates in EXPLAIN. add_path() would have had a hard time doing anything useful with such costs, but that does not really matter as if the row estimates were even close to accurate, such plan would not complete this side of the heat death of the universe. Reported-by: Onder Kalaci Backpatch: 9.5 to 13 Discussion: https://postgr.es/m/DM6PR21MB1211FF360183BCA901B27F04D80B0@DM6PR21MB1211.namprd21.prod.outlook.com --- src/backend/optimizer/path/costsize.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index c7400941ee..ca3ad7bc43 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -2707,8 +2707,9 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace, outer_rows = clamp_row_est(outer_path_rows * outerendsel); inner_rows = clamp_row_est(inner_path_rows * innerendsel); - Assert(outer_skip_rows <= outer_rows); - Assert(inner_skip_rows <= inner_rows); + /* skip rows can become NaN when path rows has become infinite */ + Assert(outer_skip_rows <= outer_rows || isnan(outer_skip_rows)); + Assert(inner_skip_rows <= inner_rows || isnan(inner_skip_rows)); /* * Readjust scan selectivities to account for above rounding. This is @@ -2720,8 +2721,9 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace, outerendsel = outer_rows / outer_path_rows; innerendsel = inner_rows / inner_path_rows; - Assert(outerstartsel <= outerendsel); - Assert(innerstartsel <= innerendsel); + /* start sel can become NaN when path rows has become infinite */ + Assert(outerstartsel <= outerendsel || isnan(outerstartsel)); + Assert(innerstartsel <= innerendsel || isnan(innerstartsel)); /* cost of source data */