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 1tcyOw-003zx8-S9 for pgsql-performance@arkaria.postgresql.org; Wed, 29 Jan 2025 03:06:03 +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 1tcyOv-004Y9B-HK for pgsql-performance@arkaria.postgresql.org; Wed, 29 Jan 2025 03:06:01 +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 1tcyOv-004Y93-5q for pgsql-performance@lists.postgresql.org; Wed, 29 Jan 2025 03:06:01 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tcyOq-0025Ro-0K for pgsql-performance@lists.postgresql.org; Wed, 29 Jan 2025 03:05:58 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-218c8aca5f1so152739345ad.0 for ; Tue, 28 Jan 2025 19:05:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738119955; x=1738724755; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:cc:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=bS6KaYDbjmOMl3yPzqraQ1ycYe9St40hbzQxXYcEF3U=; b=C9o2962b7ihVeU/akdq7tiQnv5shrQxsVqavLuhkEBAbelU2IQEsKh8HiCOxnmLpfP HI6bYeNm/qzP0J2UBgy0mj+ORa+GytUNMXJGtSmbdkUEgJqoHBpNZW4mKw7xvjdpQ1R5 06JkSG43bGA9ANjfn7nWf3Gy+tP9KmSixSlgeEI/z9iV/Sk6MzgWQIqYyzMGr91wBD0v 5/lvs54z9CzQlhJF3+qlRyeaienmEksmPZ2ddYdyT74NOt7n0Cvfe5Ah4eWVf082mkB5 DWh1dCfXYX4sZ7jcktzoez9zSvUCeKoKV+3EfFZc3vF4kE7sRZGJ0WEcFRh+9YafimyY ItaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738119955; x=1738724755; h=content-transfer-encoding:in-reply-to:from:cc:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bS6KaYDbjmOMl3yPzqraQ1ycYe9St40hbzQxXYcEF3U=; b=ddZD571OkEUmxKNswg6HxXm6RRraC/UyDb3Rj9vkECPR9III/T74Fw412W06ItXQpg Dan6XjeP/vfNpgHOOe+kkguyXfEW+eKUW/eevDJ0fjSjrnm59gjaB8B+bqmyZ/JbKSKg uR9q4gRGKCAWSA+Ru0ppT5PfgtiwbomGwB6kVJR/UhRmsHSicrEdBe4edi5zx06UePNi MAG/Su4v3J+/imVckovSziTmOXgZYuEWnSG832iMR1xVJ4JZoDlBZxrxvQ1B6kwZ9q01 sGgS3wiocv8DxVcA1uWUh7EmKHRZVxqq80pq5cSwxoy6B8Sqqq0A+A1AhIImvJvI2LMP DYBg== X-Forwarded-Encrypted: i=1; AJvYcCUiN/bn7xx/LxYj3USc7W13xMOMIqyU8dpqgMu/HgIxhX2cMo9g9v9VVtI0mGfYyTH1Gq2jHzwMfj91VEbH2HzOKA==@lists.postgresql.org X-Gm-Message-State: AOJu0YyVuswxrcPdoRwxzgfp285o0qSkn0kfNJU2GrPA2i/GN6+R9vNx DURPe41S6JzJ66GP1NxcufUJN07ipi1j3LxGoKU1nukqosVbnsUz X-Gm-Gg: ASbGncuOLCZa/vzm6/qH6nhU93AqG6vW8eZkgKLpEl14r+Nvtb8wGng66v0daFVA5EN zGiDpDarvV0wlsjhblSA1jQtTkMqa4DffjQlZ20+PHNFyF65EaFZm/e/cN6uCKbL4YZqLORWW5X MOxUfavEKBwvErZHYKmfeOmHlEUqhVc4+gXEpSifik3LrFEEPmNq13N3ZWXdaChB3H/RjEf7v3V 1UVmu6xuzp/7lmbiZ525ewYPMKnhn+IvZJy1pG4c5PS6ZhP1ts5YmAQvDy/yaLt+rHxrPOd2dIb 8xhyVE7mE0vNB8PlnNMExdKeW3HZAbD+/gYojPHWrQhxeUpqFVyUKdO7boTSteJ1eA== X-Google-Smtp-Source: AGHT+IGeBU8JX3Emuk01JBYgJN5AHAGWMpDl7+fliGbBhBoxMHpJ/08BcfqR5UDzlq7bfuZHZqBGnQ== X-Received: by 2002:a05:6a20:d498:b0:1e1:f281:8cf5 with SMTP id adf61e73a8af0-1ed7a32ceabmr3142600637.0.1738119954831; Tue, 28 Jan 2025 19:05:54 -0800 (PST) Received: from [10.10.20.106] (node-hh3.pool-125-27.dynamic.totinternet.net. [125.27.88.119]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72f8a6a0838sm10321724b3a.15.2025.01.28.19.05.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 28 Jan 2025 19:05:54 -0800 (PST) Message-ID: Date: Wed, 29 Jan 2025 10:05:50 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Simple query with Planner underestimating rows. To: =?UTF-8?Q?Felipe_L=C3=B3pez_Montes?= , pgsql-performance@lists.postgresql.org References: Content-Language: en-AU Cc: htamfids@gmail.com From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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