Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m5qpZ-0003TA-Bx for pgsql-general@arkaria.postgresql.org; Tue, 20 Jul 2021 14:34:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m5qpY-0000ZN-7E for pgsql-general@arkaria.postgresql.org; Tue, 20 Jul 2021 14:34:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m5qpX-0000ZF-UB for pgsql-general@lists.postgresql.org; Tue, 20 Jul 2021 14:34:43 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m5qpU-0004l4-AM for pgsql-general@lists.postgresql.org; Tue, 20 Jul 2021 14:34:42 +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 16KEYbEJ368622; Tue, 20 Jul 2021 10:34:37 -0400 From: Tom Lane To: Nicolas Seinlet cc: "pgsql-general@lists.postgresql.org" Subject: Re: More records after sort In-reply-to: References: Comments: In-reply-to Nicolas Seinlet message dated "Tue, 20 Jul 2021 14:25:29 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <368620.1626791677.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 20 Jul 2021 10:34:37 -0400 Message-ID: <368621.1626791677@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Nicolas Seinlet writes: > I'm facing a strange behaviour and I don't understand why. From a wider = query plan,=C2=A0https://explain.dalibo.com/plan/7yh=C2=A0,a table is scan= ned and filter based on its primary key, returning 98 records. Then, those= records are sorted on another field, and the output is=C2=A0758,247,643 r= ecords. You haven't showed us the whole plan; tsk tsk. However, I'm going to bet that this sort is the inner input of a merge join, and that the apparent "extra" tuples are caused by the merge backing up and rescanning the sort result to deal with duplicate join keys in its outer input. There must be a heck of a lot of duplicates. The planner would typically avoid using merge join if it knew that, so I'm wondering if you have up-to-date statistics for the outer side of the join. regards, tom lane