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 1v6hdE-008POf-D9 for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Oct 2025 03:47:56 +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 1v6hdB-00FPEe-Qh for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Oct 2025 03:47:54 +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 1v6hdB-00FPES-DX for pgsql-hackers@lists.postgresql.org; Thu, 09 Oct 2025 03:47:54 +0000 Received: from fhigh-b1-smtp.messagingengine.com ([202.12.124.152]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1v6hd9-000mUd-1H for pgsql-hackers@lists.postgresql.org; Thu, 09 Oct 2025 03:47:53 +0000 Received: from phl-compute-09.internal (phl-compute-09.internal [10.202.2.49]) by mailfhigh.stl.internal (Postfix) with ESMTP id A3EDB7A00C8; Wed, 8 Oct 2025 23:47:51 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Wed, 08 Oct 2025 23:47:51 -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=1759981671; x=1760068071; bh=pS0y6qpsAt oy7Uky5uaYYzTbwiySRGU5JJMjpX6U0bY=; b=NcEzlhE76ix4JltxhuvYf7QRKB iNXj5bP9HCY1XAyEMLUeVX2GHXfcIuSJZwNYBdgwk4VazXHYDhDg7cTQjQhGy/69 ztQk3EWfOazGkeJ5+pejT3ZQfeOB7f9i5vtCDMlMq9cEflKGKBDpAJAdJ88IND4F 66h1du8iBJoN/rYvFkkORHzNWRj1miZh/4nIQyhjpOKGbn0PcYbDA2SmchV2U9kh ybQZaxNTkUHUz3O7AX22WABDn1HFUZdq+/s/u+Ld74bZHyP80iYkFv4BXQoEBh4B s8RY0sz8lHvJ9EzeD7aatttIvNTd7uDrs9F9NCF7Vw3l7QW1yKIzqx4dZIfw== 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= 1759981671; x=1760068071; bh=pS0y6qpsAtoy7Uky5uaYYzTbwiySRGU5JJM jpX6U0bY=; b=qWnVsDt8lQnAOj2pd3oSPf9vHUUdrU3tcCKzZLOdYjXCNi8TUlq TOoAWWMq2C+XKsbhdrvKzeD/F8CInfgitSR9qs1cRsMxeKsDFavPd0tSwsF5ODGm h3Moc4dRj46XyDGZlTZTmKKyziiaB6x1BJIrcXlQDn47h30+6Vbeerp0hW8hBuHX vmyVtVmmmInGvc5ZizsRXRwtwDfl9G9TSFptfH8ptu2EUwbe67WAeuSCtRsmCU7i Xut9TQVrlz5oQDEm1u35gXdyEWx4jfCW4fDRRIxH5QUh7KyCufXF8pgjngs/9HF9 wXORf+UDjNFi96b5vatJ8FSywotgZkbtuBg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddutdehudefucetufdoteggodetrf 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 23:47:49 -0400 (EDT) Date: Thu, 9 Oct 2025 12:47:31 +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="9tMm0UIKUEFKV99t" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --9tMm0UIKUEFKV99t Content-Type: multipart/mixed; boundary="2UxbqkDRN+/uQwJo" Content-Disposition: inline --2UxbqkDRN+/uQwJo Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 09, 2025 at 04:35:55PM +1300, David Rowley wrote: > On Thu, 9 Oct 2025 at 15:13, Michael Paquier wrote: > > What do you think about the attached? >=20 > Thanks. Looks pretty good. >=20 > > + members =3D palloc(sizeof(int) * num_ops); >=20 > Any reason to pfree that and allocate that to the same size as it already= was? No reason. We can shortcut that a bit. > Wondering if the "members[pos] =3D members[--num_members];" is worth a > short comment. Maybe something like: /* zap this member by moving the > final array member into its place and shrinking the array by 1 */ Yes, a comment can be adapted here. Sounds good to me. -- Michael --2UxbqkDRN+/uQwJo Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-test_bitmapset-Improve-random-function.patch" Content-Transfer-Encoding: quoted-printable =46rom d7ba7df53ad8d1c45dadb7e01c47b4c30f5bfb75 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Thu, 9 Oct 2025 12:46:59 +0900 Subject: [PATCH v3] test_bitmapset: Improve random function --- .../modules/test_bitmapset/test_bitmapset.c | 57 ++++++++++++++----- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/src/test/modules/test_bitmapset/test_bitmapset.c b/src/test/mo= dules/test_bitmapset/test_bitmapset.c index 8bc9b1f48e9a..abbdde85e0b3 100644 --- a/src/test/modules/test_bitmapset/test_bitmapset.c +++ b/src/test/modules/test_bitmapset/test_bitmapset.c @@ -616,25 +616,31 @@ test_random_operations(PG_FUNCTION_ARGS) min_value =3D PG_GETARG_INT32(3); =20 pg_prng_seed(&state, seed); + + /* + * There can be up to "num_ops" members added. This is very unlikely + * still possible if all the operations hit the "0" case during phase 4 + * where multiple operation types are mixed together. + */ 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 +648,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 +656,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) { @@ -679,28 +688,49 @@ test_random_operations(PG_FUNCTION_ARGS) bms_free(result); } =20 - pfree(members); 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". + */ + num_members =3D 0; + + 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); + + /* + * Move the final array member at the position of the member + * just deleted, reducing the array size by one. + */ + 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 +738,7 @@ test_random_operations(PG_FUNCTION_ARGS) } =20 bms_free(bms); + pfree(members); =20 PG_RETURN_INT32(total_ops); } --=20 2.51.0 --2UxbqkDRN+/uQwJo-- --9tMm0UIKUEFKV99t Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmjnMFMACgkQnvQgOdby QH0XAA//bAUJwveaA7BR+a84IDJQF+bk81kra2/S7ydzQyJFc7Gev4bQKngV0a4o B6PYKp4raTH6fijKWTISsV8jLswjT9y2Vz2eDZHVEIorPtgXjJ0XL1XLv6iH3N5q /PFcMzZzgBhDDanpWdJTU7aUOcN1GdhWgIYeLrumpkK2UvyGlL2r13/R80aRK5lM 2t/iX1QjdjbeimCOkjqAOLAAag66yAf2sHKLEvS0LHWaHwDNEJGA6BXk+dpDR5S6 TEONZHvfz2dPkf7gqODf5ib14/0BONmtiQlJvRw5iCSbF/wj+rJMGUo8EKEPLnoi anw3QJ0Yz1chBgmTVKKtvNaGJzd6XJ/MEEnqn3XHealVcAc1C7uQtZDYf/t8sDSc omWJ/UYLe+PC6YuGDj7BqQOKXWQCY/kJMNS1Z0MlcNmx+z26uxf2hQnKo2mxiywk pnHbaszpz7hO+vOrSg773IFue+9mvyU+ULsh3/6Pqjo4l01dt0cW34e7jiKRlCra YAtVXRqxqv/6VjHF/Os95EGnu2/r/eVgU7Xj4muCboNlkXbwamN4b86bmDuY0mFU nsBp7OghURtxmix83zGor3fxeFDLo6XDizDv7UippaU/TtEngvpkEo8gBI/acnTj GFfRFkUtKS5yw1g5VsgYYs8uYTcYtf46xa6MLNig7cIaQ4KCcjw= =b++7 -----END PGP SIGNATURE----- --9tMm0UIKUEFKV99t--