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 1tEfZZ-008wkC-BN for pgsql-performance@arkaria.postgresql.org; Sat, 23 Nov 2024 02:08:33 +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 1tEfYZ-00EVsG-6v for pgsql-performance@arkaria.postgresql.org; Sat, 23 Nov 2024 02:07:31 +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 1tEfYY-00EVs7-PA for pgsql-performance@lists.postgresql.org; Sat, 23 Nov 2024 02:07:30 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tEfYS-003N5B-Cu for pgsql-performance@lists.postgresql.org; Sat, 23 Nov 2024 02:07:29 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-214962885a1so4194385ad.3 for ; Fri, 22 Nov 2024 18:07:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732327642; x=1732932442; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Y5UXo7PpWC65gdVDalkQwOYofIl3AxU408AzYIdnR+M=; b=NV3xD9HksPlFiOWaJzz5TcFZXMmzuoZe3zWjE4iFWkb0UvBocDMrhsVHITqHzIbtI6 8yyuX5+ryks8pxJZtvKUuNrDHkdWh7XWDLLJtMHA1GwnQig/O91LmAi1y6WlrxhzWxJ2 h33owrlGQpTQnIxlnTls2YrgKPt9H27frWRx/pPNLurksZYqasoI9WYPtPQOYXk9OvP9 Gbr5Oo2xx7zBpWjV/4lNEsUnCK62tmgetxRnueHXxvSzZbBPm0wvo89HL1P+gh9ZEPav i8mr+W0DTPKTqMyd4ynZMgFms8BDnCmg/zCSmkhr5GGfBbI16/isbcxIzQNKQfrqZenb v4LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732327642; x=1732932442; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Y5UXo7PpWC65gdVDalkQwOYofIl3AxU408AzYIdnR+M=; b=TiLocuMZhTBKzbLJl6w8VGuATyJRpopaqwdZwlnVYg27rbSqFweHaJM7OfFXktlnE7 gav9gPB9XtZCKh7YodZ593uOr5lSIbQ6m0t9wuDI7l8eTY23UVSf+aJNlasTHJb1fRmf e5GN4770Dq0fH3+bfuzp+cA617JPw7RBNEda9Fwz1FNp1hqnQCbvFKjJjoQdHWypaSyj Zt6jocLUvHzPlsRIstPegfP7jv6Mtera1LDdhrYxgvTWdKvBRA8EfacmB0aqq6lEf46T 9oBROrFZK+JLzXQvk0Zy7spG1gwn1eawook+Ts94ci/GKM+m9ZY5vejCvFKLrj8stUzm Dclw== X-Forwarded-Encrypted: i=1; AJvYcCU/CXYMj8zK6+46zn3XOxtq/WdCHCZ+vTEePjG40jNzHWQMpPBQAvclJIcKG4YSHnNzPsjYtfHPaknVBbqZP8NAeg==@lists.postgresql.org X-Gm-Message-State: AOJu0Yyn1d1qdkA1LOqA853BNXdkiyCtTzPC1ogLXNYQ5UcIMsbbJ4cq A+hZbV+8ovQytQ+trKk2fEIHaetNS21DBCtJY3YPapwXUl5dmtBs X-Gm-Gg: ASbGncs5aOsl8sAM16nvMvU5VosmPJdpfN4uLlwOcJLH1W46T0Cpaw/umo+T6FbZ+y9 B4PB93uvDirULrSmhcTm5JVXBIeP3YpUMD5cVo20L9wF/UJq9JMOJ/wg8ROUdGGL3ObHoJ0TiPt YnBxzJV5soo7eMsLV9MjpLBfBQ5dIT46yuwRHncBA3jUSqiualJp2uU7oq40KyBS8X5qMApHuII ENBQARIG+DM0WxsZIcQue+quydvuKXORnXMzNcqC8RYYQCZAXwIGO/8nZfedQJU X-Google-Smtp-Source: AGHT+IHskBHvwof/otzFaBmwqGGNdXgykYj2R6mze0VhicE2n+xgOgvqymJWk1ZQVYZGwuvn6QhYJg== X-Received: by 2002:a17:902:f690:b0:212:40e0:9553 with SMTP id d9443c01a7336-2129f248e6dmr73394935ad.32.1732327641801; Fri, 22 Nov 2024 18:07:21 -0800 (PST) Received: from [192.168.1.102] ([103.206.205.199]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dbf9d37sm23031515ad.155.2024.11.22.18.07.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 22 Nov 2024 18:07:21 -0800 (PST) Message-ID: Date: Sat, 23 Nov 2024 09:07:17 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Cardinality estimate of the inner relation To: =?UTF-8?Q?Fr=C3=A9d=C3=A9ric_Yhuel?= , pgsql-performance@lists.postgresql.org Cc: Christophe Courtois References: Content-Language: en-US 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 22/11/2024 22:53, Frédéric Yhuel wrote: > My colleague Christophe Courtois and I have been trying to fix a bad > plan for one of Dalibo's clients. It is a (probably well-known) problem > with skewed data and a parameterized Nested Loop with an underestimation > of the cardinality of the inner relation. > > Here is a test case (the script to create and populate the two tables is > at the end): Thanks for the case provided! I wonder if data science has invented a statistic or selectivity estimation technique that could tackle your case in general. As I see, we should touch the table of products to realise which specific ID has the product named 'Babar'. I can imagine the trick when you build MCV on (id, name) and have a chance to find this popular ID, cache it, and use it during join clause estimation later. But it seems too expensive to do the same for arbitrary incoming queries. If you want a workaround, such cases fit query-driven techniques. Among open-source ones, I can point your attention to the AQO extension (honestly, designed under my command). It can save information about an estimation error and correct the query next time. In an enterprise-grade area, you can pick the sr_plan extension, which is designed to store the plan for a specific query (you can choose parameterisation on your own) and spread it globally across all instances' backends. -- regards, Andrei Lepikhov