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.94.2) (envelope-from ) id 1vE4ND-004AkY-47 for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Oct 2025 11:29:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vE4NC-000CqN-3O for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Oct 2025 11:29:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vE4NB-000CqF-QC for pgsql-hackers@lists.postgresql.org; Wed, 29 Oct 2025 11:29:48 +0000 Received: from mail-qt1-x843.google.com ([2607:f8b0:4864:20::843]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vE4N9-004NEh-0P for pgsql-hackers@lists.postgresql.org; Wed, 29 Oct 2025 11:29:47 +0000 Received: by mail-qt1-x843.google.com with SMTP id d75a77b69052e-4ed0aef49abso18281641cf.3 for ; Wed, 29 Oct 2025 04:29:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761737386; x=1762342186; 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=XkYsKtXYw69dyyPliZ1nfzuUkX7/sQeBxVXSOa3SPiE=; b=Vqkex53IL9zwKTsMNvyMCh5IuvaL78Z4Cj3LQSsQ0OhN151WvwiMM3xrmUIyrCF4L1 jYEwxdKkaZBxaQeJC7XRprNXxEJCLQ2EvUbDNVk3/yQhW/dzdMG5/JJkkBO/8YNYekoV eMlW9zGUhzpWxT/baoAYdtl/cuGhb/c2TOQubRkNWs1cPuaQMWseC+6HLfE5ECuawruq xR0M7ufNrW0zwhVz1IEWZC9LqUkMkN+K2gKDtNfMl9PPPO9CN3fkRSSIJ+PcMdy6Jq7e T5wYl9DtqMF7bbLRgBL71Meps3j+r9WaawGx8sPB1zGSI+IaPSmeBzdcMKJb0emg9uqS BGLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761737386; x=1762342186; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XkYsKtXYw69dyyPliZ1nfzuUkX7/sQeBxVXSOa3SPiE=; b=Rwh+dRSixCLcN2a5PJ5zEl7WJonyp+yh6CU8NJwGLz1L6ME+ye/cOMNANo2OsjyXFZ PpxeILG0/uKVA48h6jj6QsaMYE4ePjBG3oWAT4GLb2Hgr3Wu7PCOt2SlW2TSRt6VKxAI O7x83TcTLgkHDcC+vG39jJR1OXOPWMCCh9T4nSysnlkVOGY+dSqTBHyUZSlI0TEaGDp9 8+GeK9fySP/jN4WUq8ASCWCEHZCOXkKcOd7h3RsUpQ4TLE2Cc2Ddj7hJGZwetDvaKdvN nbTvU2PUqDXEdcryDqWag33PvOyRlsyikHvvKwicZ5SuclxMQKXRI4lb8I76Qk++IRuX iG/A== X-Gm-Message-State: AOJu0YwoIOxdFFgRzZuztNdwEJKvN7WWWzTNuitzckfsHsKra4AFFWu1 hg/mcKaY8YO2gbzPyZmmG3PCMh0IJ9HQrYcH0+Ih+GQULNi0ubAAdSJBHtoF08zd72WKyyG48f0 UcJ+QDLACOzdQ0BA2Mshut7+t2X6bwZY= X-Gm-Gg: ASbGnctef1qRUrYaWY+XDzKHFqP8TMLdeBCwXkATncSDqVsQGxOVtqHozv8vRdPMJ4+ tcj7d+39fu5QPjGm/ggWIS5xh6uCgz6NodLgkp3KMjkiI0j7RHtxxxjyatQBPkXnnnVB3XIPnUJ wEw9lur2PNEZp+LpX6fuQS7nFswYL6cmeFZ0b3vt7NGUA/66TeL1sW50t8mmM2bAymjRcfHSgXD QeB2ltcYyTG1ttcdWD+cYEny6P/F0ZkYhpskOUQoXc17sPPWIiZh250s+iPm0uZBrh0Y+nXtT33 9u0vNTuYLRiS7a9lt3pL/7c59GuWiu9c/TQZYyEHo4yFJPdqZziRwtw9YPQN5vSlM5hNv16qfbb wT5pB X-Google-Smtp-Source: AGHT+IH1MHy5Qk51F3Y1FCmGI7V+ZPXVAMCKtx9ugvuX7AhT/BczVdLqmW8ygwzJ6yZM+FMH31ELCQfc+rqSlu4S760= X-Received: by 2002:a05:622a:120b:b0:4eb:a8f4:270d with SMTP id d75a77b69052e-4ed15c2f965mr27969611cf.65.1761737386289; Wed, 29 Oct 2025 04:29:46 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: John Naylor Date: Wed, 29 Oct 2025 18:29:35 +0700 X-Gm-Features: AWmQ_bmmaF4-pDsGgRYL6Fcwo4sk05MZOHvlBdaCzRceSfULpT_JbynjvW-O2H8 Message-ID: Subject: Re: tuple radix sort To: Chao Li Cc: PostgreSQL Hackers , Peter Geoghegan 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 On Wed, Oct 29, 2025 at 3:25=E2=80=AFPM Chao Li wr= ote: > > On Oct 29, 2025, at 14:28, John Naylor wrote: > > > > I suspect the challenge > > will be multikey sorts when the first key has low cardinality. > > As you predicted, when the first key has very low cardinality, radix is a= little bit slower. I built a test that proves that: > > ``` > evantest=3D# drop table if exists test_multi; > evantest=3D# create unlogged table test_multi (category int, name text); > =E2=80=94 first column has only 4 distinct values Thanks for testing. Note it's actually 5 because of rounding. Your text also seems to have em-dashes and unicode apostrophes where it should have dashes / single quotes. That's not great if you expect others to try to reproduce. I'm also not thrilled about having to remove your psql prompt. drop table if exists test_multi; create unlogged table test_multi (category int, name text); insert into test_multi select (random() * 4)::int as category, md5(random()::text) || md5(random()::text) as name from generate_series(1, 1000000); vacuum freeze test_multi; Anyway, because this table is larger than my first example, the input no longer fits into 64MB of work_mem and it switches to an external merge sort. Normally I set work_mem to 1GB for testing sorts so I don't have to think about it, but neglected to in my first email. I don't know if that explains the disparity, but I've made that change for my quick tests below. > evantest=3D# \o /dev/null > evantest=3D# select * from test_multi order by category, name; [...] > Roughly 5% slower for this corner case. Seems fine for me on this old Intel laptop (min of 5 runs): set wip_radix_sort =3D 'off'; 2368.369 set wip_radix_sort =3D 'on'; 2290.654 It's close enough that I'll want to test more closely at a range of low-cardinality inputs. I haven't done any rigorous scripted testing yet, so take this with a grain of salt. > However, when I recreate the test table with high cardinality first colum= n, wip_radix_sort seems still slower: drop table if exists test_multi; create unlogged table test_multi (category int, name text); insert into test_multi select (random() * 1000000)::int as category, md5(random()::text) || md5(random()::text) as name from generate_series(1, 1000000); vacuum freeze test_multi; > evantest=3D# set wip_radix_sort =3D 'off'; > Time: 0.904 ms > evantest=3D# select * from test_multi order by category, name; > Time: 593.578 ms > evantest=3D# select * from test_multi order by category, name; > Time: 597.329 ms > evantest=3D# select * from test_multi order by category, name; > Time: 600.050 ms > > evantest=3D# set wip_radix_sort =3D 'on'; > Time: 0.737 ms > evantest=3D# select * from test_multi order by category, name; > Time: 611.604 ms > evantest=3D# select * from test_multi order by category, name; > Time: 613.115 ms > evantest=3D# select * from test_multi order by category, name; > Time: 615.003 ms > ``` > > This seems like a real regression. It's better for me here (min of 5 again), although the time scanning the table probably dominates: off: 536.257 on: 471.345 -- John Naylor Amazon Web Services