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 1os5x1-0001zR-Dh for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Nov 2022 17:30:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1os5x0-0006kk-Aj for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Nov 2022 17:30:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1os5x0-0006jE-0C for pgsql-hackers@lists.postgresql.org; Mon, 07 Nov 2022 17:30:22 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1os5ww-0003Kl-Om for pgsql-hackers@lists.postgresql.org; Mon, 07 Nov 2022 17:30:20 +0000 Received: by mail-wr1-x42f.google.com with SMTP id a14so17312004wru.5 for ; Mon, 07 Nov 2022 09:30:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Q5NWm+zJ5jUZqPQXtL7nPigtwiEa14c4xVZw6zRGsOk=; b=ZcUrT2REaqDcN3FFnXcgEGB07KUPKNx8uPNor0ogrseR//WS5s5tXoDxdgmNxpWjsJ RiLyhacVz9/5Qu7p69/9/uGBlKBxprTkSszjRV60gs5m5YNPuaNKgi6gvf4nxtzdzBJG N1h53XIS6GcurxHsG4nuOpQeZGaMM4GkjVPrk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Q5NWm+zJ5jUZqPQXtL7nPigtwiEa14c4xVZw6zRGsOk=; b=lRsDvvpab1X+uP8vn5AXVdEgivOpl1igDy85SraJ2HMVNrWZ3I9Dt8+1IaxkXIgnZ/ St52a/ZDRhCXb2jLcezobieYn7VfToTg2apd7IAbIueHF79vrb/Pv6IJPNGS+aPS7ZZX RJik1zCO/K2NDf0zWmKtz+CXE6WaimbhHZ+u6Hlxq5Gyj7sO2ReiqZwxzkq1vHW/hibC oPXLMeGQYbI5ArinNksmmArOAoEtSn9fRJwiORnHPy6Px9GNEac0j8Mb2dcxTc8CsDqR YeBsjoDe6zO19LgivRxEsa6uCWNNTAbv3UymQZ4MYK0GlY8VhrSeALtDxTqwllKpHfPt 3dCw== X-Gm-Message-State: ACrzQf19+889LLhpb6Qsr8DoIw7p7RZ8lEyDTnpLjn4qLvT/xxISfWeV 9SCdJCbmfIa3HkOz8jPnSMAjaA== X-Google-Smtp-Source: AMsMyM70UjerUrVljzUlEvmxS+a8q18njB8jGKZ8S7OSXwZWknZyMKqQ72xPTT0OKJsdw0D//WCSKA== X-Received: by 2002:a5d:64a3:0:b0:231:f82e:9a57 with SMTP id m3-20020a5d64a3000000b00231f82e9a57mr32839399wrp.492.1667842217358; Mon, 07 Nov 2022 09:30:17 -0800 (PST) Received: from aivenlaptop.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id 124-20020a1c1982000000b003c65c9a36dfsm8629973wmz.48.2022.11.07.09.30.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 09:30:16 -0800 (PST) From: Ronan Dunklau To: David Rowley , Justin Pryzby , Pavel Luzanov Cc: Tom Lane , Richard Guo , pgsql-hackers@lists.postgresql.org, Ranier Vilela Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates Date: Mon, 07 Nov 2022 18:30:16 +0100 Message-ID: <3699971.kQq0lBPeGt@aivenlaptop> In-Reply-To: <43b1c6fc-e2f4-c252-5ce5-3db98c6b71d4@postgrespro.ru> References: <3163474.aeNJFYEL58@aivenronan> <2202180.iZASKD2KPV@aivenlaptop> <43b1c6fc-e2f4-c252-5ce5-3db98c6b71d4@postgrespro.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Le lundi 7 novembre 2022, 17:58:50 CET Pavel Luzanov a =E9crit : > > I can't see why an incrementalsort could be more expensive than a full > > sort, using the same presorted path. >=20 > The only reason I can see is the number of buffers to read. In the plan > with incremental sort we read the whole index, ~190000 buffers. > And the plan with seq scan only required ~5000 (I think due to buffer > ring optimization). What I meant here is that disabling seqscans, the planner still chooses a f= ull=20 sort over a partial sort. The underlying index is the same, it is just a=20 matter of choosing a Sort node over an IncrementalSort node. This, I think,= is=20 wrong: I can't see how it could be worse to use an incrementalsort in that= =20 case.=20 It makes sense to prefer a SeqScan over an IndexScan if you are going to so= rt=20 the whole table anyway. But in that case we shouldn't. What happened before= is=20 that some sort of incremental sort was always chosen, because it was hidden= as=20 an implementation detail of the agg node. But now it has to compete on a co= st=20 basis with the full sort, and that costing is wrong in that case.=20 Maybe the original costing code for incremental sort was a bit too=20 pessimistic.=20 =2D- Ronan Dunklau