Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wRS7q-002M9e-2I for pgsql-hackers@arkaria.postgresql.org; Mon, 25 May 2026 10:01:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wRS7o-00139B-2A for pgsql-hackers@arkaria.postgresql.org; Mon, 25 May 2026 10:01:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wRS7o-001391-1F for pgsql-hackers@lists.postgresql.org; Mon, 25 May 2026 10:01:33 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wRS7m-00000001KOS-3a1A for pgsql-hackers@lists.postgresql.org; Mon, 25 May 2026 10:01:32 +0000 Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-6746d0b2b4aso15931042a12.3 for ; Mon, 25 May 2026 03:01:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1779703288; cv=none; d=google.com; s=arc-20240605; b=LhvYHeaT4SyHwj53DgoVO4/f5Aot6xKRQlLTblOKSOAK4YUocT+vKAeNalNJU9I0Y7 2fo50Dqr3qIpJZutUQB7xvt5jp2OwNF/84xAxVGk20HKeNcw4fZV0FFQIrEZRDZ+jLL2 4VKwbU1YkBMLNO+JRD5SmpMmx1gZCHxI5k+4hzu4HoIthbsJHZOTWatrN3MBC2pul1bS 9j/mpSJ8jvkJPLHhg+SBw9oZKlldJ3AYiZ/cwTOqet3hVp/JbXjya9FCIg71Tzt+u9Qd zubDGN71JWsghabtYlWqSHNnLbDZo/9TiQjYKUYRUCpFeXY8JZM+vd3Am+djXVUFuw7Y FZuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=aH+2D5Xgrc/NKsj3QTVuMuYtwMZQ1LUjkfBwn5pOuhQ=; fh=xPvcPmYQbLGp1VD1iAa9UWGPJRGob8qrVuwANPKNg6g=; b=VKerZo5tljeu6VAa1HFhuZ7JQzPQpbL57aeTfGi/hqNUUqwdFFhr0fzQ8Xqx8RYydI xznhUgPiUX4mwuMR8AsHgmR1516ro+Nfutk6RVW6B0PP/2NT7JdqDszTggI0KRnP/kp/ r1GERHGCW1tmm0IBvCCz8ywsfKQWsk5LRDE37Bt1aDDciZrYYSyKYx2/JdpbtZtbT3aT 93GilEhTK0PWrlDbmSJJ6FRCcOBhqlnANBK8UGm0g5np5CBG3cfTVEJ8BRXwsKSRbhbu cd+b9xbgvQcYYsIDIgOnSGH715TLxRGdt6ychnykrjGaod/ctKqs0ecyv8daR70Rc/kX ad7Q==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779703288; x=1780308088; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=aH+2D5Xgrc/NKsj3QTVuMuYtwMZQ1LUjkfBwn5pOuhQ=; b=hEVd23ZGYNCCYeEVlbCusgWw8snRQG4Cmt9u3sv3RltLb2eHBj26c9zrzTZ9fk4y7g mfs7Eu0uDxPGh/WTy3JMWowNi5VvtQYqbb3LuBDb52EmgihrIKy5g36+H9dgvfJRbADG uol8GZKNp21KiamyOcoRiUDJjty3sLcqUOk7v80/JBlMsdgoYKjDiLIloCh52ro7NDn1 REbv5NbdzMvWZc1i1mJO65RCwOgXHyH5afvfQwoMTsT5c3acs6QG6xnOr8e4vmKyFpyE fAPDejkWmYVl4r0KuMzU1ljDGHwgV/h/DlJ7HiYseOpZ1vAkbTjnlOtFz+e5yxTCUrDq 8Fow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779703288; x=1780308088; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=aH+2D5Xgrc/NKsj3QTVuMuYtwMZQ1LUjkfBwn5pOuhQ=; b=U0GCvjibBs5I70r7oKM5VBITZzvuW8PFTCwJMNDppxTeGw0rBUbF3Xl+XeumkWQtIS 0qdeewQGCwmwMx2unh1wZ9G2B9fehd5Nt3af+IwREpHdgPL7RPWfmDaC+1oJM+xlS0aH rntfr62sOiHSoPjNT6p2BgK97SzqZzdPdqmSsvS7hNuMmT1FnGlRtk5DWIrFDtn591a3 NGmLKpvGJdd/giYn/JJpFX/m5CSyYITFRpm3oQL6gBJAn7BfifErbHJfIeHXH78v8/+G LkFEfd/T/ppn1JD/LCj9OObtLpn6z//sD5v71O1fABaEgXB9fh65pNIcxxRejDFBTtUx WCbw== X-Forwarded-Encrypted: i=1; AFNElJ/qopugd78jSGWHGZBZhFIvino62jGAeO405L9WGPIwZL6L3j+08EcIlto9lnd/EtjAZCbkt3sMOSeVg/Cz@lists.postgresql.org X-Gm-Message-State: AOJu0Yyxjk2cSm/ePMLeuhCfIJzD0yz8Z7SIDBSHpTVUQ/bDnB/t32mu 2f/mplbHKbIUUp/mO0RS9Bk5ukzgFfmY/t0gPs3KfHddPvc5yE5jlZ/pV0bFMAcAIDjv8fVmnLg ZB6YxWP1xwyLhWLW3hjqWQloZyHN6w/M= X-Gm-Gg: Acq92OF32Zc1gXd37oBK+fJTEuxzNBuBo+Y/AReGKUytJpyrMeZfWdY4AtY4uK4IXGj IeaHgndOR7gGYGLI1eRziXghwUrZnU8iHhPhUppHCbO3LxpG6JKXckSZ0Ran7/W641zEMdNysvn RSdZlOgZUYErB1FhTqyoVGqaNGvy9mRhO7TJpDl7KubyR+plCOl5eGwMbc493phQ2/RybONAZ7L e1N/olq/7DWaw+6E5n01JP5tSFjet+jJ7HxJu5x+8eOiItjizscx212INB+jqTYBSydjl+ffwDv IGBikTkHohiEhzeDMeNUUYtYXVcqCDI01S0hI0zCLQmvL/wmiuzdQ0fHNI5G5ckS1SAG3BwMYbk AT+LMV6zH4j4jmInugXKjJuqnzfs8C7ndMzXekpfQ X-Received: by 2002:a17:907:7fac:b0:bd5:de3:263 with SMTP id a640c23a62f3a-bdd26ce97e4mr833371666b.46.1779703288297; Mon, 25 May 2026 03:01:28 -0700 (PDT) MIME-Version: 1.0 References: <28f47336-84e2-445e-8216-d1ce7d3ddc3e@iki.fi> <69012ece-1907-4d70-ac1a-db7897bac865@proxel.se> In-Reply-To: <69012ece-1907-4d70-ac1a-db7897bac865@proxel.se> From: Xuneng Zhou Date: Mon, 25 May 2026 18:01:15 +0800 X-Gm-Features: AVHnY4J4mpuWpikPuD9GQk3OzOVYcNFSkIfVBHrAVr-prpot8sp4dIvX_5-g4lI Message-ID: Subject: Re: Support loser tree for k-way merge To: cca5507 , Andreas Karlsson Cc: John Naylor , Sami Imseih , Heikki Linnakangas , pgsql-hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi ChangAo, On Sat, Jan 3, 2026 at 11:46=E2=80=AFAM Andreas Karlsson wrote: > > On 12/8/25 7:46 AM, cca5507 wrote: > > For heap, it reduces one tuple comparison if the keys are same and incr= ease one if not. > > For loser tree, it reduces many tuple comparisons (maybe tree's height = - 1?) if the keys > > are same and increase one if not. The bad case is all keys are differen= t, so we still need > > to decide when to use the fast path, it's hard I think. > > My suggestion is that you start with trying to find some cases where we > get regressions and measure how big these regressions are and if there > are any clear cutoffs where we can use a simple heuristic to select > algorithm. One thought I have is that pre-sorted input could be slower > with loser than with heap but since I am unfamiliar with loser trees I > could be wrong. I came across a paper written by Goetz Graefe [1] which describes the tree-of-losers priority queue model. I am not familiar with this topic, but it looks promising. If some of the techniques described there are desirable for Postgres, does it make sense to extract the loser tree as a standalone module like generic heap does? [1] https://dl.acm.org/doi/10.1145/3778176 --=20 Regards, Xuneng Zhou HighGo Software Co., Ltd.