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 1v6g9f-0085Ex-A5 for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Oct 2025 02:13:19 +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 1v6g9c-00EgB5-RY for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Oct 2025 02:13:17 +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 1v6g9c-00EgAx-98 for pgsql-hackers@lists.postgresql.org; Thu, 09 Oct 2025 02:13:17 +0000 Received: from fhigh-b8-smtp.messagingengine.com ([202.12.124.159]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v6g9a-0018xI-1a for pgsql-hackers@lists.postgresql.org; Thu, 09 Oct 2025 02:13:16 +0000 Received: from phl-compute-12.internal (phl-compute-12.internal [10.202.2.52]) by mailfhigh.stl.internal (Postfix) with ESMTP id 67F947A010E; Wed, 8 Oct 2025 22:13:11 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Wed, 08 Oct 2025 22:13:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1759975991; x=1760062391; bh=E+q+lkJ7/y 1BWLlFbsKZMqFjfyCQYEkYMu3D8Mp5N7c=; b=pSLepCzWGRqtiHglfrFB2pi8W0 JUOZW6OEKY1+zO28/z3Stu9kOfsQ0bOmQxROrsky50qdisrE9zKOGU1VYbhPtSzQ J3fBjtNKJrz4mZh0p1nsvqIsCRz/KQM1jQH1VxsMIjUJSnQ7xf6S/SHOJFugRXP5 dVcMp9DLB9hlPbVWy4Fqlmwa3Tv4Fx3R8yptvJ8rKhxyUAgBLe07OvsXoNUP30MB M11T44AfAkmI6uDO1r7vSdjO0Y9aY5BxRg5etNIAX5otrhFD0n7AgRyqDtl/x0+g NioD0cWIxIrP4hf1R4Yfj+OJyrqxrvSnu8XR382x+F3bud9Y+uRfDCE9bFvg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1759975991; x=1760062391; bh=E+q+lkJ7/y1BWLlFbsKZMqFjfyCQYEkYMu3 D8Mp5N7c=; b=vTLMX/tjGWjLim4zTzdMQ5klM/JbVw1jfsdXFpaTM0yOPVWxeCH fWLZTn8WK2B6RIbszwZjuJeteMqfGcrDf/RMRKv7ggoN593cDTjO1y/FwV/8HFKI 6Ch89kICu+e+LwBbjbNzUG6gSqTUT6BtZ6kdZBwXZ119MQIPsxyoR56kK2YT5wqS c6HFbDeIsx+UYRIwO+16/+eM137Tgphzdv/t2qE1q2ZBWsA7N5oyBE/qmVE8mSL3 Z5tgWeM09ir9sd1b/59S5Ll3f7ozaFM7nfRb4qCa0tGdu2K23RxdnTVXt8+vx28a anVJIdEO/6VGmnB3CDNuooHB3YSfX9fLNdg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddutdegleehucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnegfrh hlucfvnfffucdljedtmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddv necuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesphgrqh huihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhepteelieefudffhffhtdetleeggeeg fffhkeeuveetiefgudduvedutefggeeivdejnecuvehluhhsthgvrhfuihiivgeptdenuc frrghrrghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiihiidp nhgspghrtghpthhtohephedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepughgrh hofihlvgihmhhlsehgmhgrihhlrdgtohhmpdhrtghpthhtohepghhrvghgsegsuhhrugdr mhgvpdhrtghpthhtoheprhgrnhhivghrrdhvfhesghhmrghilhdrtghomhdprhgtphhtth hopegurghnihgvlheshigvshhqlhdrshgvpdhrtghpthhtohepphhgshhqlhdqhhgrtghk vghrsheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 8 Oct 2025 22:13:08 -0400 (EDT) Date: Thu, 9 Oct 2025 11:12:50 +0900 From: Michael Paquier To: David Rowley Cc: Greg Burd , Ranier Vilela , Daniel Gustafsson , PostgreSQL Hackers Subject: Re: [PATCH] Add tests for Bitmapset Message-ID: References: <2D90FFB0-C80A-4189-A5BF-C37F05E271D7@greg.burd.me> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="NCxJX7g75OVnq8Gg" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --NCxJX7g75OVnq8Gg Content-Type: multipart/mixed; boundary="edRvHQr1T2/lWe2i" Content-Disposition: inline --edRvHQr1T2/lWe2i Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 09, 2025 at 02:57:41PM +1300, David Rowley wrote: > I don't think there's any need to maintain the order of that members > array, so couldn't you just do this?: >=20 > bms =3D bms_del_member(bms, member); > members[pos] =3D members[--num_members]; Yep, I was just playing with all that and moving the last element to the member we know is gone would be cheaper. I have also added more comments to document everything in a more precise way, while going through. I also do not see a point in preventing inserts in the second set at the beginning of the function. This forces bms_union() to do more operations with overlapping sets. What do you think about the attached? -- Michael --edRvHQr1T2/lWe2i Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-test_bitmapset-Improve-random-function.patch" Content-Transfer-Encoding: quoted-printable =46rom 03c6ee49b133e9f77dbd07e1df0a4e0aece511d7 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Thu, 9 Oct 2025 11:11:51 +0900 Subject: [PATCH v2] test_bitmapset: Improve random function --- .../modules/test_bitmapset/test_bitmapset.c | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/test/modules/test_bitmapset/test_bitmapset.c b/src/test/mo= dules/test_bitmapset/test_bitmapset.c index 8bc9b1f48e9a..ce2034b43290 100644 --- a/src/test/modules/test_bitmapset/test_bitmapset.c +++ b/src/test/modules/test_bitmapset/test_bitmapset.c @@ -618,23 +618,23 @@ test_random_operations(PG_FUNCTION_ARGS) pg_prng_seed(&state, seed); members =3D palloc(sizeof(int) * num_ops); =20 - /* Phase 1: Random insertions */ + /* Phase 1: Random insertions in first set */ for (int i =3D 0; i < num_ops / 2; i++) { member =3D pg_prng_uint32(&state) % max_range + min_value; =20 if (!bms_is_member(member, bms1)) - { members[num_members++] =3D member; - bms1 =3D bms_add_member(bms1, member); - } + bms1 =3D bms_add_member(bms1, member); } =20 - /* Phase 2: Random set operations */ + /* Phase 2: Random insertions in second set */ for (int i =3D 0; i < num_ops / 4; i++) { member =3D pg_prng_uint32(&state) % max_range + min_value; =20 + if (!bms_is_member(member, bms2)) + members[num_members++] =3D member; bms2 =3D bms_add_member(bms2, member); } =20 @@ -642,7 +642,7 @@ test_random_operations(PG_FUNCTION_ARGS) result =3D bms_union(bms1, bms2); EXPECT_NOT_NULL(result); =20 - /* Verify union contains all members from first set */ + /* Verify union contains all members from first and second sets */ for (int i =3D 0; i < num_members; i++) { if (!bms_is_member(members[i], result)) @@ -650,7 +650,10 @@ test_random_operations(PG_FUNCTION_ARGS) } bms_free(result); =20 - /* Test intersection */ + /* + * Test intersection, checking that all the members in the result are from + * both the first and second sets. + */ result =3D bms_intersect(bms1, bms2); if (result !=3D NULL) { @@ -683,24 +686,44 @@ test_random_operations(PG_FUNCTION_ARGS) bms_free(bms1); bms_free(bms2); =20 - for (int i =3D 0; i < num_ops; i++) + /* + * Phase 4: mix of operations on a single set, cross-checking a bitmap + * with a secondary state, "members". There can be up to "num_ops" + * members added, very unlikely still possible if all the operations hit + * the "0" case. + */ + num_members =3D 0; + members =3D palloc(sizeof(int) * num_ops); + + for (int op =3D 0; op < num_ops; op++) { - member =3D pg_prng_uint32(&state) % max_range + min_value; switch (pg_prng_uint32(&state) % 3) { case 0: /* add */ + member =3D pg_prng_uint32(&state) % max_range + min_value; + if (!bms_is_member(member, bms)) + members[num_members++] =3D member; bms =3D bms_add_member(bms, member); break; case 1: /* delete */ - if (bms !=3D NULL) + if (num_members > 0) { + int pos =3D pg_prng_uint32(&state) % num_members; + + member =3D members[pos]; + if (!bms_is_member(member, bms)) + elog(ERROR, "expected %d to be a valid member", member); + bms =3D bms_del_member(bms, member); + members[pos] =3D members[--num_members]; } break; case 2: /* test membership */ - if (bms !=3D NULL) + /* Verify that bitmap contains all members */ + for (int i =3D 0; i < num_members; i++) { - bms_is_member(member, bms); + if (!bms_is_member(members[i], bms)) + elog(ERROR, "missing member %d", members[i]); } break; } @@ -708,6 +731,7 @@ test_random_operations(PG_FUNCTION_ARGS) } =20 bms_free(bms); + pfree(members); =20 PG_RETURN_INT32(total_ops); } --=20 2.51.0 --edRvHQr1T2/lWe2i-- --NCxJX7g75OVnq8Gg Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmjnGiIACgkQnvQgOdby QH3fARAAimK3+s9E6+BKR9GMgY3oEk/JkmV+52PvFsalK4tbX7pLirhQS0095RPq ipGpqS8Nf466PcbhDiJwRmOw+MM4MCXFiXn1m0Or1RL7PNVHcFcu249D1kHS0sO0 u7Eo8r0emIFMRvKb1vwAp3gmOlqlsgnpNsGz925U87kyAfdwak2/C3sXHc/1NQtn vA24BT7ItLoGKYaJDXXGTaeQXKFNJbEuEtn3Bvsw1EqjBY0fIc+zdwR5OK8gdXmC EdrEgeIZz/DDm2JlDiCwPAORmKXDXIga0OJZcb13tADP9g3+LKu7ubXO82SA2UID 70wVsRtNWTGO8JONRJ5Rnzx53fpIiUZnIvLu+fPFQTX56OweeJCMEHwp8Ei//ATR EYVPWrffx0iDWhMoLurgu08U5/HcRVTbELTr8jLszlS2I9TClbyFrMerMcbq2qQ0 gHCPPZhmj+yacSHBbhIxTPIyuPSFkUSvOix2t+K8tFVjYgcspwQJnRS9Wjb+AcPe bh2xzQd3RMj1Fsdf1Br8A4HwQLYrh0zEmynWUPIHXow08Ew5Y3QYj8p7UVg2W5pF IolxBpSBL2Q03zBnmZSiGWoTEh/s6mBQcbX6/+ENIZKljaQFmaK8DLwMUGA/dcvh 1/UE4NnFFF1Jms5g2HvCTrwFk8mJahh7eKZzmBzbNZVG7DArqAY= =xjvv -----END PGP SIGNATURE----- --NCxJX7g75OVnq8Gg--