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.96) (envelope-from ) id 1vmqca-00GxcG-06 for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Feb 2026 09:53:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vmqcX-00DJp9-39 for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Feb 2026 09:53:26 +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.96) (envelope-from ) id 1vmqcX-00DJp0-2D for pgsql-hackers@lists.postgresql.org; Mon, 02 Feb 2026 09:53:26 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vmqcW-00000000ecp-1NG4 for pgsql-hackers@lists.postgresql.org; Mon, 02 Feb 2026 09:53:26 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-482f2599980so13247405e9.0 for ; Mon, 02 Feb 2026 01:53:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770025998; x=1770630798; 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=V++9JG3/wYq4u15wZqJx+b9mjeNYEKm1IDcBHU9+ijU=; b=IJ93Xg++Wp8XDsrWvnOYI9lSpQXkZwNYB9vC/YwHZxIA06/WGjHA1A5rVrsMJc5zlL Gymnh7MbCAAUL96515Y7zH4/BPLD/fnxhZcTvegMh9ZVZW6hkwAJ1XuVlOQQphVu91rW SdsXUPhiZVBOwebh9DegVEmvfoYtGq78+aJHjzhgdUndRMgJP1WScw2hoUCn/ATMfmg6 Q105ITl68A0lAbE66k2nNyheUN/RpO8RMMA0TsFdV7mlOjjP/0U6h6UnCdjK4iIIGNn/ 1ZjC7+7DRW1uGvKeURkB/zA8eatZ5m7ehUUPrcgKbm/EHTcpRrxPE+Hovo0nLua4HrA7 EOtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770025998; x=1770630798; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=V++9JG3/wYq4u15wZqJx+b9mjeNYEKm1IDcBHU9+ijU=; b=t45B5vRU1TwQHnFdb1zrWBbQ0qUVrIF4ORmEE3MedpgeshD7w93YmdWhfApK6T15Ye 1SBclEb0+zOOza3avRGccUiLcEm/qnvBOmK7lBlodDuDRAnzB49gbI5/dXLBovfjkyhz kTB9sYFotY5TXiegISZTk5Axq5qjFebcbaseXNDDzzz0sM16FpKQNGUA8GUs/UK25BQp uAGPLc5qCuYC+KZj1fuhmlNlOZ0mjmMjjqwzrdH/8B6jr5mn+NjDvLFbND0Y+P5+XuS9 8ZEeLJ54/FgJ+I9ASlJI8WFxslp9gpDkkosudw360A5AtbIqyNHTFL4HpiXPOf+mCQf7 sF9w== X-Forwarded-Encrypted: i=1; AJvYcCUxc1rFY92VMpiXxIK0NnggE4KAsMnPRrfwqnyNd9hUdUMf2aAsAGw44w6QHDbH0iycC4WdCibzg5OAAx0A@lists.postgresql.org X-Gm-Message-State: AOJu0YybZF2WmNCiyn8kA6cUeM/cT9R0P5gpo94ZTmL2kI7RTgpDOdC+ yeeSvx4Evo/lNanOmj+Lc22uqq1CrwDhBKiIxrEKUaLSRqKfBkzblZRo X-Gm-Gg: AZuq6aKMBz1rVQ+HcfOHY9czgDkY/WxzzmYcy2Go97NYbh8p6/YgBYfLD+Iiehkblb4 F35O46mkNiWUUC154FLDZdul3asAZMWfAhegGMULXyFTYhUvJQ+fwL8xcLqbNtpb+lfn+f34KNv lp1g/IZWtJlKAO3tQrleRPGpV5OKGpjzZ3G7IjevFJDjiwEFD2LfDpK5MqfEFSlwzSRxit/E3wN 9bNNsUUyuN0kiD+tPjVrJSq4pqmSPt9i90nha+d08+IgLwJggjCekwNTa/VLA+Dn6jckfeNXnGS 14axGz+vt41Q4zbv/CqLQljoA5eWNVoShrfTUq780KBXSE4YmoLavwnle3kRxEpaQ64zPyvVel7 ka/0N7sqQuS7nusHjYToOKAfi1HEAi0N1CTaBu+oGfeXQNJK7i8t69FsWO6PqePIP66fNhiC8cp 86zAGPx5YZbs062xYyvb4tiite8C0BPa8iuuoi X-Received: by 2002:a05:600c:310f:b0:479:1b0f:dfff with SMTP id 5b1f17b1804b1-482db4602f0mr126593995e9.10.1770025998011; Mon, 02 Feb 2026 01:53:18 -0800 (PST) Received: from [192.168.15.159] ([80.251.191.198]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4806ce4c515sm369919745e9.11.2026.02.02.01.53.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Feb 2026 01:53:17 -0800 (PST) Message-ID: <3c477f2f-10e4-4705-bb21-90ccbe67e9d2@gmail.com> Date: Mon, 2 Feb 2026 10:53:16 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Is there value in having optimizer stats for joins/foreignkeys? To: Tomas Vondra , Alexandra Wang , Corey Huinker Cc: Tom Lane , pgsql-hackers@lists.postgresql.org, hs@cybertec.at, Jeff Davis References: <246035.1764627115@sss.pgh.pa.us> <8df3d212-5d60-4e30-9606-d8849f7d37ae@gmail.com> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 1/2/26 17:39, Tomas Vondra wrote: > We can't simply store an opaque VIEW, and build the stats by simply > executing it (and sampling the results). The whole premise of extended > stats is that people define them to fix incorrect estimates. And with > incorrect estimates the plan may be terrible, and the VIEW may not even > complete. Ok, I got the point. I think linking to a join or foreign key seems restrictive. In my mind, extended statistics may go the following way: CREATE STATISTICS abc_stat ON (t1.x,t2.y,t3.z) FROM t1,t2,t3; Suppose t1.x,t2.y, and t3.z have a common equality operator. Here we can build statistics on (t1.x = t2.y), (t1.x = t3.z), (t2.y = t3.z), and potentially (t1.x = t2.y = t3.z). But I don't frequently detect problems with JOIN estimation using a single join clause. Usually, we have problems with (I) join trees (clauses spread across joins) and (II) a single multi-clause join. We can't solve (I) here (kinda statistics on a VIEW might help, I think), but may ease (II) using: CREATE STATISTICS abc_stat ON ((t1.x=t2.x),(t1.y=t2.y)) FROM t1,t2; or even more bravely: CREATE STATISTICS abc_stat ON ((t1.x=t2.x),(t1.y=t2.y)) FROM t1,t2 WHERE (t1.z <> t2.z); -- regards, Andrei Lepikhov, pgEdge