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 1pHIfw-00025b-6L for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Jan 2023 06:08:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pHIfu-0002C5-Nx for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Jan 2023 06:08:54 +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 1pHIfu-0002Bv-D2 for pgsql-hackers@lists.postgresql.org; Mon, 16 Jan 2023 06:08:54 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pHIfr-0003zl-W2 for pgsql-hackers@lists.postgresql.org; Mon, 16 Jan 2023 06:08:53 +0000 Received: by mail-pj1-x1030.google.com with SMTP id z9-20020a17090a468900b00226b6e7aeeaso30086644pjf.1 for ; Sun, 15 Jan 2023 22:08:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=SAOWqxgbe6+HLJE/qLTgIjDE7rzRYG7x58lsxg7lf9g=; b=emNVw4+WRFqKLy0J0E3+HGDTB47w7q5Gpyko8b1H4ygS5VLn132aozCHsreL4SysKS DR9amqzPxHXfuCk2uDL16/eIsw86Ousisq2TSijTc6nhZncjpme6XpTx/mfBsUQF2V7N DcM1ZjGZ5M2824l5+Se7YX4TDTMS9n7+QHRSD7U0RYNdSF+0ePICWkRVR07M8pd9vuYq 0o1CM8zBbIjN3TB2fRFViv72+NcBqfcBAd2sddXPZ3jn2D7hj4vi+33R0VgaiTGTy+3j YvFkWdoMVAD79CeIQHifn5NiKLZD5O6A/lgH4bB1FLkH6lc6RXXfciM3SWtKkBq7tBsQ 9iRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SAOWqxgbe6+HLJE/qLTgIjDE7rzRYG7x58lsxg7lf9g=; b=nCOWQOZ0jhjkRK1X1Fvzt2zxBwakBcomTRjY3smBOgRPQHHtTO28PC3tHlyAr33zTj Aa6KRtfFxAM48y27NttIonUU9+SID0tpjlvREERCWbxBoWkNZbqJu8msgflNz7ZDQu2p WRI1Km0iU1+MyNk2Gll+ekA5+3J1xeSchrMBhbasEI5ew4FzGi9d9i/f1HIz95LDEDNT zhmJip3UeqY+pB2P3+qMwIXXL+XJ6X6AtqadZT/rgtygVQqRnMSFwKCksojucWTxj0Vm 3RbtnGT7+Q1UhgOdKkOWJQn8tdOOyGQVofww8pKb4EUfkXp4/+9SWrfcdb5ar+LaRG2Y WvuA== X-Gm-Message-State: AFqh2kog5Xtd5zZil7gGLXA9GZmR0HwOCHDYcv2ljcnKpmXss50QNiEs PludePXU8OC5b104DOgEJz0= X-Google-Smtp-Source: AMrXdXstua75IiJO1JdTLn+hvh7MTUHnBFKCqAdo4L0jMu2hyzRMgPX7C6Y1GtsHo3kko4D4eRVzQA== X-Received: by 2002:a17:902:740b:b0:194:5068:ff1d with SMTP id g11-20020a170902740b00b001945068ff1dmr18966970pll.16.1673849330334; Sun, 15 Jan 2023 22:08:50 -0800 (PST) Received: from [10.0.2.15] ([110.226.212.117]) by smtp.gmail.com with ESMTPSA id m7-20020a170902db0700b00192b0a07891sm18414042plx.101.2023.01.15.22.08.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Jan 2023 22:08:49 -0800 (PST) Message-ID: <2dfc2b6f-d05f-a11d-7732-563712c454b5@gmail.com> Date: Mon, 16 Jan 2023 11:38:45 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: Todo: Teach planner to evaluate multiple windows in the optimal order To: David Rowley Cc: pghackers , Vik Fearing References: <83d80853-a45c-d85c-68eb-59acfe7fb5fb@gmail.com> <2040c902-32b1-46c8-0b26-d3f43a16b9bb@gmail.com> <12f6ef19-e06e-8ea9-ad98-3fd92afb7f0b@gmail.com> <8d0b24ce-f8ba-cd63-82ae-be853b4067e8@gmail.com> <4ef7f947-526b-c4ec-f7a0-8b4586e693e4@gmail.com> <2f9f632d-3297-09dd-1d01-af3e0961c446@gmail.com> <7d5e8038-0417-2d5b-3db7-c96106bd4cb0@gmail.com> <4303947c-c313-ec43-d442-80c4c8e92490@gmail.com> Content-Language: en-US From: Ankit Kumar Pandey In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 16/01/23 09:48, David Rowley wrote: > I don't think we should touch this. It could significantly increase > the number of indexes that we consider when generating paths on base > relations and therefore *significantly* increase the number of paths > we consider during the join search. What I had in mind was just > making better use of existing paths to see if we can find a cheaper > way to perform the DISTINCT. That'll only possibly increase the > number of paths for the distinct upper relation which really only > increases the number of paths which are considered in > create_ordered_paths(). That's unlikely to cause much of a slowdown in > the planner. Okay, I see the issue. Makes sense > I'm seeing these two things as separate patches. I don't think there's > any need to add further complexity to the patch that tries to reduce > the number of sorts for WindowAggs. I think you'd better start a new > thread for this. Will be starting new thread for this and separate patch. > As far as I see it, you shouldn't be touching the distinct_pathkeys. > Those are set in such a way as to minimise the likelihood of an > additional sort for the ORDER BY. If you've fiddled with that, then I > imagine this is why the plan below has an additional Incremental Sort > that didn't exist before. > I've not looked at your patch, but all I imagine you need to do for it > is to invent a function in pathkeys.c which is along the lines of what > pathkeys_count_contained_in() does, but returns a List of pathkeys > which are in keys1 but not in keys2 and NIL if keys2 has a pathkey > that does not exist as a pathkey in keys1. In > create_final_distinct_paths(), you can then perform an incremental > sort on any input_path which has a non-empty return list and in > create_incremental_sort_path(), you'll pass presorted_keys as the > number of pathkeys in the path, and the required pathkeys the > input_path->pathkeys + the pathkeys returned from the new function. Okay, this should be straightforward. Let me try this. > As an optimization, you might want to consider that the > distinct_pathkeys list might be long and that the new function, if you > code the lookup as a nested loop, might be slow. You might want to > consider hashing the distinct_pathkeys once in > create_final_distinct_paths(), then for each input_path, perform a > series of hash lookups to see which of the input_path->pathkeys are in > the hash table. That might require adding two functions to pathkeys.c, > one to build the hash table and then another to probe it and return > the remaining pathkeys list. I'd go and make sure it all works as we > expect before going to the trouble of trying to optimize this. A > simple nested loop lookup will allow us to review that this works as > we expect. Okay make sense, will start with nested loop while it is in review and then optimal version once it is all good to go. Thanks, Ankit