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 1vEHuF-0092rL-DI for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Oct 2025 01:56: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 1vEHuC-0058xC-Tw for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Oct 2025 01:56:47 +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.94.2) (envelope-from ) id 1vEHuC-0058x4-J3 for pgsql-hackers@lists.postgresql.org; Thu, 30 Oct 2025 01:56:47 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vEHu9-004z8x-0v for pgsql-hackers@lists.postgresql.org; Thu, 30 Oct 2025 01:56:47 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-273a0aeed57so19517725ad.1 for ; Wed, 29 Oct 2025 18:56:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761789402; x=1762394202; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=44Mr9/zyqW2vB+r6R2zmrXNqzlekiPI5S3Eo4wZ3PKw=; b=Er5iixTGhNawBj0COPy+TU+BEWvMGWtSUB/D00FsuP+8kkEEAHeHc+C3H+83S8FTXd lekxurpZ4XlQaydHLbGw7O5FvLwLxIi7lZ/4A8ChVnYMfcf8xQ9VQAu8hBAN0Dk6f6YC qWTGkFM97coo3c9hZLSlAa/qALG4U/CwcSYbvyudnYMc549qD/bvi82CvTj10eL7+8jR EqFaKDENURxeaM1SpGfT5kaIZ6mAPjHyDw2gOsm/HZVty+Cq7CcuuAi0z0QKaEQFp24S 6tWWwQHSzC+b065K2lt6r5dA0afiecNEXf0Qbua71VHziSM7H/O00zKDrjVFlH0MdBts Vo8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761789402; x=1762394202; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=44Mr9/zyqW2vB+r6R2zmrXNqzlekiPI5S3Eo4wZ3PKw=; b=VXVWLniO2gW3inYv9HH7Yn/ddGbonF8zM904Ji8kDeu2Zi/eLnRLKGJN0RPUCRaQBg fB3FyRQ/6VzQKKorFQG1v6jtlmHFbm+yu915nbjP/Ty/mNvx9wBJqzPzVIY/cFohmTuY iVpLpwDysG/GdwB1ds1K/+b9jzk5r249Ft3vxo8E6fH9bqr7wzy6o/UMWFYo8oWFAmYd B/vqvcS9RmSoPbwrNLwMW5IyYr/53kAHYG3JoIIvw2Jz7t6/+7AhWL1en3bMUTspFA/o iNj7bFqoQ6NH+gLHwux4iFwMnRsxZEYklnQC9eQKYeuUiulST8pvJSulQ9u+jExh/TBH ZBQQ== X-Gm-Message-State: AOJu0YzoCSLeIc8PTLK2J4QgWEX5NmRITsqafBV2yzwP2LbnS1hUQvpu x72MYS2qH7+NZdZTSbr2Pq3/5GDLYwqHem/fB5LTRvlMji7hKw/sxptw X-Gm-Gg: ASbGncvdWVEayIA95QR2NPC6xFbKXKOsuUJFykQqJGl/5egjSb2Fz0hr7K6KUQGqNeT vEBR+Dxu8eO3P4siPcQa94G0/193nfIHBJvpjEsS1H6xrvKx6yzTzN/c+YnE3OmCugT3uZoDxNC cDKl3t4Zaia+CRS5vNPtJ1K539fT9omWWi0yDNieg7Js3BmUFqqeLc8yRVStuFoLNl/AQ0WVrKw Da2WsJ9D2UU98BbkhKKozHrO8icLeWUVav71pVodX/OHp8I+9QmQgC+6bE6jN+eqjjUi6riWJEC 2afutLB1dzqzHxUCoOez1uiA8dAs62j2XJtu4hQGkME0N0/XC7AhdAQ0KI30tM5mUzC5dI7R7Rz 3HfyAsaAwAJOga0p1M18odrbaPiqV4U1ac9EeITyP5HSQbX84X0aNX0Y2XB9YbWJkTc8+XarOSw AxJxWtAhcoOQ== X-Google-Smtp-Source: AGHT+IHdEG2CX1JGC1saq0hCn1GsCu8yl0kMsrj5T/12JfP+OjmCFT1xi25pKNHAxg44CLgLPhuYOQ== X-Received: by 2002:a17:902:ea10:b0:294:fbe5:89ef with SMTP id d9443c01a7336-294fbe59666mr4509245ad.28.1761789402039; Wed, 29 Oct 2025 18:56:42 -0700 (PDT) Received: from smtpclient.apple ([196.247.24.158]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29498d09a33sm164908415ad.32.2025.10.29.18.56.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Oct 2025 18:56:41 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: tuple radix sort From: Chao Li In-Reply-To: Date: Thu, 30 Oct 2025 09:56:06 +0800 Cc: PostgreSQL Hackers , Peter Geoghegan Content-Transfer-Encoding: quoted-printable Message-Id: <72B9451B-0121-491E-A702-BE8AAEFDB80E@gmail.com> References: To: John Naylor X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Oct 29, 2025, at 19:29, John Naylor = wrote: >=20 > On Wed, Oct 29, 2025 at 3:25=E2=80=AFPM Chao Li = wrote: >>> On Oct 29, 2025, at 14:28, John Naylor = wrote: >>>=20 >>> I suspect the challenge >>> will be multikey sorts when the first key has low cardinality. >>=20 >> As you predicted, when the first key has very low cardinality, radix = is a little bit slower. I built a test that proves that: >>=20 >> ``` >> 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 >=20 > Thanks for testing. Note it's actually 5 because of rounding. Yes, 0-4, totally 5. > 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 just copied the content from psql (running in iTerm). I did a Google = search, and found that was because of Mac Mail=E2=80=99s =E2=80=9Csmart = quotes=E2=80=9D substitution. Looks like even I manually type in a pair = of single quotes, it still does the substitution. I will try to see how = to disable that, but I don=E2=80=99t want to switch to another mail app. > I'm also not thrilled about having to > remove your psql prompt. >=20 I just wanted to show my entire test process, so I simply copied all = contents from psql. In future, I will remove psql prompts from reproduce = procedure. > 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; >=20 > 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.=20 I changed work_men to 1GB and reran the test. As the high cardinality = data are still there, so I first reran with data: ``` evantest=3D# set work_mem =3D '1GB'; Time: 0.301 ms evantest=3D# evantest=3D# select * from test_multi order by category, name; Time: 575.247 ms evantest=3D# select * from test_multi order by category, name; Time: 554.351 ms evantest=3D# select * from test_multi order by category, name; Time: 565.100 ms evantest=3D# evantest=3D# set wip_radix_sort =3D 'on'; Time: 0.752 ms evantest=3D# select * from test_multi order by category, name; Time: 558.057 ms evantest=3D# select * from test_multi order by category, name; Time: 565.542 ms evantest=3D# select * from test_multi order by category, name; Time: 559.973 ms ``` With radix_sort on and off, execution time are almost the same. Then I restore the data to low cardinality, off is still faster than on: ``` evantest=3D# set wip_radix_sort =3D =E2=80=98off'; Time: 0.549 ms evantest=3D# select * from test_multi order by category, name; Time: 5509.075 ms (00:05.509) evantest=3D# select * from test_multi order by category, name; Time: 5553.566 ms (00:05.554) evantest=3D# select * from test_multi order by category, name; Time: 5598.595 ms (00:05.599) evantest=3D# set wip_radix_sort =3D =E2=80=98on'; Time: 0.786 ms evantest=3D# evantest=3D# select * from test_multi order by category, name; Time: 5770.964 ms (00:05.771) evantest=3D# select * from test_multi order by category, name; Time: 5779.755 ms (00:05.780) evantest=3D# select * from test_multi order by category, name; Time: 5851.134 ms (00:05.851) evantest=3D# evantest=3D# set work_mem =3D '2GB=E2=80=99; # increasing work_mem to = 2GB doesn=E2=80=99t help Time: 0.404 ms evantest=3D# evantest=3D# select * from test_multi order by category, name; Time: 5781.005 ms (00:05.781) evantest=3D# select * from test_multi order by category, name; Time: 5826.025 ms (00:05.826) evantest=3D# select * from test_multi order by category, name; Time: 5937.919 ms (00:05.938) ``` Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/