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 1slIil-006dcH-3t for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Sep 2024 01:52: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 1slIik-005MSL-8c for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Sep 2024 01:52:38 +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 1slIii-005MRd-LH for pgsql-hackers@lists.postgresql.org; Tue, 03 Sep 2024 01:52:38 +0000 Received: from fhigh2-smtp.messagingengine.com ([103.168.172.153]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1slIib-000NB8-TJ for pgsql-hackers@lists.postgresql.org; Tue, 03 Sep 2024 01:52:35 +0000 Received: from phl-compute-05.internal (phl-compute-05.nyi.internal [10.202.2.45]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 95C3C1140176 for ; Mon, 2 Sep 2024 21:52:28 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Mon, 02 Sep 2024 21:52:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm1; t=1725328348; x=1725414748; bh=zE326zAi4hQHWFRCWCs5w4MIgKAfI2mi bvBpZMRdPXQ=; b=ntsUcBvN8X2jw2/yrm9zFXdn/Kwh+H23guAg2kLVswBvXYhk k6RUqTKC/OP24+iwiBcCNDaDsVSdtjgwowFF6ZD4HhYDKGYWRS7/mFO48pzkfWss sOHXRo4jOY+NO4Lf+IDLpjaZW+hLSv2SuQtlpiLcdi+g90oJuDfFvjd9TTJX7CKH UkjVGLv8RIhFNFo95PCSU/L3isGXwJYOhZuhaSAQCpjbHYfQjzPFXujc27mRSEqi 6DDclOX4z+R7xUVAJ8vlNDXS0P24SDfWMvKf6CRwWE1Rh/xUqJ7JhN60vneiwDpV Ot854TyoWAslpI7NNG+vdJZyhhXcgWB6+5z5tQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1725328348; x=1725414748; bh=zE326zAi4hQHWFRCWCs5w4MIgKAfI2mibvB pZMRdPXQ=; b=M8ufIOq7yRDjIljGNhqaLg8OLbKmsoTS707LSTO/dfpbhSsa/tH bQG4BsdiYsI3wy0j63xZIGWqrLr8OECFikV43NldHc5xH4cqHsDabCFoJ7+4rvDr /z1BHDcK57HMi/O75+5OECCe1L3DWv5qFnQ0qbQD2uXkEuhULrzaZP3R0DgBK107 TM+rCyUvZNsfPG0vhKBWtJtFAnVrp6cXPdQvO4qxuVCuSUs2bo2yrtv2f0yzLmnl WyWv4ojlaLQWQEpKP6pEnU+Uow2ecmvIOVySRf62Md7p8PMsZ8slDzqB3kY/lbtQ WcaCojDy4PlUeUOVsO/FUqw8XYmBHWtv65A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudehgedgheefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnegfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvuffkgggt ugesghdtreertddtvdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmih gthhgrvghlsehprghquhhivghrrdighiiiqeenucggtffrrghtthgvrhhnpedvtdffteeg tdfhuefgvedvtdejgfeugfeggfejgfdvhefgfeevieektdelkefhgfenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehprghq uhhivghrrdighiiipdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpohhuthdprh gtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehlihhsthhsrdhpohhsthhgrhgvshhq lhdrohhrgh X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 2 Sep 2024 21:52:27 -0400 (EDT) Date: Tue, 3 Sep 2024 10:52:20 +0900 From: Michael Paquier To: Postgres hackers Subject: Add callback in pgstats for backend initialization Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="yvQLCtnfU9tfyJhS" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --yvQLCtnfU9tfyJhS Content-Type: multipart/mixed; boundary="91RdqAfIfD8eH+RB" Content-Disposition: inline --91RdqAfIfD8eH+RB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi all, Currently, the backend-level initialization of pgstats happens in pgstat_initialize(), where we are using a shortcut for the WAL stats, with pgstat_init_wal(). I'd like to propose to add a callback for that, so as in-core or custom pgstats kinds can assign custom actions when initializing a backend. The use-case I had for this one are pretty close to what we do for WAL, where we would rely on a difference of state depending on what may have existed before reaching the initialization path. So this can offer more precision. Another case, perhaps less interesting, is to be able to initialize some static backend state. I wanted to get that sent for the current commit fest, but did not get back in time to it. Anyway, here it is. This gives the simple patch attached. Thanks, -- Michael --91RdqAfIfD8eH+RB Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Add-callback-for-backend-initialization-in-pgstats.patch" Content-Transfer-Encoding: quoted-printable =46rom 1e41a5155fb689f1d4ae8a5cfb244b824e913bae Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Tue, 3 Sep 2024 10:38:15 +0900 Subject: [PATCH] Add callback for backend initialization in pgstats This is currently used by the WAL stats, and is now made available for others as well. --- src/include/utils/pgstat_internal.h | 7 +++++++ src/backend/utils/activity/pgstat.c | 14 ++++++++++++-- src/backend/utils/activity/pgstat_wal.c | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat= _internal.h index fb132e439d..53f7d5c98c 100644 --- a/src/include/utils/pgstat_internal.h +++ b/src/include/utils/pgstat_internal.h @@ -229,6 +229,12 @@ typedef struct PgStat_KindInfo */ uint32 pending_size; =20 + /* + * Perform custom actions when initializing a backend (standalone or under + * postmaster). Optional. + */ + void (*init_backend_cb) (void); + /* * For variable-numbered stats: flush pending stats. Required if pending * data is used. @@ -676,6 +682,7 @@ extern bool pgstat_flush_wal(bool nowait); extern void pgstat_init_wal(void); extern bool pgstat_have_pending_wal(void); =20 +extern void pgstat_wal_init_backend_cb(void); extern void pgstat_wal_init_shmem_cb(void *stats); extern void pgstat_wal_reset_all_cb(TimestampTz ts); extern void pgstat_wal_snapshot_cb(void); diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activi= ty/pgstat.c index b2ca3f39b7..3b6983ff5d 100644 --- a/src/backend/utils/activity/pgstat.c +++ b/src/backend/utils/activity/pgstat.c @@ -441,6 +441,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[= PGSTAT_KIND_BUILTIN_SIZE] .shared_data_off =3D offsetof(PgStatShared_Wal, stats), .shared_data_len =3D sizeof(((PgStatShared_Wal *) 0)->stats), =20 + .init_backend_cb =3D pgstat_wal_init_backend_cb, .init_shmem_cb =3D pgstat_wal_init_shmem_cb, .reset_all_cb =3D pgstat_wal_reset_all_cb, .snapshot_cb =3D pgstat_wal_snapshot_cb, @@ -604,10 +605,19 @@ pgstat_initialize(void) =20 pgstat_attach_shmem(); =20 - pgstat_init_wal(); - pgstat_init_snapshot_fixed(); =20 + /* Backend initialization callbacks */ + for (PgStat_Kind kind =3D PGSTAT_KIND_MIN; kind <=3D PGSTAT_KIND_MAX; kin= d++) + { + const PgStat_KindInfo *kind_info =3D pgstat_get_kind_info(kind); + + if (kind_info =3D=3D NULL || kind_info->init_backend_cb =3D=3D NULL) + continue; + + kind_info->init_backend_cb(); + } + /* Set up a process-exit hook to clean up */ before_shmem_exit(pgstat_shutdown_hook, 0); =20 diff --git a/src/backend/utils/activity/pgstat_wal.c b/src/backend/utils/ac= tivity/pgstat_wal.c index e2a3f6b865..8c19c3f2fd 100644 --- a/src/backend/utils/activity/pgstat_wal.c +++ b/src/backend/utils/activity/pgstat_wal.c @@ -138,7 +138,7 @@ pgstat_flush_wal(bool nowait) } =20 void -pgstat_init_wal(void) +pgstat_wal_init_backend_cb(void) { /* * Initialize prevWalUsage with pgWalUsage so that pgstat_flush_wal() can --=20 2.45.2 --91RdqAfIfD8eH+RB-- --yvQLCtnfU9tfyJhS Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmbWa9QACgkQnvQgOdby QH1D0g/+IpKyKMfVHW+PKQIQLPkcFH9sPpZQazuAr/OC9bH3wTVdvmJJcPaJ1HAX n4t9ZM5D6Ove6RY8urdEjBP2CfvEqhA0ogJH9EWbA1V4MPjZijZ30I5mqCyvU65+ g/zwjo1Sfr+w54ntj4P5PtLY8TZ+EshrRIZkh0MGmfRfy4oCumkHbMW9fPv1xrFP slVC81eNezZ2X1BCg8ilvq/XPeKJtKotsfMAeqeNIljDNbI67gjk1M1cCVsABxHg X9t99pr4dN1jyILUtR9Jqg94JWuhuVEY2YtkL/9vloXUFccMnMfn7uZ94NgnvXyC NXZLegzM1nL1mRmd7li2j+NNvILPo5k/fWc6hR/Y4EXLRujqiHIgw/42tDhzEXc3 jwmkkbVK0mHJQe8SMrQp0obum8zWTxsau9QRT2Ug8OMu0Bc7JgjXExSm5KNOWDPv Q+qhEt9w3GjAz6F6bi2fms6anmKwZAdzRCSNrZNHHL3rYrJlqECnHVQVOUG6P7kj 7oxpL7bAT0wnkEflNVlLl1Dw3a8B+KpnJBOAIoSaJMwPPngG5g3gAAy+1oJk0LHv PVG0zOnAI3nCnhWZUUcg8iUHdN3ruckFh7yKFa6cuwcysty9Mdlh+drw7jDnk2bF XDXLqNi6bdB1DELheXCcw6yHDmdD1IJbG0an5vcmxf2Rz8dJOZ0= =s9R9 -----END PGP SIGNATURE----- --yvQLCtnfU9tfyJhS--