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 1rnUJZ-00DXJW-4w for pgsql-hackers@arkaria.postgresql.org; Fri, 22 Mar 2024 02:07:25 +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 1rnUJX-0007td-EM for pgsql-hackers@arkaria.postgresql.org; Fri, 22 Mar 2024 02:07:23 +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 1rnUJX-0007tV-59 for pgsql-hackers@lists.postgresql.org; Fri, 22 Mar 2024 02:07:23 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rnUJU-005ls5-0e for pgsql-hackers@lists.postgresql.org; Fri, 22 Mar 2024 02:07:22 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 42M27JVW813834; Thu, 21 Mar 2024 22:07:19 -0400 From: Tom Lane To: Nathan Bossart cc: pgsql-hackers@lists.postgresql.org, alex work , Robert Haas Subject: Re: Slow GRANT ROLE on PostgreSQL 16 with thousands of ROLEs In-reply-to: <20240322015251.GA2193900@nathanxps13> References: <341186.1711037256@sss.pgh.pa.us> <619340.1711050134@sss.pgh.pa.us> <642442.1711053105@sss.pgh.pa.us> <20240321204012.GA1992418@nathanxps13> <20240322005139.GA2060816@nathanxps13> <806361.1711069194@sss.pgh.pa.us> <20240322010332.GA2109205@nathanxps13> <20240322015251.GA2193900@nathanxps13> Comments: In-reply-to Nathan Bossart message dated "Thu, 21 Mar 2024 20:52:51 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <813832.1711073239.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 21 Mar 2024 22:07:19 -0400 Message-ID: <813833.1711073239@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Nathan Bossart writes: > The Bloom filter appears to help a bit, although it regresses the > create-roles.sql portion of the test. I'm assuming that's thanks to all > the extra pallocs and pfrees, which are probably avoidable if we store t= he > filter in a long-lived context and just clear it at the beginning of eac= h > call to roles_is_member_of(). The zero-fill to reinitialize the filter probably costs a good deal all by itself, considering we're talking about at least a megabyte. Maybe a better idea is to not enable the filter till we're dealing with at least 1000 or so entries in roles_list, though obviously that will complicate the logic. + if (bloom_lacks_element(bf, (unsigned char *) &otherid, sizeo= f(Oid))) + roles_list =3D lappend_oid(roles_list, otherid); + else + roles_list =3D list_append_unique_oid(roles_list, otherid= ); + bloom_add_element(bf, (unsigned char *) &otherid, sizeof(Oid)= ); Hmm, I was imagining something more like if (bloom_lacks_element(bf, (unsigned char *) &otherid, sizeof(Oid= )) || !list_member_oid(roles_list, otherid)) { roles_list =3D lappend_oid(roles_list, otherid); bloom_add_element(bf, (unsigned char *) &otherid, sizeof(Oid))= ; } to avoid duplicate bloom_add_element calls. Probably wouldn't move the needle much in this specific test case, but this formulation would simplify letting the filter kick in later. Very roughly, if ((bf && bloom_lacks_element(bf, (unsigned char *) &otherid, siz= eof(Oid))) || !list_member_oid(roles_list, otherid)) { if (bf =3D=3D NULL && list_length(roles_list) > 1000) { ... create bf and populate with existing list entries } roles_list =3D lappend_oid(roles_list, otherid); if (bf) bloom_add_element(bf, (unsigned char *) &otherid, sizeof(O= id)); } regards, tom lane