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 1tPv7A-00GJ5E-IZ for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Dec 2024 02:57:44 +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 1tPv77-000oNW-Iv for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Dec 2024 02:57:41 +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.94.2) (envelope-from ) id 1tPv77-000oNN-8y for pgsql-bugs@lists.postgresql.org; Tue, 24 Dec 2024 02:57:41 +0000 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tPv75-000HVG-1m for pgsql-bugs@lists.postgresql.org; Tue, 24 Dec 2024 02:57:40 +0000 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-5401bd6cdb7so5337467e87.2 for ; Mon, 23 Dec 2024 18:57:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735009057; x=1735613857; 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=xVL7IUYPoSZtwK9O8fh8IngxplNKvytRgakCaXk98a8=; b=WEPAZPdIy2IR8+i7yK5HygPeT9B9wDp6nQLeL0Khd0ZHvrG9QSv2zcfafIP5tePTfY ruNcnSwp3slfoJtaMgBDmTNoo8HCn3SRxahx4Y3ouBJPEy6V6CEacF+99TH52tH+D93z lwuwrTgITJuWdbSSi/OIN2iNRZ0RcMq+HUuVIU1CCHrYWA1qQL9tUpeXHvATPiUUwSXM qJGiQv3gbMqXvTRI/Eor+l7s3GofmIXtSo7zTPL2rktljW3Pfn9q1OAiP1Ad5MNG25WU LxLj/JO0gJW2CUjz3XjHUO4Ks52lVVegSViRPOUFaMM43mGLw0xDhiX2xLWCvaJt57vw Yjtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735009057; x=1735613857; 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=xVL7IUYPoSZtwK9O8fh8IngxplNKvytRgakCaXk98a8=; b=nzbVtNdEZ8KnppJ4aKPOlOBquUTq8d7I09H4BcuY7b7u+OvpAJIJ6lqyz/8Jpc1WZ8 5hCnWjsyEVvEauA5vXb20vkFjGM5OVSXYZuEYN/7V9fm0NY/sC4c0yh75A2pJlYFCn3I 4KsTJoDAcuoiSKjJRsxuEAaBOzszO1rPVsWihxVyWjdGijk8QDFrJxvmh6oPEZmttmLn 5r/08VIGBiKxMiTQ/xg7sX7dJnz2X5xh+t9iUkrIpnpfSqfDc9ecbLkEzyB8fjHPH/oS z+MrgIn2eZPEf2cs+ol2ecGRx8ho+b4c9KBr86Jl5l7mhwuOMQAEa68uFe+40B+EKG75 kaFg== X-Forwarded-Encrypted: i=1; AJvYcCVT7VO0F3jWw+0Tdy8mH7bZhBiBjBvagKifTrWFqGr7B9hfXIZuDw0yTBQ9688K/oEOiWODD0wDQ8St@lists.postgresql.org X-Gm-Message-State: AOJu0YxNC7SsRG4HOyQlm1UzxVMm4luoU3uYuum5SLLewahhxL2cYvvB Z3v4EeW4xSISKjEkfIDrhEudS0AIrhE9Y60MEGiwDzOC7JixCmm2 X-Gm-Gg: ASbGnctiI7HHIWu5vtBdo7sTpcbalj0lpOSaKkhYtnCqHHyUrAWub2jH8BiqP7DmDzQ /c/UsGErPTwJUgn4TmtRBVkPdeXK85AUIOabL7bEn6+EQ+8ml2bmFYGoFnv2xf9k2XJwIDRECBI BY4hOJBqCn5O8/potj4Uv9b11s7HYzUeO21SI1fAyOotumjgb/4E2aEWhTGfexLkWLYbVMneZyH tkHN7L3xsYj1ISIqBCyXMdO+YqA4P9nBz/1gIBFcUHrfsveQwW5lceMNA== X-Google-Smtp-Source: AGHT+IG/Zop3Lywu3YinDMdPHYaoZp0Z2xif97+mZ3k49lqfJA6Acz38uGV6QdiximK3Gy+UjMM6sg== X-Received: by 2002:a05:6512:281b:b0:540:1fae:b355 with SMTP id 2adb3069b0e04-5422956bdc1mr4286371e87.52.1735009056781; Mon, 23 Dec 2024 18:57:36 -0800 (PST) Received: from [172.30.48.70] ([93.174.131.138]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-542238217a4sm1438757e87.191.2024.12.23.18.57.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 23 Dec 2024 18:57:36 -0800 (PST) Message-ID: Date: Tue, 24 Dec 2024 09:57:31 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #18751: Sub-optimal UNION ALL plan To: Tom Lane , Kirill Reshke Cc: pingw33n@gmail.com, pgsql-bugs@lists.postgresql.org References: <18751-887c7bf4e67e65a7@postgresql.org> <1347038.1734967136@sss.pgh.pa.us> Content-Language: en-AU From: Andrei Lepikhov In-Reply-To: <1347038.1734967136@sss.pgh.pa.us> 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 12/23/24 22:18, Tom Lane wrote: > Kirill Reshke writes: >> I reproduced this on REL_16_STABLE, HEAD & REL_13_STABLE, so this is >> not really a bug, just a missing optimization? > > Yeah. I believe what is happening is that the addition of the WHERE > clause forces the second sub-SELECT to be planned as an independent > query. And that level of planning has no idea that it might be > useful to produce a result ordered by "t", so it doesn't generate > a sub-plan that can do that. Then the best that the outer level > can do is sort after-the-fact. I didn't discover the case deeply yet, but it looks similar to your improvement of CTEs in a65724d. BTW, SQL Server also uses bad variant of the plan for this query, or I just don't know how to cook that soup properly. -- regards, Andrei Lepikhov