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 1pEPBZ-0005Cr-P6 for pgsql-hackers@arkaria.postgresql.org; Sun, 08 Jan 2023 06:29:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pEPBY-0001u6-6n for pgsql-hackers@arkaria.postgresql.org; Sun, 08 Jan 2023 06:29:36 +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 1pEPBX-0001tx-OD for pgsql-hackers@lists.postgresql.org; Sun, 08 Jan 2023 06:29:35 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pEPBU-000514-RD for pgsql-hackers@lists.postgresql.org; Sun, 08 Jan 2023 06:29:34 +0000 Received: by mail-pl1-x632.google.com with SMTP id p24so6134744plw.11 for ; Sat, 07 Jan 2023 22:29:32 -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:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=QQ1WLhw2iFgpqoHFQCoqJDvvZZ4GL0q9mbasR8Bk9MM=; b=pcwyw/nzKuHHYpQGxZB5rOYBC45WNtRdUkJEP0Thi+NCMLBu6wKc5Y2M7l2qPB3MVR mQGeZX6/ld1LT40fwReUesfkmOj5gO3Rkb/o80f5IvtP9+n70uU3n1V1z0hQxhQtomFx BEneI+0hibdACQf78Xd998o83tki+ZE3qz3VCQxKrHKyToUY7bW0vpmJa3az3WpDWMP/ asH/dUjhqH3JOFvv6VdC5v54tvAVYbtbeee6WRuG7SCtNc5gOnpELVMgtpx9WPb9gnH/ hA9HqfDOPqPXI5AiozRnQLfz6TIt3wYJS4dtRVlC4mJ55Zsw8KXnN3ZtvoYEmEZ4QIvl XTAg== 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:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QQ1WLhw2iFgpqoHFQCoqJDvvZZ4GL0q9mbasR8Bk9MM=; b=oFBS6/7aZt5ZsA+rdlDBGRmrQMftjNRUs5/AYj3C7RzB3pQhYts0/Y06qWxdAVVm8W HSXC8WPkJRa1cYgx8qKBLVL0/coTlA5lOsxK8I5TwPypWf+j046O5dKkv3K9ftDjPMsX GMW0Wh8eEoMilm/NDRLSNKZpYNaylD/x0CHwSbrxw7nxepdzw/eYkLrfLl01Y/MC4ZtN 4HUvYtt7Un8zZToB7TDhR6+QCRyxA1pXsOP5Kk21k7w2fkVV8Yc1sD6EMZzkyneS2pmN Rodak/NXl+Y9PlDsIj6eyXdVZrj1nsQ9IjWKf/O3G4BmbScAn8NhB5VOCVVKhISIRSqZ 4muw== X-Gm-Message-State: AFqh2kpJM/7UKbUek5B8Qf/ntN2lzImrlDlaLHpYHPwAC3Dn7FFSoril eyLmBh9h3V88dxnP+rfDJH8= X-Google-Smtp-Source: AMrXdXtIvd0KxuRBfO0ZeYDFmu+BuZS+wbnHS8RutYfFOaoDncLITghH478f5lQoO/DR2MEaY6b8tQ== X-Received: by 2002:a05:6a20:3ca5:b0:af:ae01:54df with SMTP id b37-20020a056a203ca500b000afae0154dfmr102047817pzj.11.1673159371723; Sat, 07 Jan 2023 22:29:31 -0800 (PST) Received: from [10.0.2.15] ([106.202.139.208]) by smtp.gmail.com with ESMTPSA id b3-20020a63d803000000b00477bfac06b7sm3206744pgh.34.2023.01.07.22.29.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 07 Jan 2023 22:29:31 -0800 (PST) Message-ID: Date: Sun, 8 Jan 2023 11:59:27 +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 Content-Language: en-US To: David Rowley Cc: pghackers , Vik Fearing References: <83d80853-a45c-d85c-68eb-59acfe7fb5fb@gmail.com> <7f77ee7d-bd04-d8e2-bb34-42395fd1f7c2@gmail.com> <6ebcc137-45ea-6373-e3e1-3c304c452e1f@gmail.com> <441d135e-1941-c3ef-1649-18c3e8811549@postgresfriends.org> <9620d994-b89a-2dcf-fca5-821e19d56858@gmail.com> <01248493-182b-0f92-f2f2-ff28dc4b2e83@gmail.com> <295ebbb0-f5b4-f090-b27e-9e069ef7eae1@gmail.com> 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 08/01/23 04:06, David Rowley wrote: > On Sun, 8 Jan 2023 at 05:45, Ankit Kumar Pandey wrote: >> Attached patch with test cases. > I can look at this in a bit more detail if you find a way to fix the > case you mentioned earlier. i.e, push the sort down to the deepest > WindowAgg that has pathkeys contained in the query's ORDER BY > pathkeys. > EXPLAIN (COSTS OFF) > SELECT empno, > depname, > min(salary) OVER (PARTITION BY depname ORDER BY empno) depminsalary, > sum(salary) OVER (PARTITION BY depname) depsalary > FROM empsalary > ORDER BY depname, empno, enroll_date; > QUERY PLAN > ----------------------------------------------- > Incremental Sort > Sort Key: depname, empno, enroll_date > Presorted Key: depname, empno > -> WindowAgg > -> WindowAgg > -> Sort > Sort Key: depname, empno > -> Seq Scan on empsalary > (8 rows) > > You'll also need to pay attention to how the has_runcondition is set. > If you start pushing before looking at all the WindowClauses then you > won't know if some later WindowClause has a runCondition. Yes, this should be the main culprit. > Adding an additional backwards foreach loop should allow you to do all the > required prechecks and find the index of the WindowClause which you > should start pushing from. This should do the trick. Thanks for headup, I will update the patch with suggested changes and required fixes. Regards, Ankit