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 1qxzSz-004w0W-TN for pgsql-general@arkaria.postgresql.org; Wed, 01 Nov 2023 00:52:17 +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 1qxzSw-005H5s-TX for pgsql-general@arkaria.postgresql.org; Wed, 01 Nov 2023 00:52:14 +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 1qxzSw-005H5j-Ik for pgsql-general@lists.postgresql.org; Wed, 01 Nov 2023 00:52:14 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qxzSt-003Mrt-LT for pgsql-general@lists.postgresql.org; Wed, 01 Nov 2023 00:52:13 +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 3A10q7EX2664451; Tue, 31 Oct 2023 20:52:07 -0400 From: Tom Lane To: David Rowley cc: Dimitrios Apostolou , pgsql-general@lists.postgresql.org Subject: Re: Inefficient query plan for SELECT ... EXCEPT ... In-reply-to: References: <86c352b6-c0ec-8c4d-9506-297506b1b5da@gmx.net> Comments: In-reply-to David Rowley message dated "Wed, 01 Nov 2023 11:57:03 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2664449.1698799927.1@sss.pgh.pa.us> Date: Tue, 31 Oct 2023 20:52:07 -0400 Message-ID: <2664450.1698799927@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > It would be possible to have some sort of MergeExcept operator and > have the planner consider that. Unfortunately, since the upper planner > was changed a few years ago to have it consider paths the same as the > join planner does, nobody has yet come back to the union planner to > properly pathify that. I do have a WIP patch to do this work, but I > wasn't planning on improving EXCEPT, only UNION. Making it work for > EXCEPT and INTERSECT would require a new executor operator. Yeah. We're moderately good about UNION ALL, but UNION/INTERSECT/EXCEPT are an area that nobody has ever gotten around to optimizing: the two sub-queries will be planned independently and then fed to a simplistic set-operation node. Maybe that'll get better someday but don't hold your breath. In the meantime, try to recast an EXCEPT query as an antijoin. regards, tom lane