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 1vuoRv-007lvU-1P for pgsql-performance@arkaria.postgresql.org; Tue, 24 Feb 2026 09:11:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vuoRu-000HAs-1M for pgsql-performance@arkaria.postgresql.org; Tue, 24 Feb 2026 09:11:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vuoRu-000HAj-0K for pgsql-performance@lists.postgresql.org; Tue, 24 Feb 2026 09:11:22 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vuoRr-00000000w1H-17S2 for pgsql-performance@postgresql.org; Tue, 24 Feb 2026 09:11:21 +0000 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-436309f1ad7so3989818f8f.3 for ; Tue, 24 Feb 2026 01:11:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771924277; x=1772529077; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:cc:content-language :references:to:from:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=RZPDbrdpJ/qYlmG9CUbl5QtYdILczUXUFgz05c8+Gts=; b=LTWdB7w96RzrzX4o0vl3yYSGtouBK587GIpTInAMJA3zlORxaqJ9IGYHLCIGOop161 P2WB9nUW1aTEs4nGXtwo5LqQg984VE6oZTQJZ8xVdPzofynU9zEiHDwtpUguf1tdnlWk 8Vao71j50L+F4Siv23WLm8dr35bEvpmPdEPJ/yJriaceJOWjfiujRd6sZ3XL/GBcCDhy yJBiPuuG0SepkpetKVRS3SoHAsWY498EStXlMo992zcEG54kcKbiDcld7ZsopjyiORmp +SiTGDNMq+apP87jkEfyn7zrTvJmgF7fpU1nhFLUSmkyLm7kwOJBsMvXYoQqvfeQDI7p lJBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771924277; x=1772529077; h=content-transfer-encoding:in-reply-to:cc:content-language :references:to:from: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=RZPDbrdpJ/qYlmG9CUbl5QtYdILczUXUFgz05c8+Gts=; b=GmBe09zRgvw8iGKk41QFghcdXzsdnRXNj6IR4PHEhzVZYdP0ndHWFrQV4DHqfnmlbN MJq3D7F8h/I2M77/pTSKu8MHr6ggZOt60lmR8gFskCLCqQ2N/uGrAW2FccYraN+YBCyU SA+OIhkghzxYPpIKVt8PnCd8N3x5tHCjMDAl34hYrmdyo8xMefo87oqp6W73ruX+ftxj imOhOZ1bTN69eXMgAQnFhxdVEiF00AehpQ/gRuC4PkNn/CbXf4uB2q6YofDaL9kTwW/w ToEQsyFJKn+AuLy9uikJq7lBQ/PWuNeZ1Q3X192UQUmcuVBJd5MYCv+oV/9aS3334+QW HWhg== X-Forwarded-Encrypted: i=1; AJvYcCXAmjEiZZMU5vTd7VHovFFnLx37IJ2VJej7lnF3qqAkGiJImPI8X6xk9EbuzMswdMdaVK0fR5fxGxm0XVumD8tsjw==@postgresql.org X-Gm-Message-State: AOJu0YyA5vJKuPCOPJg7bjJWgaqrqUkWuTil5fu37VIbCGN3uCxGLo0V KVQBg/wgmnXv/apQ1w3iG0KZyxLvoU/AWcKKlgM6cDXrC/qBDKkNYsW2 X-Gm-Gg: ATEYQzyq7yO6rjap8XVcSqQGzs9mH4EI7klwI20iqSoKZEeFGJy0Drs0Xp8a2ATTfQo IPX500GwMbvT6lsRxn7v6KAnKtCdM6KXnPiSfJoyDSv4fXbFHHLSfKMMk+iszcXzQxPwEh94lH8 cblKxdHBD6ZlsF/pLKbnFNlsbi42c2NwA+pIgw0RcpQYBbi+TKf3Kf2JFyYmBc/S/FYZtjRfNoK IifbtjkFu/n5aOI+9WDOkF6b70bb2jq2MH3Yxayf8IkdNT+z440a6K3D7OpnbcBQd54qI9Pnza+ P/r8aDpVxZTHhrI5Nmwn7OjufN3wHf8U33NY9xvtcPBak++k+FCY1fXNtuPADx/086PJlxLT/bB v256y9M3a7qyDGf4j4sEcsm9E8+X7pNllwoVhRl7G2n5NaVBO1/rVl2jWVCjcSfbM8hfOWOPCnd 9yCjowdPxT/6lJGpOZJZFPhscjOGBqCh+3hjFf9Q== X-Received: by 2002:a05:6000:228a:b0:437:69ee:71f4 with SMTP id ffacd0b85a97d-4396f17e2f3mr20435367f8f.28.1771924277225; Tue, 24 Feb 2026 01:11:17 -0800 (PST) Received: from [192.168.15.167] ([80.251.191.198]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43970bfa015sm25233234f8f.8.2026.02.24.01.11.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 24 Feb 2026 01:11:16 -0800 (PST) Message-ID: <74632f86-2b03-4259-ac23-b837958acbb6@gmail.com> Date: Tue, 24 Feb 2026 10:11:15 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: unstable query plan on pg 16,17,18 From: Andrei Lepikhov To: Attila Soki , pgsql-performance@postgresql.org References: <64ddfe83-317c-4cf1-b04f-cb74d9f40629@gmail.com> <529E2365-6C3F-4BDA-9625-312F3A023C5B@gmx.net> <5bace3e5-3d6b-4f81-92a9-9ff4e9e5de53@gmail.com> Content-Language: en-US Cc: Laurenz Albe , Tom Lane In-Reply-To: <5bace3e5-3d6b-4f81-92a9-9ff4e9e5de53@gmail.com> 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 23/2/26 21:25, Andrei Lepikhov wrote: > On 23/2/26 18:03, Attila Soki wrote: > So, let me discover a little more, but your PG14 explain could add more > details here. It seems much more interesting than just a trivial accumulation of cost estimation errors. Look: ... -> Hash (cost=86.59..86.59 rows=8 width=67) (actual time=0.136..0.136 rows=44.56 loops=21798) Buckets: 2048 (originally 1024) Batches: 1 (originally 1) ... -> Nested Loop (cost=1.12..86.59 rows=8 width=67) (actual time=0.017..0.126 rows=44.56 loops=21798) ... This hash table has been rescanned multiple times. And on each rescan, it was rebuilt as well (the number of loops in the underlying Join was also 21798). It is the first time I have seen such a query plan. And discovering how rescan reckons in the cost model, this Hash table rebuilding == subtree rescanning, you may find the following: cost_rescan(): case T_HashJoin: /* * If it's a single-batch join, we don't need to rebuild the hash * table during a rescan. */ if (((HashPath *) path)->num_batches == 1) { /* Startup cost is exactly the cost of hash table building */ *rescan_startup_cost = 0; *rescan_total_cost = path->total_cost - path->startup_cost; } ... That means (if I read the code correctly) we don't take into account the cost=86.59 of subtree rescanning and htab rebuilding at all! So, it looks like a rare cost model bug. To learn more, I still need your PG14 EXPLAIN. Can you also share your SQL so we can understand which combination of SQL structures led to this unusual query plan? -- regards, Andrei Lepikhov, pgEdge