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 1jKA4e-0006ay-0J for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Apr 2020 00:20:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jKA4c-0007fx-He for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Apr 2020 00:20:38 +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 1jKA4c-0007fj-6W for pgsql-hackers@lists.postgresql.org; Fri, 03 Apr 2020 00:20:38 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jKA4Y-0008PG-N2 for pgsql-hackers@lists.postgresql.org; Fri, 03 Apr 2020 00:20:37 +0000 Received: by mail-wm1-x342.google.com with SMTP id t8so5428525wmi.2 for ; Thu, 02 Apr 2020 17:20:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=2PytLFMCvnXOPtqVhde8JBYcGfOz78X41U7+vlQYjQ8=; b=S2/Wyg+PvxsuuktAf/J/fzIWhyxqbUjJS7B1EYA3BH3jmyaIxrKRViNnly3qWsDPC2 RkpFIvw10/2yBwfCtL3s0Y7hXaWdm1rY/ixfmUsOLCMp4f2AyNid0zWKPoP1hS3esXy/ 82wethwBEETm8vWw+rqMVApeN/B6COWHED7g2Zarkqn4k+TZOJWMkoC/kj+Kr6U0Cs+A G61/o0h51l6/iFJC4sA65/O8PyBTZq8/LtvnjKhWxajxzTwDg+iPkpHSDKd5gZYlXWSk 3PpP0bA/Qk/TzRE5M34gkwO9QvAxAXFXzmwD21viAXBGFuWD6Zom5HhmQ0UAn4hKc8jR V6MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=2PytLFMCvnXOPtqVhde8JBYcGfOz78X41U7+vlQYjQ8=; b=LMXmcj4ifU8LBc/gZEqg5c2q+TIyrSNUOwVqX3ZsrpormGyug004dJfb9c8UegiMJb 8pjMSCNghrzdRWDwSDCvGXUec9T2d7/rE131KZTAlRJIg4gIVzjD0N3ZELnbRgJi6Hq0 DMChq8AFo9t3hm/FCocTn266fxwwL0qQPCXn0YcTaUCcJd4mLVNHP+qk5MjvkHp2NA/C ojdAOExKbluY71jhJSp08TXpQr8Ox7hjfsm36g0UIPH0x7hnsjooFQyiVfBW8XrEB1d3 460WW1M4ux/npBmkUO/zQRXD450DY7PHGeCb4zjuhFoeqy+6pgiZ9y8ERwUk5Zp4/AXn +rUw== X-Gm-Message-State: AGi0Pub5Am2GLuzE+poSQ91HIPqwW0Ov5i6iCl8/vlns26/DX3XJ2Y5Q /sC3zvbJmF5LAxJYjXnIGlLR4Q== X-Google-Smtp-Source: APiQypKMHdEm6fXM1vMQu9jFRtL7EwjLwB0mGJHACUMvrKiF3/Xh643IZ2wVZ8Dc7W3glzpdcMN9iA== X-Received: by 2002:a1c:41d4:: with SMTP id o203mr5666502wma.1.1585873232750; Thu, 02 Apr 2020 17:20:32 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id u17sm10391068wri.45.2020.04.02.17.20.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Apr 2020 17:20:31 -0700 (PDT) Date: Fri, 3 Apr 2020 02:20:28 +0200 From: Tomas Vondra To: James Coleman Cc: Tom Lane , Alvaro Herrera , Michael Paquier , Rafia Sabih , Peter Geoghegan , Simon Riggs , Shaun Thomas , Dmitry Dolgov <9erthalion6@gmail.com>, Alexander Korotkov , PostgreSQL Developers , Andreas Karlsson Subject: Re: [PATCH] Incremental sort (was: PoC: Partial sort) Message-ID: <20200403002028.kp3rxncmwvsui6xz@development> References: <20200401015925.bmbbqc3ple5rf3yn@development> <20200401024424.g4uuerdvxqohvzks@development> <20200401214215.ut6b72e3zlbm33w2@development> <20200402024746.5wlwk6q7eiv5faux@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, Thanks, the v52 looks almost ready. I've been looking at the two or three things I mentioned, and I have a couple of comments. 1) /* XXX comparison_cost shouldn't be 0? */ I'm not worried about this, because this is not really intriduced by this patch - create_sort_path has the same comment/issue, so I think it's acceptable to do the same thing for incremental sort. 2) INITIAL_MEMTUPSIZE tuplesort.c does this: #define INITIAL_MEMTUPSIZE Max(1024, \ ALLOCSET_SEPARATE_THRESHOLD / sizeof(SortTuple) + 1) supposedly to keep the array size within ALLOCSET_SEPARATE_THRESHOLD. But I think it fails to do that, for a couple of reasons. Firstly, ALLOCSET_SEPARATE_THRESHOLD is 8192, and SortTuple is 21B at minimum (without padding), so with 1024 elements it's guaranteed to be at least 21kB - so exceeding the threshold. The maximum value is something like 256. Secondly, the second part of the formula is guaranteed to get us over the threshold anyway, thanks to the +1. Let's say SortTuple is 30B. Then ALLOCSET_SEPARATE_THRESHOLD / 30 = 273 but we end up using 274, resulting in 8220B array. :-( So I guess the formula should be more like Min(128, ALLOCSET_SEPARATE_THRESHOLD / sizeof(SortTuple)) or something like that. FWIW I think the whole hypothesis that selecting the array size below ALLOCSET_SEPARATE_THRESHOLD reduces overhead is dubious. AFAIC we allocate this only once (or very few times), and if we need to grow the array we'll hit the threshold anyway. I'd just pick a reasonable constant - 128 or 256 seems reasonable, 1024 may be OK too. 3) add_partial_path I'm a bit torn about using enable_incrementalsort in add_partial_path. I know we've done that to eliminate the overhead, but it just seems weird. I wonder if that really saves us anything or if it was just noise. I'll do more testing before making a decision. While looking at add_path, I see the comparisons there are made in the opposite order - we first compare costs, and only then pathkeys. That seems like a more efficient way, I guess (cheaper float comparison first, pathkey comparison with a loop second). I wonder why it's not done like that in add_partial_path too? Perhaps this will make it cheap enough to remove the enable_incrementalsort check. 4) add_partial_path_precheck While looking at add_partial_path, I realized add_partial_path_precheck probably needs the same change - to consider startup cost too. I think the expectation is that add_partial_path_precheck only rejects paths that we know would then get rejected by add_partial_path. But now the behavior is inconsistent, which might lead to surprising behavior (I haven't seen such cases, but I haven't looked for them). At the moment the add_partial_path_precheck is called only from joinpath.c, maybe it's not an issue there. If it's OK to keep it like this, it'd probably deserve a comment why the difference is OK. And the comments also contain the same claim that parallel plans only need to look at total cost. 5) Overall, I think the costing is OK. I'm sure we'll find cases that will need improvements, but that's fine. However, we now have - cost_tuplesort (used to be cost_sort) - cost_full_sort - cost_incremental_sort - cost_sort I find it a bit confusing that we have cost_sort and cost_full_sort. Why don't we just keep using the dummy path in label_sort_with_costsize? That seems to be the only external caller outside costsize.c. Then we could either make cost_full_sort static or get rid of it entirely. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services