Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1smEyz-004Gvv-U9 for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Sep 2024 16:05:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1smEyy-00D7de-Ci for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Sep 2024 16:05:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1smEyx-00D7cJ-WD for pgsql-hackers@lists.postgresql.org; Thu, 05 Sep 2024 16:05:16 +0000 Received: from forward102a.mail.yandex.net ([2a02:6b8:c0e:500:1:45:d181:d102]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1smEyv-000JGv-Dp for pgsql-hackers@postgresql.org; Thu, 05 Sep 2024 16:05:15 +0000 Received: from mail-nwsmtp-smtp-production-main-84.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-84.vla.yp-c.yandex.net [IPv6:2a02:6b8:c2a:1c1:0:640:adc:0]) by forward102a.mail.yandex.net (Yandex) with ESMTPS id 1A78160AC8 for ; Thu, 5 Sep 2024 19:05:11 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-84.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id 75gl1V98Z8c0-JLBQnYdc; Thu, 05 Sep 2024 19:05:10 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tantorlabs.com; s=mail; t=1725552310; bh=u444rHMC1fs03KpcHIWrK7sngQ9G0L/1ySbOP539/HY=; h=Subject:To:Message-ID:Date:From; b=otDdneo3SARkxxWxRVDjOS0VuD8SLp0Tqfo88NcijclpbBYpfICjHmLLaAVp56KZC rECOgCTnJW7fPLb59C8N2JKuWFaAMqPmwSIFkBbWdYeb4tW8pA+Me87XQHH6ncqi6q vWPg6lCSGbAp0OPCZVlMtz4ySBVWJO1h6NWNfzQ8= Authentication-Results: mail-nwsmtp-smtp-production-main-84.vla.yp-c.yandex.net; dkim=pass header.i=@tantorlabs.com Content-Type: multipart/mixed; boundary="------------vrX5039H7WQXZPq90ejTHa40" Message-ID: Date: Thu, 5 Sep 2024 19:05:06 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: pgsql-hackers From: Ilia Evdokimov Subject: Adding NOTICE for differences between estimated and actual rows List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------vrX5039H7WQXZPq90ejTHa40 Content-Type: multipart/alternative; boundary="------------rCVLS5z60Rj4jM53QkzU0hxM" --------------rCVLS5z60Rj4jM53QkzU0hxM Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi hackers, It's quite common that poor query performance can be attributed to inaccurate row estimations by the planner. To make it easier to detect these discrepancies, rather than scrutinizing the estimates manually, it would be helpful to output a dedicated |NOTICE| message. In the current patch, I've introduced a new GUC parameter called 'estimated_rows_scale_factor'. If the ratio of the estimated rows to the actual rows is less than this factor, or if the estimated rows significantly exceed the actual rows (when the ratio is greater than this factor), a NOTICE message will be printed. The message reads: "Estimated rows (%.0f) less(greater) than actual rows (%.0f)." Here is an example: CREATE TABLE t(a int, b int); INSERT INTO t SELECT x/10, x FROM generate_series(1,10000000) g(x); ANALYZE; SET estimated_rows_scale_factor = 0.9; EXPLAIN ANALYZE SELECT * FROM t WHERE a > 10 AND b <= 200; NOTICE:  Estimated rows (1000) greater than actual rows (91).                                                      QUERY PLAN ---------------------------------------------------------------------------------------------------------------------  Gather  (cost=1000.00..107848.00 rows=1000 width=8) (actual time=0.446..122.476 rows=91 loops=1)    Workers Planned: 2    Workers Launched: 2    ->  Parallel Seq Scan on t  (cost=0.00..106748.00 rows=417 width=8) (actual time=77.657..118.000 rows=30 loops=3)          Filter: ((a > 10) AND (b <= 200))          Rows Removed by Filter: 3333303  Planning Time: 0.097 ms  Execution Time: 122.502 ms (8 rows) EXPLAIN ANALYZE SELECT * FROM t WHERE a = 10 AND b <= 200; NOTICE:  Estimated rows (1) less than actual rows (10).                                                     QUERY PLAN ------------------------------------------------------------------------------------------------------------------  Gather  (cost=1000.00..107748.10 rows=1 width=8) (actual time=0.280..104.752 rows=10 loops=1)    Workers Planned: 2    Workers Launched: 2    ->  Parallel Seq Scan on t  (cost=0.00..106748.00 rows=1 width=8) (actual time=66.493..101.102 rows=3 loops=3)          Filter: ((b <= 200) AND (a = 10))          Rows Removed by Filter: 3333330  Planning Time: 0.129 ms  Execution Time: 104.768 ms (8 rows) If you have any suggestions regarding the wording of the message, its placement, or if you'd like to see a different criterion used, I would greatly appreciate your feedback. Looking forward to your thoughts and suggestions. -- Regards, Ilia Evdokimov, Tantor Labs LCC. --------------rCVLS5z60Rj4jM53QkzU0hxM Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi hackers,

It's quite common that poor query performance can be attributed to inaccurate row estimations by the planner. To make it easier to detect these discrepancies, rather than scrutinizing the estimates manually, it would be helpful to output a dedicated NOTICE message.

In the current patch, I've introduced a new GUC parameter called 'estimated_rows_scale_factor'. If the ratio of the estimated rows to the actual rows is less than this factor, or if the estimated rows significantly exceed the actual rows (when the ratio is greater than this factor), a NOTICE message will be printed. The message reads: "Estimated rows (%.0f) less(greater) than actual rows (%.0f)."


Here is an example:

CREATE TABLE t(a int, b int);
INSERT INTO t SELECT x/10, x FROM generate_series(1,10000000) g(x);
ANALYZE;

SET estimated_rows_scale_factor = 0.9;

EXPLAIN ANALYZE SELECT * FROM t WHERE a > 10 AND b <= 200;
NOTICE:  Estimated rows (1000) greater than actual rows (91).
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Gather  (cost=1000.00..107848.00 rows=1000 width=8) (actual time=0.446..122.476 rows=91 loops=1)
   Workers Planned: 2
   Workers Launched: 2
   ->  Parallel Seq Scan on t  (cost=0.00..106748.00 rows=417 width=8) (actual time=77.657..118.000 rows=30 loops=3)
         Filter: ((a > 10) AND (b <= 200))
         Rows Removed by Filter: 3333303
 Planning Time: 0.097 ms
 Execution Time: 122.502 ms
(8 rows)

EXPLAIN ANALYZE SELECT * FROM t WHERE a = 10 AND b <= 200;
NOTICE:  Estimated rows (1) less than actual rows (10).
                                                    QUERY PLAN                                                    
------------------------------------------------------------------------------------------------------------------
 Gather  (cost=1000.00..107748.10 rows=1 width=8) (actual time=0.280..104.752 rows=10 loops=1)
   Workers Planned: 2
   Workers Launched: 2
   ->  Parallel Seq Scan on t  (cost=0.00..106748.00 rows=1 width=8) (actual time=66.493..101.102 rows=3 loops=3)
         Filter: ((b <= 200) AND (a = 10))
         Rows Removed by Filter: 3333330
 Planning Time: 0.129 ms
 Execution Time: 104.768 ms
(8 rows)


If you have any suggestions regarding the wording of the message, its placement, or if you'd like to see a different criterion used, I would greatly appreciate your feedback.

Looking forward to your thoughts and suggestions.

-- 
Regards,
Ilia Evdokimov,
Tantor Labs LCC.
--------------rCVLS5z60Rj4jM53QkzU0hxM-- --------------vrX5039H7WQXZPq90ejTHa40 Content-Type: text/x-patch; charset=UTF-8; name="v1-0001-Add-message-when-estimated-rows-differ-from-actual.patch" Content-Disposition: attachment; filename*0="v1-0001-Add-message-when-estimated-rows-differ-from-actual.p"; filename*1="atch" Content-Transfer-Encoding: base64 RnJvbSA4ZTAzOGYxNmQ0NWQ1ZjhjMzljNzIwNGZjNjYxZWQ0YjdmNTA0YTYzIE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBJbGlhIEV2ZG9raW1vdiA8aWx5YS5ldmRva2ltb3Z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