Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q9pbp-00045I-UG for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jun 2023 16:14:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q9pbo-0003gb-RH for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jun 2023 16:14:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q9pbo-0003gS-Cf for pgsql-hackers@lists.postgresql.org; Thu, 15 Jun 2023 16:14:04 +0000 Received: from mail-oi1-x22c.google.com ([2607:f8b0:4864:20::22c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q9pbl-002YtB-0D for pgsql-hackers@lists.postgresql.org; Thu, 15 Jun 2023 16:14:03 +0000 Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-39cd084ea62so3658657b6e.0 for ; Thu, 15 Jun 2023 09:14:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neon-tech.20221208.gappssmtp.com; s=20221208; t=1686845639; x=1689437639; h=in-reply-to:references:message-id:from:to:subject:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ROXJuu4VS/JGpEx7gz/oh38hSclYq1L0/4LHU21UmiQ=; b=VNXaI0BLHF0V2W8/al4aasG4G2SiMVR0imMvoGAcJtVE4ma/JxSFWFhfwjFwSccnBj uPtD/Z29/6hUrlJauhvgA2Ch7R44StOS3N8HE+i84uy4FQ0prxzpX5SPUGN3EH6G7s4+ g7Tt4dzJmeG2xxEzC3UE8EW+gs0jWL3v7K0vGh/SSmxF1QScP0bLNc+w8xuW30QHEOdE AVku/wEU80kQUwlytm/zlRe7GM0ePEDXVt+ecR0wPmHtPWe9Cw6cj7JJrlaEb7alMYTK 8wJcN1aA7Ehg7jjQNokW9xUvRlH6+sJzQX/UvEDCD6VTe2KXLsDrU5HZCIrtcdVLo6Zv efgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686845639; x=1689437639; h=in-reply-to:references:message-id:from:to:subject:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ROXJuu4VS/JGpEx7gz/oh38hSclYq1L0/4LHU21UmiQ=; b=XWv9wI2HT0pQgRdeT8kf4rKj+z4q/a7wfaGSm1Sz20sheIlYtK87Hv4qgZFR2/iNH5 3J/aG1PxnP1RpDNNs3QxKmO0CJVp+IeK8uS3frKFlqiyL64Z0Aj1BGxvLoI/UX0LBqhJ PYpDpRFJRvPp6651KSUDAiaxZUq2XW4n0AYMrsC+a4u72u/bovD3kMYdSzpUJMJBuXLp Xll3WROEd/i/AXDXLth6mTg5YhtomnIG3gmBe4UM4T4MGrkUKxY8T5YkBmrc9C1LO3Gz k8CsivAvDrsash1wIdrRvRvzDrN3xY+7d7YsPzF/QiywRSU77dR4OriBzh5YPfF4NgQw KdzQ== X-Gm-Message-State: AC+VfDzH6E5maqp631Di/ULTsmaK/OiU7oDKFlhvtOILR7EoZQvMbsYR I9eydpGCnIJI3moP2B70PmSgUNtgAGzm1QyDmEg= X-Google-Smtp-Source: ACHHUZ78NcyxLi4I5Lbprz1ldq/rZ5EuiHoKUOhmY9s+QPCVsX0dFUZE0y5G4UyVXY8O7oi5EqZ9Ww== X-Received: by 2002:a05:6808:148b:b0:398:4ad8:5ce6 with SMTP id e11-20020a056808148b00b003984ad85ce6mr18275546oiw.30.1686845639338; Thu, 15 Jun 2023 09:13:59 -0700 (PDT) Received: from localhost ([2607:fb90:e696:c1dc:3996:e070:71ee:42fb]) by smtp.gmail.com with ESMTPSA id r204-20020acac1d5000000b0039cecd48728sm3441282oif.9.2023.06.15.09.13.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Jun 2023 09:13:58 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Thu, 15 Jun 2023 11:13:57 -0500 Subject: Re: Support to define custom wait events for extensions To: "Masahiro Ikeda" , From: "Tristan Partin" Message-Id: X-Mailer: aerc 0.15.2-46-g263d8cbec504 References: In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk We had this on our list of things to do at Neon, so it is a nice surprise that you brought up an initial patchset :). It was also my first time looking up the word tranche. From 59a118402e5e59685fb9e0fb086872e25a405736 Mon Sep 17 00:00:00 2001 From: Masahiro Ikeda Date: Thu, 15 Jun 2023 12:57:29 +0900 Subject: [PATCH 2/3] Support to define custom wait events for extensions. > Currently, only one PG_WAIT_EXTENSION event can be used as a > wait event for extensions. Therefore, in environments with multiple > extensions are installed, it could take time to identify bottlenecks. "extensions are installed" should be "extensions installed". > +#define NUM_BUILDIN_WAIT_EVENT_EXTENSION \ > + (WAIT_EVENT_EXTENSION_FIRST_USER_DEFINED - WAIT_EVENT_EXTENSION) Should that be NUM_BUILTIN_WAIT_EVENT_EXTENSION? > + NamedExtensionWaitEventTrancheRequestArray =3D (NamedExte= nsionWaitEventTrancheRequest *) > + MemoryContextAlloc(TopMemoryContext, > + NamedExtension= WaitEventTrancheRequestsAllocated > + * sizeof(Named= ExtensionWaitEventTrancheRequest)); I can't tell from reading other Postgres code when one should cast the return value of MemoryContextAlloc(). Seems unnecessary to me. > + if (NamedExtensionWaitEventTrancheRequestArray =3D=3D NULL) > + { > + NamedExtensionWaitEventTrancheRequestsAllocated =3D 16; > + NamedExtensionWaitEventTrancheRequestArray =3D (NamedExte= nsionWaitEventTrancheRequest *) > + MemoryContextAlloc(TopMemoryContext, > + NamedExtension= WaitEventTrancheRequestsAllocated > + * sizeof(Named= ExtensionWaitEventTrancheRequest)); > + } > + > + if (NamedExtensionWaitEventTrancheRequests >=3D NamedExtensionWai= tEventTrancheRequestsAllocated) > + { > + int i =3D pg_nextpower2_32(NamedExten= sionWaitEventTrancheRequests + 1); > + > + NamedExtensionWaitEventTrancheRequestArray =3D (NamedExte= nsionWaitEventTrancheRequest *) > + repalloc(NamedExtensionWaitEventTrancheRequestArr= ay, > + i * sizeof(NamedExtensionWaitEve= ntTrancheRequest)); > + NamedExtensionWaitEventTrancheRequestsAllocated =3D i; > + } Do you think this code would look better in an if/else if? > + int i =3D pg_nextpower2_32(NamedExten= sionWaitEventTrancheRequests + 1); In the Postgres codebase, is an int always guaranteed to be at least 32 bits? I feel like a fixed-width type would be better for tracking the length of the array, unless Postgres prefers the Size type. > + Assert(strlen(tranche_name) + 1 <=3D NAMEDATALEN); > + strlcpy(request->tranche_name, tranche_name, NAMEDATALEN); A sizeof(request->tranche_name) would keep this code more in-sync if size of tranche_name were to ever change, though I see sizeof expressions in the codebase are not too common. Maybe just remove the +1 and make it less than rather than a less than or equal? Seems like it might be worth noting in the docs of the function that the event name has to be less than NAMEDATALEN, but maybe this is something extension authors are inherently aware of? --- What's the Postgres policy on the following? for (int i =3D 0; ...) for (i =3D 0; ...) You are using 2 different patterns in WaitEventShmemInit() and InitializeExtensionWaitEventTranches(). > + /* > + * Copy the info about any named tranches into shared memory (so = that > + * other processes can see it), and initialize the requested wait= events. > + */ > + if (NamedExtensionWaitEventTrancheRequests > 0) Removing this if would allow one less indentation level. Nothing would have to change about the containing code either since the for loop will then not run > + ExtensionWaitEventCounter =3D (int *) ((char *) NamedExtensionWai= tEventTrancheArray - sizeof(int)); From 65e25d4b27bbb6d0934872310c24ee19f89e9631 Mon Sep 17 00:00:00 2001 From: Masahiro Ikeda Date: Thu, 15 Jun 2023 13:16:00 +0900 Subject: [PATCH 3/3] Add docs to define custom wait events > + > + wait events are reserved by calling: > + > +void RequestNamedExtensionWaitEventTranche(const char *tranche_name) > + > + from your shmem_request_hook. This will ensure = that > + wait event is available under the name tranche_name, > + which the wait event type is Extension. > + Use GetNamedExtensionWaitEventTranche > + to get a wait event information. > + > + > + To avoid possible race-conditions, each backend should use the LWLo= ck > + AddinShmemInitLock when connecting to and init= ializing > + its allocation of shared memory, same as LWLocks reservations above= . > + Should "wait" be capitalized in the first sentence? "This will ensure that wait event is available" should have an "a" before "wait". Nice patch. --=20 Tristan Partin Neon (https://neon.tech)