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 1pUot0-0003s2-F2 for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Feb 2023 13:10:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pUosy-0007I8-KL for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Feb 2023 13:10:16 +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 1pUosy-0007Hy-92 for pgsql-hackers@lists.postgresql.org; Wed, 22 Feb 2023 13:10:16 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pUosv-0002IL-5V for pgsql-hackers@lists.postgresql.org; Wed, 22 Feb 2023 13:10:15 +0000 Received: by mail-wm1-x333.google.com with SMTP id m14-20020a7bce0e000000b003e00c739ce4so5305570wmc.5 for ; Wed, 22 Feb 2023 05:10:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; t=1677071411; 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=L820uiKAjuGfe3vuKFVDIk1D/C8s5wLe/16HiuNIWSg=; b=ATM1iQVWZsnpYPjMZVXpGVIKNFHFeyxpAzL8ANEa+LgWhf17Afdy01lBupKjrWKEgC ArkI2EOP9oOOzB+P6Pnsnz8t3I77a4ICZuSZCdsVxnKfCaWXhf+9mRQY//JZBuRtc77S qWmYWcOfMXs3VpZkCdsqbUSswwpl4smpVDPNg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677071411; 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=L820uiKAjuGfe3vuKFVDIk1D/C8s5wLe/16HiuNIWSg=; b=0AHV2I7knPYFulnNvvKCuHDhiZ4H4vmMJKZNHFehaxDabI+Gxhvbw6N5ojwAo/7v8E k22SvDsQLQD8k1apoEnQtP2WwTqt8l7ZtxQ2fP7JHAM1aAZ1Lbd30O8ww2m1SsQysk1Z U9O6cyZiqM0KexRa+DJ3iGi3PX/NbNPzy2Pkqt2GAwLWZUz0se11bEPdKZN6Lng2v4xJ wy9GnJVktgPvhemsxH4SDwzL9Bn/VLGVqx0cZnoObCIxm+NDv4LukD0dhYq6w9X/OesT zAhUjCI7qlgrJbfVx3W1o6vWwHxbOSy5VVuSgJONr+EBm9Nzie3yKpcH3o1nh/QmIBPM wy0w== X-Gm-Message-State: AO0yUKWbv0FSUD+rGpnUt8opthn45MGcc3kcpMOfYx3mD9hpaMhwKqJx 18XfL7LwpLVpF+YttvG5dmNyFKcWrljsQM6yZrrDJhwo33ZtcQR+J2vlSMBy1Mrk5atUm7Lw/LP jtUcOVtQZG4jExOpAsAvIpt5eXaaz7NT2Zq2V2Mk/pSE772teduACUkjuPdKbtf1v4kezWCrvc5 HLFecZO+qxe1AQTV9V X-Google-Smtp-Source: AK7set8aa3mfDCk888IXNBMQ+GS519u/tI//gYbV3UInFF/5yyd9IEE6eCU148THkBcAqxZrDnOSEA== X-Received: by 2002:a05:600c:4494:b0:3e1:f8af:8da4 with SMTP id e20-20020a05600c449400b003e1f8af8da4mr82790wmo.1.1677071411118; Wed, 22 Feb 2023 05:10:11 -0800 (PST) Received: from aivenlaptop.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id m3-20020a05600c4f4300b003df5be8987esm535113wmq.20.2023.02.22.05.10.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 05:10:10 -0800 (PST) From: Ronan Dunklau To: PostgreSQL Developers Cc: David Rowley Subject: Re: Allow ordered partition scans in more cases Date: Wed, 22 Feb 2023 14:10:48 +0100 Message-ID: <2868814.e9J7NaK4W3@aivenlaptop> In-Reply-To: References: 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 Thank you for improving this optimization ! Le mardi 21 f=E9vrier 2023, 04:14:02 CET David Rowley a =E9crit : > I still need to look to see if there's some small amount of data that > can be loaded into the table to help coax the planner into producing > the ordered scan for this one. It works fine as-is for ORDER BY a,b > and ORDER BY a; so I've put tests in for that. I haven't looked too deeply into it, but it seems reasonable that the whole= =20 sort would cost cheaper than individual sorts on partitions + incremental=20 sorts, except when the the whole sort would spill to disk much more than th= e=20 incremental ones. I find it quite difficult to reason about what that thres= hold=20 should be, but I managed to find a case which could fit in a test: create table range_parted (a int, b int, c int) partition by range(a, b); create table range_parted1 partition of range_parted for values from (0,0) = to=20 (10,10); create table range_parted2 partition of range_parted for values from (10,10= )=20 to (20,20); insert into range_parted(a, b, c) select i, j, k from generate_series(1, 19= )=20 i, generate_series(1, 19) j, generate_series(1, 5) k; analyze range_parted; set random_page_cost =3D 10; set work_mem =3D '64kB'; explain (costs off) select * from range_parted order by a,b,c; It's quite convoluted, because it needs the following: - estimate the individual partition sorts to fit into work_mem (even if th= at's=20 not the case here at runtime) - estimate the whole table sort to not fit into work_mem - the difference between the two should be big enough to compensate the=20 incremental sort penalty (hence raising random_page_cost). This is completely tangential to the subject at hand, but maybe we have=20 improvements to do with the way we estimate what type of sort will be=20 performed ? It seems to underestimate the memory amount needed. I'm not sur= e=20 it makes a real difference in real use cases though.=20 Regards, =2D- Ronan Dunklau