Re: Simple query with Planner underestimating rows.

From: Andrei Lepikhov <lepihov(at)gmail(dot)com>
To: Felipe López Montes <xocas89(at)gmail(dot)com>, pgsql-performance(at)lists(dot)postgresql(dot)org
Cc: htamfids(at)gmail(dot)com
Subject: Re: Simple query with Planner underestimating rows.
Date: 2025-01-29 03:05:50
Message-ID: a383df07-989d-4903-8eb7-c93d4a16b628@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On 1/29/25 02:29, Felipe López Montes wrote:
> Hi all,
>
> I am using PostgreSQL 17.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC)
> 12.4.0, 64-bit.
>
> I have been facing this issue past weeks and I don't know what else to
> try, I have a very simple query where the planner is underestimating the
> number of rows. So instead of estimating the ~ actual number of rows
> (5120), it estimates only 1.
Your query represents a typical PostgreSQL issue: Here, I see two
'almost' unique columns: participant_identifier and
programme_identifier. The result is that the join cardinality on a
unique column is predicted to be close to the size of the smaller relation.
But in the second join, you have a join by two columns with low selectivity:

ON t3.participant_identifier = t1.participant_identifier
AND t3.programme_identifier = t2.programme_identifier;

Postgres doesn't gather dependency statistics on two or more columns and
just multiplies the low selectivities of these clauses, reducing the
number of rows to a possible minimum - 1.

What you can do? Right now, maybe only pg_hint_plan may help in such a
situation. However, if you provide some test cases, we may check the
forgotten feature [1], which enables extended statistics in join clause
estimations and may push development efforts in that direction.

[1] using extended statistics to improve join estimates
https://www.postgresql.org/message-id/flat/c8c0ff31-3a8a-7562-bbd3-78b2ec65f16c%40enterprisedb.com

--
regards, Andrei Lepikhov

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Andrei Lepikhov 2025-01-29 08:57:15 Re: Simple query with Planner underestimating rows.
Previous Message Greg Sabino Mullane 2025-01-29 02:12:03 Re: Simple query with Planner underestimating rows.