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 1v3kNg-002ssC-Ex for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Oct 2025 00:07:40 +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 1v3kNc-00DgRM-6S for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Oct 2025 00:07:36 +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 1v3kNb-00DgRE-NN for pgsql-hackers@lists.postgresql.org; Wed, 01 Oct 2025 00:07:36 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v3kNR-000mS6-1c for pgsql-hackers@lists.postgresql.org; Wed, 01 Oct 2025 00:07:35 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-5821dec0408so5741935e87.1 for ; Tue, 30 Sep 2025 17:07:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759277243; x=1759882043; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=DIj4zfQWJSAJYdL7CGOEHoinlyBsb057CgjAxCQGcHw=; b=NXB5dQLqesmBxFG+5IYlw8Olb+FCtUkMH93xNl7n0knZXZzpx9Z2p5yoTA4/BE5YzC 7sCNwx9O89RaCThw8nL57vwimZRT/AN0HZdLWnRudAyo6mRuAH3FNPF5j4tbgH9yXwh7 hNVb85iUlvTxAzioYhsG2uthuuvM9kJK4KNLOx08e1bDcPfdVZzLAx9NbxkqdBqaVfzp nC7sFEqkvELkPDGB80Co2O6ysKyav6ZTl7gaVIsl2bqQx4puZbe7shqzjASRUyOeZoRO UsMJrmi1UiNJkQaw+lZyC+5dAT1TsB3cVlcURC/yZKXRAd/gITEZ+8lLP1mVZanDdPns CH6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759277243; x=1759882043; h=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=DIj4zfQWJSAJYdL7CGOEHoinlyBsb057CgjAxCQGcHw=; b=AKw+Rgdh+HGE1ep08MoMAYIQlt/HYiBrxNbEGQppxL6qKmRGzMMV6xOQZjjkYSeQuh KtLJir8nkmSPbEqZ4mG8ae8y5epIy8qG5Geh4PIP1gAA4tJjUrFIeDxtSIaSCTMvzfqI wEDnlu5aKG7QJ5HBJdXOo3/GK6mUbHxDpZgxsoxciy4ReYCUJckT5DAm3vAvsOizR8VN IYmbtttH79FfLOfddT2Ae+p9gNqipjlT3XdROTkXXzXMDTLDOJMxQZcVMJOdYYa/1WoW Q4HEPktLnQbXj4Uj2mVTpEDIYRq+VhYMP1uesUQ2OjpLnTgg941zS09bRhoTj9u8ikIa n0QA== X-Forwarded-Encrypted: i=1; AJvYcCVdgm/lHHHZDoHwhjo2DUvZR2mKzZ1YBoVeihdHzBZtIc3eJ2MnO+SlHr2U6T84o2ww+HDpAi1NeFYU7PXn@lists.postgresql.org X-Gm-Message-State: AOJu0Yx4/Mbp6t+C0tNJ3sffiob3QQeFmfcFi/mZSiwsMMRnyh/TJ4uP cMMgpg3X8nKs3j6XZ8J13lGZaTAJTuip7XtIN09FLn0cz8OdlSWNFqzqjOuL3vSkUz5XM3uBZ+k fV55BcYcLhVy83Wzd7AkAj4Nk5YNbG8Q= X-Gm-Gg: ASbGncuWkNtJzjnTR4sQc/E0RjjTcyp/yNnkFNQbk3VsBn0UYQ6RxuB0IhIz5uiHknb w5ervcF7mP2fd0JYEY1iI67To9sy3qmytqtf5Rgdywuz4GlY5VhhcNBQbRuH/0PpXgDkp83t4FC VzRhCUcoBPxZibNJXrp3+Vzvpq+3LbXdvd/Z5vBEbjEEuWsqzHrOG4Me5RVWBhf3Y+t0IEg+yKD Doel4YUf8oDM1GmD1dT4MQ+GP/iexZnXFnVSTEn1yfYQs37UDrsYX15dnTRuYUfmZgNVt81q/3w K8Qco3XUiUe8pCnX5zqdbg== X-Google-Smtp-Source: AGHT+IFGKwYEo5hYKlAZR8DAmvg5ahahXqqDaaNeOK7vrrQO8d0addVsL9B20Unf+xZnKAVmyy0uFnVOvr9h7WWfuAk= X-Received: by 2002:a05:6512:2383:b0:588:d2d5:8cfc with SMTP id 2adb3069b0e04-58af9f5197emr321176e87.47.1759277242727; Tue, 30 Sep 2025 17:07:22 -0700 (PDT) MIME-Version: 1.0 References: <76DBEF5D-EA47-4C30-8D28-97B3B88E2E2D@greg.burd.me> In-Reply-To: From: David Rowley Date: Wed, 1 Oct 2025 13:07:10 +1300 X-Gm-Features: AS18NWBnpHFdCY9USRJPPy8Wb3BSKaEiqhSUMuj_hDyeKC9T8RIisDCqp5Sufhs Message-ID: Subject: Re: [PATCH] Add tests for Bitmapset To: Michael Paquier Cc: Greg Burd , PostgreSQL Hackers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 1 Oct 2025 at 12:37, Michael Paquier wrote: > > On Wed, Oct 01, 2025 at 12:00:39PM +1300, David Rowley wrote: > > 1. Added helper macros PG_ARG_GETBITMAPSET() and > > PG_RETURN_BITMAPSET_AS_TEXT(). These themselves save a few hundred > > lines of code and make the functions easier to read. > > +#define PG_RETURN_BITMAPSET_AS_TEXT(bms) \ > + do { \ > + text *result = BITMAPSET_TO_TEXT(bms); \ > + bms_free(bms); \ > + PG_RETURN_TEXT_P(result); \ > + } while (0); > > This is also something I've considered as embedding in a macro, and > discarded it. The reason was code readability, because it gives to > the reader a quick knowledge of what can be freed once a call to a > specific bitmapset.c routine is done, acting as a kind of reference > template. Here, the free call knowledge gets hidden. test_bms_copy() > becomes more confusing to me. This argument comes down to one's taste > and philosophy on the matter of test modules. These are for me tools > of coverage, but also can work as reference templates when calling an > internal routine because we also document some of its implied > behaviors, like the inputs recycled. This sounds like another argument towards getting rid of the bms_frees from the module. > > 2. Standardised that the test function returns NULL when it receives > > invalid input. e.g if we aim to pass an "int" to the underlying > > Bitmapset function, if someone calls the test function with an SQL > > NULL, just return NULL rather than trying to come up with some logic > > on what we should do about that. > > +static const char *emptyset = "(b)"; > > Not sure that I'm on board with hardcoding this knowledge in the test > module. But well, if you feel strongly about it, I'm not going to put > a fight for it. I'm not a huge fan of that part either. I didn't feel strongly enough to change it when adjusting Greg's v2. For me, I don't see the problem with leaving it up to nodeToString(), which will return "<>" for an empty set. > > 3. I couldn't quite understand the extra parameter to > > test_bms_get_singleton_member() so I ended up removing it and making > > the test function return -1 when the set isn't a singleton. I didn't > > search for discussion about that so I might have missed the specific > > reason it was done that way. > > It is not very far. In short, I am caring about the status returned > by bms_get_singleton_member(), then translated in the output of the > SQL function based on the default given in input: > https://www.postgresql.org/message-id/aNolInqSzt1d1338@paquier.xyz > > Perhaps not the most elegant solution, but simpler than returning a > setof without changing the meaning of the tests. So we could return > the status directly or use the default value from the input, not doing > any of these actions reduces what's reported back at SQL level. Thanks for explaining. Is anything being lost with the -1 return? Since that's not a valid member for a set, -1 can only be returned if bms_get_singleton_member() returned false. > > 4. Wrote some comments to try to communicate the standards defined. > > This aims to offer guidance to callers of these functions and anyone > > hacking on the module itself in the future. > > +/* Helper macro to fetch Text parameters as Bitmapsets. SQL-NULL means empty set */ > +#define PG_ARG_GETBITMAPSET(n) \ > + (PG_ARGISNULL(n) ? NULL : TEXT_TO_BITMAPSET(PG_GETARG_TEXT_PP(n))) > > True that embedding the PG_ARGISNULL() call may be better in a macro, > as passing NULL values to the internal routines is OK. OK good. There's a decent code reduction from this one. > > I also wonder how worthwhile it is to try to free the Bitmapsets. I'd > > have expected these allocations to be in a pretty short-lived memory > > context. I count 44 calls to bms_free(). > > I've wondered about that as well. However, I have concluded to leave > the free calls arounds, for the memory context argument if someone > plays more with this module. I don't mind the removal of the NULL > checks when calling bms_free() to cut lines in the module, though. I > was also seeing an argument with cross-checking the frees after a > REALLOCATE_BITMAPSETS copy, as well, for sanity checks. So I would > rather leave them as they are, not remove them. Again, that may come > up to one's view on the matter and one's philosophy around these > modules. Because you're not happy with embedding the bms_free() in with PG_RETURN_BITMAPSET_AS_TEXT(), why don't we just get rid of all the bms_free's? It's just a test module. If anyone ever comes complaining about memory usage then we can reconsider. I think it's nice to keep these wrappers as thin as possible in terms of the amount of C code. Having conditional logic in them, IMO, risk hiding the true behaviour of the underlying code that's being tested. I know I've just come in here like a bull in a china shop. It's not my intention to imply that anything is wrong with the code. I just put together my thoughts in the hopes that it can be made even better. David