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 1pQoqj-0005R9-68 for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Feb 2023 12:19:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pQoqi-0004Jl-0I for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Feb 2023 12:19:24 +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 1pQoqh-0004JH-L7 for pgsql-hackers@lists.postgresql.org; Sat, 11 Feb 2023 12:19:23 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pQoqd-0001qk-OO for pgsql-hackers@lists.postgresql.org; Sat, 11 Feb 2023 12:19:22 +0000 Received: by mail-pj1-x1034.google.com with SMTP id nh19-20020a17090b365300b00233ceae8407so667351pjb.3 for ; Sat, 11 Feb 2023 04:19:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:to:subject:from:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=tZKKlypFApHlGPzyKcU+QOC/I7aMHeBIcz5pS5/1fxQ=; b=XJVrk0+gxtOmv2sKxqmHb2NgwY8RplIIPJZG+eEifj5eJvDavZoPd1HS6SpysE7yPa mpsTzS6buRgotMUMIurqKm5l594BYliAjTW7s8ebypSR3KkBmN86q/imsIOJl5ZWnR/Y 5247kViLq/lXtafOK+3TMrl3txqx7ZGef3qt4gO938oRfrM8iY3X6z14ATdYY38BgUJE /kuSt74wKZlFk2xnxjdg7iQ3MJmnX85BMXp5fQ2M5blv23U1uohxnryXsySkH3Bvdzy3 ohTC4iGHJAVGCIRieaSt1BhoIT+fpdTxnfSXDlPLad7oBcejw0LTp6VbTd4qfCvkU2ya +6Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:to:subject:from:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=tZKKlypFApHlGPzyKcU+QOC/I7aMHeBIcz5pS5/1fxQ=; b=zZDL3DSytFag4jZpcnWeRVa3OWjNVrcIKw7xjfIOdEe43LnrW9Rr6eiaAa970G9Vse 2itiKvsSNomqD4u+vkJ+Sinv1L8E8ZMgN0bNLegoA3fySIVuMapMOAVzK88JHKzavOfS jwDxH7yDJGzcq414EVJ0sYVskymKGPjU2Bf+g8ncltAFqRbQM5t9v3G92QQroksaNV0o opaVveqt5gUUC4O9a2w/yCUHau/DUE9MCnVTV+6owdzENANDAb+ZsjT/cjiO3grb0Nuz eTubm60zfX+6aopPbfNbZQWQjG2Bi5JYQAYw5sqvZnHFAEyu/xUcqev7Fu40Az8ZAHRy r6Mg== X-Gm-Message-State: AO0yUKXPyPy5sbYxKBZSr86Stkcx9W7IJWOlvXbSvgIRtZ5G5x26Bzlu wK2p7QkaM6DsneGhBRrh6DCnr2mH1Rg= X-Google-Smtp-Source: AK7set9qKFjPnOch4AyfFA5MsHfpjQ/JThxFDEE+EWfNl5LKz4B6yHY1WtW6tnCp/Rt3yZrdnvHbVQ== X-Received: by 2002:a17:903:41ca:b0:198:d004:bfa0 with SMTP id u10-20020a17090341ca00b00198d004bfa0mr20728294ple.15.1676117957711; Sat, 11 Feb 2023 04:19:17 -0800 (PST) Received: from [10.0.2.15] ([115.96.20.3]) by smtp.gmail.com with ESMTPSA id d18-20020a170902b71200b001930b189b32sm4868632pls.189.2023.02.11.04.19.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 11 Feb 2023 04:19:17 -0800 (PST) Message-ID: <444ccfcc-06d6-0160-f662-9fa2075229ad@gmail.com> Date: Sat, 11 Feb 2023 17:49:02 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Content-Language: en-US From: Ankit Kumar Pandey Subject: Sort optimizations: Making in-memory sort cache-aware To: pghackers , David Rowley 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 Hi all, While working on sort optimization for window function, it was seen that performance of sort where all tuples are in memory was bad when number of tuples were very large [1] Eg: work_mem = 4 GB, sort on 4 int columns on table having 10 million tuples. Issues we saw were as follows: 1. The comparetup function re-compares the first key again in case of tie-break. 2. Frequent cache misses Issue #1 is being looked in separate patch. I am currently looking at #2. Possible solution was to batch tuples into groups (which can fit into L3 cache) before pushing them to sort function. After looking at different papers on this (multi-Quicksort, memory-tuned quicksort, Samplesort and various distributed sorts), although they look promising (especially samplesort), I would like to get more inputs as changes look bit too steep and may or may not be in of scope of solving actual problem in hand. Please let me know your opinions, do we really need to re-look at quicksort for this use-case or we can perform optimization without major change in core sorting algorithm? Are we are open for trying new algorithms for sort? Any suggestions to narrow down search space for this problem are welcomed. [1] https://www.postgresql.org/message-id/CAApHDvqh+qOHk4sbvvy=Qr2NjPqAAVYf82oXY0g=Z2hRpC2Vmg@mail.gmail.com Thanks, Ankit