Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9ba7bcc

Browse files
committed
Correct relation size estimate with low fillfactor
Since commit 29cf61a, table_block_relation_estimate_size() considers fillfactor when estimating number of rows in a relation before the first ANALYZE. The formula however did not consider tuples may be larger than available space determined by fillfactor, ending with density 0. This ultimately means the relation was estimated to contain a single row. The executor however places at least one tuple per page, even with very low fillfactor values, so the density should be at least 1. Fixed by clamping the density estimate using clamp_row_est(). Reported by Heikki Linnakangas. Fix by me, with regression test inspired by example provided by Heikki. Backpatch to 17, where the issue was introduced. Reported-by: Heikki Linnakangas Backpatch-through: 17 Discussion: https://postgr.es/m/2bf9d973-7789-4937-a7ca-0af9fb49c71e@iki.fi
1 parent e596e07 commit 9ba7bcc

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/backend/access/table/tableam.c

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "access/syncscan.h"
2525
#include "access/tableam.h"
2626
#include "access/xact.h"
27+
#include "optimizer/optimizer.h"
2728
#include "optimizer/plancat.h"
2829
#include "port/pg_bitutils.h"
2930
#include "storage/bufmgr.h"
@@ -740,6 +741,8 @@ table_block_relation_estimate_size(Relation rel, int32 *attr_widths,
740741
tuple_width += overhead_bytes_per_tuple;
741742
/* note: integer division is intentional here */
742743
density = (usable_bytes_per_page * fillfactor / 100) / tuple_width;
744+
/* There's at least one row on the page, even with low fillfactor. */
745+
density = clamp_row_est(density);
743746
}
744747
*tuples = rint(density * (double) curpages);
745748

src/test/regress/expected/stats.out

+17
Original file line numberDiff line numberDiff line change
@@ -1749,4 +1749,21 @@ SELECT COUNT(*) FROM brin_hot_3 WHERE a = 2;
17491749

17501750
DROP TABLE brin_hot_3;
17511751
SET enable_seqscan = on;
1752+
-- Test that estimation of relation size works with tuples wider than the
1753+
-- relation fillfactor. We create a table with wide inline attributes and
1754+
-- low fillfactor, insert rows and then see how many rows EXPLAIN shows
1755+
-- before running analyze. We disable autovacuum so that it does not
1756+
-- interfere with the test.
1757+
CREATE TABLE table_fillfactor (
1758+
n char(1000)
1759+
) with (fillfactor=10, autovacuum_enabled=off);
1760+
INSERT INTO table_fillfactor
1761+
SELECT 'x' FROM generate_series(1,1000);
1762+
SELECT * FROM check_estimated_rows('SELECT * FROM table_fillfactor');
1763+
estimated | actual
1764+
-----------+--------
1765+
1000 | 1000
1766+
(1 row)
1767+
1768+
DROP TABLE table_fillfactor;
17521769
-- End of Stats Test

src/test/regress/sql/stats.sql

+16
Original file line numberDiff line numberDiff line change
@@ -895,4 +895,20 @@ DROP TABLE brin_hot_3;
895895

896896
SET enable_seqscan = on;
897897

898+
-- Test that estimation of relation size works with tuples wider than the
899+
-- relation fillfactor. We create a table with wide inline attributes and
900+
-- low fillfactor, insert rows and then see how many rows EXPLAIN shows
901+
-- before running analyze. We disable autovacuum so that it does not
902+
-- interfere with the test.
903+
CREATE TABLE table_fillfactor (
904+
n char(1000)
905+
) with (fillfactor=10, autovacuum_enabled=off);
906+
907+
INSERT INTO table_fillfactor
908+
SELECT 'x' FROM generate_series(1,1000);
909+
910+
SELECT * FROM check_estimated_rows('SELECT * FROM table_fillfactor');
911+
912+
DROP TABLE table_fillfactor;
913+
898914
-- End of Stats Test

0 commit comments

Comments
 (0)