fix NaN cost estimate on empty columnar tables (#4593)

Fixing a division by zero in the cost calculations for scanning a columnar table.

Due to how the columns in a columnar table are counted an empty table would result in a division by zero. Instead this patch keeps the column selection ratio on zero when this happens, resulting in an accurate cost of zero pages to scan a columnar table.

fixes #4589
pull/4585/head
Nils Dijk 2021-01-27 17:32:17 +01:00 committed by GitHub
parent 07cf037b13
commit 07d3b4fd04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View File

@ -242,7 +242,18 @@ ColumnarScanCost(RangeTblEntry *rte)
{ {
Bitmapset *attr_needed = rte->selectedCols; Bitmapset *attr_needed = rte->selectedCols;
double numberOfColumnsRead = bms_num_members(attr_needed); double numberOfColumnsRead = bms_num_members(attr_needed);
double selectionRatio = numberOfColumnsRead / (double) maxColumnCount; double selectionRatio = 0;
/*
* When no stripes are in the table we don't have a count in maxColumnCount. To
* prevent a division by zero turning into a NaN we keep the ratio on zero.
* This will result in a cost of 0 for scanning the table which is a reasonable
* cost on an empty table.
*/
if (maxColumnCount != 0)
{
selectionRatio = numberOfColumnsRead / (double) maxColumnCount;
}
Cost scanCost = (double) totalStripeSize / BLCKSZ * selectionRatio; Cost scanCost = (double) totalStripeSize / BLCKSZ * selectionRatio;
return scanCost; return scanCost;
} }

View File

@ -94,6 +94,19 @@ truncate t_compressed;
-- alter type -- alter type
alter table t_uncompressed alter column a type text; alter table t_uncompressed alter column a type text;
alter table t_compressed alter column a type text; alter table t_compressed alter column a type text;
-- verify cost of scanning an empty table is zero, not NaN
explain table t_uncompressed;
QUERY PLAN
---------------------------------------------------------------------
Custom Scan (ColumnarScan) on t_uncompressed (cost=0.00..0.00 rows=1 width=32)
(1 row)
explain table t_compressed;
QUERY PLAN
---------------------------------------------------------------------
Custom Scan (ColumnarScan) on t_compressed (cost=0.00..0.00 rows=1 width=32)
(1 row)
-- drop -- drop
drop table t_compressed; drop table t_compressed;
drop table t_uncompressed; drop table t_uncompressed;

View File

@ -43,6 +43,10 @@ truncate t_compressed;
alter table t_uncompressed alter column a type text; alter table t_uncompressed alter column a type text;
alter table t_compressed alter column a type text; alter table t_compressed alter column a type text;
-- verify cost of scanning an empty table is zero, not NaN
explain table t_uncompressed;
explain table t_compressed;
-- drop -- drop
drop table t_compressed; drop table t_compressed;
drop table t_uncompressed; drop table t_uncompressed;