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 1rhBjM-00A67w-FD for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Mar 2024 17:04:01 +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 1rhBjK-009XWw-R8 for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Mar 2024 17:03:59 +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 1rhBjK-009XWn-HI for pgsql-hackers@lists.postgresql.org; Mon, 04 Mar 2024 17:03:59 +0000 Received: from mail-lf1-x136.google.com ([2a00:1450:4864:20::136]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rhBjH-002khZ-AE for pgsql-hackers@postgresql.org; Mon, 04 Mar 2024 17:03:57 +0000 Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-513181719easo2996486e87.3 for ; Mon, 04 Mar 2024 09:03:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1709571834; x=1710176634; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Jd7ToN1qJtm+RO4Oru5wF2fSbR3qv1kY8voe5Cs7tzA=; b=FSj90eV66Z50vmLQPlWnUDVUAZGvtXisNLIIq/57JVv3yk07BEuY4Gmt+rsVL0E5z4 iUIpJXcGGMuam4QfUViLE7Slc+0AR8yJqqIgRHCQdCzzV7KH/iB4dce3PLpIjk+SFICB ZFLKV3NKL+MfoLKGMXH6TAJ/SDYeOlpxGedKNrAZTBj5C8o4PTSkZKop9vSRnPcPwE5T E5IjlH6H2SU/1vVg+eTUsilJM57NC44juWzzjXL0Q0yanaRvPq78nCUmUX3tOcQqkvKz prhoY4asSfE5/cj+gdYKefqkCcfZPGSvb2GWyBBIjlyx+3ljH2GDsDTiWk7HUj47v+BZ 8x8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709571834; x=1710176634; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Jd7ToN1qJtm+RO4Oru5wF2fSbR3qv1kY8voe5Cs7tzA=; b=meh2Mpo/SsVANEsvBeMUcCsDsMbtDL9PYGuvdzn20HW2ulb5dI23wLtufJZFHcVZl5 l4QZbsTGGAd7D+qgNmWQ+juCDF5mmlNVKK8DRph2qkDfBMKLr2h4qCZfnUeF6XvlBIO1 uQza8dxRpDUVvkwzX0rVtPjft/RBdNNHLIDCCiGLJJiWqi+wjJKtGVJGGoyPUbSESQa3 1H/FzAC9wSowtzfhFAwiLg9wLIbF/AQywaAtTQSbU01hw5Y2P0667jEe+0ULmUeGk2tO fGqUyJVwZVzKPQyM/q8h9k8uTGeODPux5NBW597QltdoknDjSwZQT7CRhjAQG5CYaliE JO1A== X-Gm-Message-State: AOJu0YwgT2Vj1JNkETfYBbInFrOYtigvPaHTzVbTHX7e1nAuEJI2TmOe 1qRNybNh7cUgXIyHeN1TPOwiUOX1L51fIBr+yP80FNJfOA/798Xh2dBKHLkLDDtTv6fO9yl94bI = X-Google-Smtp-Source: AGHT+IH7EabvS3Go77vj+kMfvIwQI5ZySx5CWWiG/1RANBNEUTjwgwovBIGNP5I4R8n7LNNu/TxpKQ== X-Received: by 2002:a05:6512:3ba8:b0:513:1e8a:f1a5 with SMTP id g40-20020a0565123ba800b005131e8af1a5mr8396778lfv.5.1709571833839; Mon, 04 Mar 2024 09:03:53 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-229-30.bb.vodafone.cz. [86.49.229.30]) by smtp.gmail.com with ESMTPSA id x4-20020a056402414400b005666465520dsm4860529eda.26.2024.03.04.09.03.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Mar 2024 09:03:53 -0800 (PST) Message-ID: <5284e0de-7457-490c-9413-14ba90116c92@enterprisedb.com> Date: Mon, 4 Mar 2024 18:03:52 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: a wrong index choose when statistics is out of date Content-Language: en-US To: David Rowley , Andy Fan Cc: PostgreSQL Hackers References: <878r2zeqsp.fsf@163.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/4/24 06:33, David Rowley wrote: > On Sun, 3 Mar 2024 at 20:08, Andy Fan wrote: >> The issue can be reproduced with the following steps: >> >> create table x_events (.., created_at timestamp, a int, b int); >> >> create index idx_1 on t(created_at, a); >> create index idx_2 on t(created_at, b); >> >> query: >> select * from t where create_at = current_timestamp and b = 1; >> >> index (created_at, a) rather than (created_at, b) may be chosen for the >> above query if the statistics think "create_at = current_timestamp" has >> no rows, then both index are OK, actually it is true just because >> statistics is out of date. > > I don't think there's really anything too special about the fact that > the created_at column is always increasing. We commonly get 1-row > estimates after multiplying the selectivities from individual stats. > Your example just seems like yet another reason that this could > happen. > > I've been periodically talking about introducing "risk" as a factor > that the planner should consider. I did provide some detail in [1] > about the design that was in my head at that time. I'd not previously > thought that it could also solve this problem, but after reading your > email, I think it can. > > I don't think it would be right to fudge the costs in any way, but I > think the risk factor for IndexPaths could take into account the > number of unmatched index clauses and increment the risk factor, or > "certainty_factor" as it is currently in my brain-based design. That > way add_path() would be more likely to prefer the index that matches > the most conditions. > > The exact maths to calculate the certainty_factor for this case I > don't quite have worked out yet. I plan to work on documenting the > design of this and try and get a prototype patch out sometime during > this coming southern hemisphere winter so that there's at least a full > cycle of feedback opportunity before the PG18 freeze. > I've been thinking about this stuff too, so I'm curious to hear what kind of plan you come up with. Every time I tried to formalize a more concrete plan, I ended up with a very complex (and possible yet more fragile) approach. I think we'd need to consider a couple things: 1) reliability of cardinality estimates I think this is pretty much the same concept as confidence intervals, i.e. knowing not just the regular estimate, but also a range where the actual value lies with high confidence (e.g. 90%). For a single clauses this might not be terribly difficult, but for more complex cases (multiple conditions, ...) it seems far more complex :-( For example, let's say we know confidence intervals for two conditions. What's the confidence interval when combined using AND or OR? 2) robustness of the paths Knowing just the confidence intervals does not seem sufficient, though. The other thing that matters is how this affects the paths, how robust the paths are. I mean, if we have alternative paths with costs that flip somewhere in the confidence interval - which one to pick? Surely one thing to consider is how fast the costs change for each path. 3) complexity of the model I suppose we'd prefer a systematic approach (and not some ad hoc solution for one particular path/plan type). So this would be somehow integrated into the cost model, making it yet more complex. I'm quite worried about this (not necessarily because of performance reasons). I wonder if trying to improve the robustness solely by changes in the planning phase is a very promising approach. I mean - sure, we should improve that, but by definition it relies on a priori information. And not only the stats may be stale - it's a very lossy approximation of the actual data. Even if the stats are perfectly up to date / very detailed, there's still going to be a lot of uncertainty. I wonder if we'd be better off if we experimented with more robust plans, like SmoothScan [1] or g-join [2]. regards [1] https://stratos.seas.harvard.edu/sites/scholar.harvard.edu/files/stratos/files/smooth_vldbj.pdf [2] http://wwwlgis.informatik.uni-kl.de/cms/fileadmin/users/haerder/2011/JoinAndGrouping.pdf -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company