]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Correct relation size estimate with low fillfactor
authorTomas Vondra <tomas.vondra@postgresql.org>
Wed, 19 Feb 2025 22:51:48 +0000 (23:51 +0100)
committerTomas Vondra <tomas.vondra@postgresql.org>
Wed, 19 Feb 2025 22:54:18 +0000 (23:54 +0100)
Since commit 29cf61ade3, 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

src/backend/access/table/tableam.c
src/test/regress/expected/stats.out
src/test/regress/sql/stats.sql

index e57a0b7ea310bce2c8459090e58591f0c522b4e0..3ec11fb52c075a10cb96cd463f09f02ec4600ab4 100644 (file)
@@ -24,6 +24,7 @@
 #include "access/syncscan.h"
 #include "access/tableam.h"
 #include "access/xact.h"
+#include "optimizer/optimizer.h"
 #include "optimizer/plancat.h"
 #include "port/pg_bitutils.h"
 #include "storage/bufmgr.h"
@@ -740,6 +741,8 @@ table_block_relation_estimate_size(Relation rel, int32 *attr_widths,
                tuple_width += overhead_bytes_per_tuple;
                /* note: integer division is intentional here */
                density = (usable_bytes_per_page * fillfactor / 100) / tuple_width;
+               /* There's at least one row on the page, even with low fillfactor. */
+               density = clamp_row_est(density);
        }
        *tuples = rint(density * (double) curpages);
 
index 6e08898b183063e026ebb690e224874f5e108020..43b7873c62b183ea3b17ac3d5e6c47265bdbcd03 100644 (file)
@@ -1646,4 +1646,21 @@ SELECT COUNT(*) FROM brin_hot_3 WHERE a = 2;
 
 DROP TABLE brin_hot_3;
 SET enable_seqscan = on;
+-- Test that estimation of relation size works with tuples wider than the
+-- relation fillfactor. We create a table with wide inline attributes and
+-- low fillfactor, insert rows and then see how many rows EXPLAIN shows
+-- before running analyze. We disable autovacuum so that it does not
+-- interfere with the test.
+CREATE TABLE table_fillfactor (
+  n char(1000)
+) with (fillfactor=10, autovacuum_enabled=off);
+INSERT INTO table_fillfactor
+SELECT 'x' FROM generate_series(1,1000);
+SELECT * FROM check_estimated_rows('SELECT * FROM table_fillfactor');
+ estimated | actual 
+-----------+--------
+      1000 |   1000
+(1 row)
+
+DROP TABLE table_fillfactor;
 -- End of Stats Test
index d8ac0d06f484d6023e3eb22c499a21a3aa1ba7a5..ed0bc1ef0745b9fc2d4b659a0718aefde8455af6 100644 (file)
@@ -849,4 +849,20 @@ DROP TABLE brin_hot_3;
 
 SET enable_seqscan = on;
 
+-- Test that estimation of relation size works with tuples wider than the
+-- relation fillfactor. We create a table with wide inline attributes and
+-- low fillfactor, insert rows and then see how many rows EXPLAIN shows
+-- before running analyze. We disable autovacuum so that it does not
+-- interfere with the test.
+CREATE TABLE table_fillfactor (
+  n char(1000)
+) with (fillfactor=10, autovacuum_enabled=off);
+
+INSERT INTO table_fillfactor
+SELECT 'x' FROM generate_series(1,1000);
+
+SELECT * FROM check_estimated_rows('SELECT * FROM table_fillfactor');
+
+DROP TABLE table_fillfactor;
+
 -- End of Stats Test