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 1tQ0rL-00H2Si-Dz for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Dec 2024 09:05:47 +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 1tQ0rK-003WRI-6n for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Dec 2024 09:05:45 +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 1tQ0rJ-003WR9-Sg for pgsql-bugs@lists.postgresql.org; Tue, 24 Dec 2024 09:05:45 +0000 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tQ0rI-000Jvi-08 for pgsql-bugs@lists.postgresql.org; Tue, 24 Dec 2024 09:05:44 +0000 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-2f13acbe29bso5068344a91.1 for ; Tue, 24 Dec 2024 01:05:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735031143; x=1735635943; 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=2PkxOJmpsgsCSUlwVAm1JqJ6bTLBjzTIjikAN1Z9gxw=; b=O58emWJDVaDcfWaDcFTnxucmFQDKE1Rh6+TlwDt/unHNH3LfpktssYa5PgXWAt8dn0 N4i0yOkUlpjyjXk1JL0SwinkFd2jG1Ixhsy572RI9flfsgWF1q0PWUXIF6N44r8IvWS/ RpQio3dkUO0eqG9tAwfJjgLEYGtj8t2msdzxKDAQ+aAz54Ilo9TiuNsaAyfEZBlWxggu oVSpqKQxu9eHcfJPY9p/okSgfNjYMg3ENks7lYtaxIRgCRerMvd9zrpImW9uFU27dDTb 2sku88YgZPHS/bKRkUbJ0gelK0k+t1P8aShrPkJQpJwlSTgN1dllx4qSNT2tcVgfcMBH 3JVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735031143; x=1735635943; 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=2PkxOJmpsgsCSUlwVAm1JqJ6bTLBjzTIjikAN1Z9gxw=; b=DZWWXKqyhecDTEB7XLwkjTWoOfOLuZm5gJZkZfZ69gNEtgdjFoHq2iDqmFGZzU+9Ct 3D3J0+eN3KOXI6570Fqd9OEyIQdSnZlZqodjfem97XPJxDpFC2lFmn5vNpl1osEEaj3H yO5hJ4qUWUms/LrVFeGUAHHtKBD5ZP2Nfmvoh2SRar3C2xQzGEASUBJoZSg6YDNuf+BG XZsHbFti1oXb5Og/kuIzjFNrEJbeHk2olEhs2uNVmpYfFF1mLT78NdZYCJ4yYCZ0x7Qc 5xSC2PTBrpP5Kj/uQal1IiZZYHnzzbOtIxtL9Blu6IvKudCGj802S0xm+tnTNNcR6mZZ 5Pig== X-Forwarded-Encrypted: i=1; AJvYcCW3k6WBbjAdu3IJx3BtYACenV4bmta6hWUyd1RHhe0gac+78YsdTw08RNKrTS1nzWYDL9VAJ/wtlA22@lists.postgresql.org X-Gm-Message-State: AOJu0Yz231VbAhZTuVEbFMTrIO5Zmo3sc6fj5lQdvynDFtrGODUSC3qj Bj6HOfoIhXeSJm4rCLAmWnkiYRoLWwnuMpW8utka9vmyCy2pf08U X-Gm-Gg: ASbGncvqRPjfsF51fBoJpO1Rz2YgDROThS6eidL775Yazi4nJMuRDcrWBzxl/dvdkoo 76wk6V4TbStSDLDiSnseMGkB4WtD24YJu470Coj5r1TIOSMFHA2tdcQ4JAFtpBVYHZl1rfOJRqa s9cMaHZKMzNdtPNaP5hMs0d4cfe26tSRtEa3DmyeaKjOAyzJvUHpFLBzS6ruiKpE8Wn+Utg1UFA MwfvO9M8ke1Swv9DmaDnpNSA1lBmpBJ4XnmS5JvpZVvot+QqXaux/cGpD8kEZaNDw35+hUsFEBs NGW1y4E+RLVGXYXuxUXx+kgn7i9cs0g= X-Google-Smtp-Source: AGHT+IG1E9XdZr18fvBBYTMv/zvmf3iSzfm7pQaiA+7qHYbfmC0+gY8eQDunmlmR5H1PIYbKaN6lAg== X-Received: by 2002:a17:90b:2b83:b0:2ef:9dbc:38d1 with SMTP id 98e67ed59e1d1-2f4435ad5e6mr27651151a91.5.1735031142770; Tue, 24 Dec 2024 01:05:42 -0800 (PST) Received: from [10.10.20.106] (node-od4.pool-125-26.dynamic.totinternet.net. [125.26.123.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9d9775sm85736855ad.148.2024.12.24.01.05.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 24 Dec 2024 01:05:42 -0800 (PST) Message-ID: Date: Tue, 24 Dec 2024 16:05:38 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #18751: Sub-optimal UNION ALL plan To: Tom Lane Cc: Kirill Reshke , pingw33n@gmail.com, pgsql-bugs@lists.postgresql.org References: <18751-887c7bf4e67e65a7@postgresql.org> <1347038.1734967136@sss.pgh.pa.us> <1531450.1735012018@sss.pgh.pa.us> Content-Language: en-AU From: Andrei Lepikhov In-Reply-To: <1531450.1735012018@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/24/24 10:46, Tom Lane wrote: > Andrei Lepikhov writes: >> On 12/23/24 22:18, Tom Lane wrote: > As of v17 there is some mechanism to do that (see the setops > argument to subquery_planner), but I now realize that that > was designed in a really short-sighted fashion: it *only* > works with SetOperation nodes. We'd have to refactor that > so that what the upper query passes down is desired pathkeys, > or at least something closer to a pathkey than a SetOperation. I have waited for such a proposal for years! It would be beneficial, of course. I may imagine an implementation that will consider LIMIT statements or grouping columns because they exist in the parse tree. But sometimes I have seen examples where MergeJoin or IncrementalSort could bring speedup in case of sorted subquery output. But without planning, we don't realise we need this type of sorting. For example: ------------ CREATE TABLE test (x int PRIMARY KEY); INSERT INTO test SELECT gs FROM generate_series(1,1E4) AS gs; VACUUM ANALYZE; SET enable_nestloop = f; SET enable_hashjoin = f; EXPLAIN (COSTS OFF) SELECT q1.x FROM (SELECT t1.x FROM test t1 LIMIT 10) AS q1, test t2 WHERE q1.x=t2.x LIMIT 10; /* Limit -> Merge Join Merge Cond: (t2.x = t1.x) -> Index Only Scan using test_pkey on test t2 -> Sort Sort Key: t1.x -> Limit -> Seq Scan on test t1 */ -- regards, Andrei Lepikhov