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 1uD1tq-005ZHb-AV for pgsql-general@arkaria.postgresql.org; Thu, 08 May 2025 14:06:58 +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 1uD1tp-003e8D-C1 for pgsql-general@arkaria.postgresql.org; Thu, 08 May 2025 14:06:57 +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 1uD1tp-003e84-1k for pgsql-general@lists.postgresql.org; Thu, 08 May 2025 14:06:57 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uD1tm-000q7z-14 for pgsql-general@lists.postgresql.org; Thu, 08 May 2025 14:06:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 548E6p6q184134; Thu, 8 May 2025 10:06:51 -0400 From: Tom Lane To: Karsten P cc: pgsql-general@lists.postgresql.org Subject: Re: Feature-Request: Performance-Optimization when using limit in combination with order by on querys using union In-reply-to: References: Comments: In-reply-to Karsten P message dated "Thu, 08 May 2025 12:57:40 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <184132.1746713211.1@sss.pgh.pa.us> Date: Thu, 08 May 2025 10:06:51 -0400 Message-ID: <184133.1746713211@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Karsten P writes: > i'm sorry i didn't check that first. it just won't work in my real-life > example. > though each part of the query is using an index-scan it is than using a > 'normal' append > instead of a merge-append, but i don't know why. The "Subquery Scan" nodes shown in your real-life example indicate that you're using views that the planner is unable to flatten completely, and those are preventing detection that the index you want to use would be helpful. The view you showed originally wouldn't be that, so there is something you're doing that you left out. It looks like your actual view contains some WHERE restrictions in the UNION arms, which I think are enough to cause this problem. Even then, though, the "Subquery Scan" nodes get optimized away in simple tests, which means there's an additional optimization blocker. I'd look closely at whether the output column types of the UNION arms match. regards, tom lane