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 1pEC2U-0002JN-4y for pgsql-hackers@arkaria.postgresql.org; Sat, 07 Jan 2023 16:27:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pEC2S-0003nX-JW for pgsql-hackers@arkaria.postgresql.org; Sat, 07 Jan 2023 16:27:20 +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 1pEC2S-0003nN-7t for pgsql-hackers@lists.postgresql.org; Sat, 07 Jan 2023 16:27:20 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pEC2L-0002Xg-RR for pgsql-hackers@lists.postgresql.org; Sat, 07 Jan 2023 16:27:19 +0000 Received: by mail-pl1-x632.google.com with SMTP id d15so4906155pls.6 for ; Sat, 07 Jan 2023 08:27:13 -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=ShDVRq5IgV/4C4BP7CzpOOFFaVjML4elERbWCBc+l9M=; b=cPMeiEPBfHp4yTB6jFJ3Yftd7Da9BOygiXHccCCoM+qIAS6kOPw6LhQ965Qun9ZRLn 39G41lA+QMnLMX9QouD3KOfeeF6raVy2+/TPBdLe2Kug8KY0IVupnIkwsFjelZfqAsMt JapPYcBS1lsO6Pstt/rUMPMAmzCE0jb1nYEAMKz6PwTv5SROe3OevLjscnj/nRU7yI/H 9fiUjPSMTQwTz5AK6HSwTVdx3c+PPVa7FgG6mPWJrz3TsInDjTOrK2DBBaJlaSvC6qx8 JZxQ/KFAOH81+wN6wnGJ6M4IieSQ2cDTXkaun4SmnvW+GwZsEZwlQ8O0RCb+RGxr6dMC fxpw== 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=ShDVRq5IgV/4C4BP7CzpOOFFaVjML4elERbWCBc+l9M=; b=cYY2jdWHArUSzimQ6wjCGs14cNoMD8RSUpMawmZf/+RPtbcTO4J6P0sjpuP3+zycSg pu143BYhVSbjwoPBkMS1mncVsbBlYygXRTnZ1fsiTT8qBa0JjNFwuhWeTG4n8HsCleIw wd3yMhel2oN9ZQxkqLAWAcTk5/mcbJRPNXTUjP/drgN9D9M+h+quNoitw3WRItxsfIv8 IE96oH2hsXQqca0VU+YxxJFkqWSgCIJODgbHyQY8N6fYOha5YZLgYEUX8c0ZfQVdcYgX pyVGGWJ1fP+yUDnjj0tDfxbFaTfN5r9x7R0hJvqU4rsBQxIO6+UppIDUSC81Gxb6oDjI YAvQ== X-Gm-Message-State: AFqh2krpXqbFALmotG2gLZFHIBw/dDx+hPdoK7Bx8K6zTb5/3RlSJNU0 klO1yrwUfNnAN7f1WtiXV2U= X-Google-Smtp-Source: AMrXdXsbmZj3GkM0XMK9pGSVXyXp7nA8vz6cZ1jEoUYCOTRAkonwm0h/L+BlTChL5YD/UgWb6cUWDg== X-Received: by 2002:a17:902:ab44:b0:193:118f:5ded with SMTP id ij4-20020a170902ab4400b00193118f5dedmr4263564plb.20.1673108830901; Sat, 07 Jan 2023 08:27:10 -0800 (PST) Received: from [10.0.2.15] ([106.202.139.208]) by smtp.gmail.com with ESMTPSA id f5-20020a170902ce8500b00178b9c997e5sm2968799plg.138.2023.01.07.08.27.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 07 Jan 2023 08:27:10 -0800 (PST) Message-ID: Date: Sat, 7 Jan 2023 21:57:07 +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> From: Ankit Kumar Pandey In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 07/01/23 09:58, David Rowley wrote: > > The attached patch has no tests added. It's going to need some of > those. While writing test cases, I found that optimization do not happen for case #1 (which is prime candidate for such operation) like 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 This happens because mutual exclusiveness of two operands (when number of window functions > 1) viz is_sorted and last activeWindow in the condition: ( !is_sorted && lnext(activeWindows, l) == NULL) For 2nd last window function, is_sorted is false and path keys get added. In next run (for last window function), is_sorted becomes true and whole optimization part is skipped. Note: Major issue that if I remove is_sorted from condition, even though path keys are added, it still do not perform optimization and works same as in master/unoptimized case. Perhaps adding path keys at last window function is not doing trick? Maybe we need to add pathkeys to all window functions which are subset of query's order by irrespective of being last or not? Case #2: For presorted columns, eg CREATE INDEX depname_idx ON empsalary(depname); SET enable_seqscan=0; EXPLAIN (COSTS OFF) SELECT empno,         min(salary) OVER (PARTITION BY depname) depminsalary FROM empsalary ORDER BY depname, empno; Is this correct plan: a)                       QUERY PLAN -------------------------------------------------------  Incremental Sort    Sort Key: depname, empno    Presorted Key: depname    ->  WindowAgg          ->  Index Scan using depname_idx on empsalary (5 rows) or this: b) (Akin to Optimized version)                       QUERY PLAN -------------------------------------------------------  WindowAgg    ->  Incremental Sort          Sort Key: depname, empno          Presorted Key: depname          ->  Index Scan using depname_idx on empsalary (5 rows) Patched version does (a) because of is_sorted condition. If we remove both is_sorted and lnext(activeWindows, l) == NULL conditions, we get correct results in these two cases. -- Regards, Ankit Kumar Pandey