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.96) (envelope-from ) id 1wf9H2-005IM8-1f for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Jul 2026 04:43:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wf9H1-00HHQO-0E for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Jul 2026 04:43:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wf9H0-00HHQG-2R for pgsql-hackers@lists.postgresql.org; Thu, 02 Jul 2026 04:43:38 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wf9Gx-00000001JdE-45GK for pgsql-hackers@lists.postgresql.org; Thu, 02 Jul 2026 04:43:38 +0000 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-493c1950518so539265e9.1 for ; Wed, 01 Jul 2026 21:43:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782967415; x=1783572215; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=0W0nGg1Hfj+zICnrT3CitUu2Asyv3WqtNumsxRqyygM=; b=ca+gTXVymyq5U0n6Y+2XQBieiIunhJYkLBtQ1JR4sT3syw9TGCVBptW//RKBD41dWv LHV9nUb5LXURR9yho8Ol/2BF8njVgj93M9+IedmZgpk/LfUDlt9eTFIj3FIuYD67N2Zr W57DD1S0ExVlxfNI+w5BENuq1UUoc/JqoK3IQIhRUiZQjjQiBZX1MvjTU1xElbB06JuG PCI6BqY5y78/J3S2/t7YNIQIULm+dTVkczT2tlASjLXUbdIarAfSocNxZQmfE359G8PJ vCglzcQphBlJaY+hb+Rk68hAdzEvrdvWpQp6pV5BUcPDg/VlWe6QsVy9XZVPkOAevkAz bYpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782967415; x=1783572215; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0W0nGg1Hfj+zICnrT3CitUu2Asyv3WqtNumsxRqyygM=; b=RFhRf5VVkL3aQIdgUWFPPrbqBzw7Kcf4odTxShpGEqJ8oqJHF+GpRNI+4pkehk7PWv 0l7SdNJWSEBOfKumJqF+2Z3W9tnoVziLo6DEWKW0N1Bo/db6MszqrZwdExDz397ntOo1 jXVG/jWKvhIAY4UT+GpFOvbYAAl2HFTljWyYMJn0ks0BSv6633VmFcNr4NNIZfeUgGes P3ZbnNBMmt8SBOMCvodjygvFEJ7TpLMJRhcCAtr2n31pW45LYK0GxuxycgTZuUXijnU9 vNV80pBTeiG3WGb7hfQZ8AJMO/BZMe34lonbBaO8F3vzmGHhnQqm+QgdHhTu4s4MvPKH uQgg== X-Forwarded-Encrypted: i=1; AFNElJ9Ksnir0SGqDyh210PD6OV7lZ69KHpGKW9EyNSBDJiA8Vj7cDq4l0Xd6CeozmSe+Qk+dbtfJM6AiPumVDvj@lists.postgresql.org X-Gm-Message-State: AOJu0YzWHd0VnSkx78Rp2NdUTV0zzl3tsVO7AKqKPMaUtEeEK2Rxa39J Mf028KZvWhzrZqSFcoyfcRZ3oaeBH/m/joL9MpYCR4wt8TM/vNYabe5q X-Gm-Gg: AfdE7cnwW9PMZDVYqnRWMgw6yqVAewnPhqdo5uYyV8X1zBkpmPPYUSDpuom5WPkmhHi YEhPlw4R4JCc/Hks1a4UmA448CyIMpQ3JnkVq/dsf99Au20KZMrH6sFLI02OzYMm9dL0O90y9ag RIZmw5J03Gax7OuE4vXoXtrbpoYBOzBSj+IjQIZknHYl3Zc2RoyHspI4atZfE5pKqmD9H6lNgyx eK/dPcSt3vQY7GtBmkvMsxnOBO4vbsah95GCufx0b1P4OuX/YhjtggLWFuYIPCfMR1hJSzPJVfP WwHIxjCAdkTJNFXoUq/56dH/F5TdQF4APL8ZsKX9fVYBbfMpeGfQ0Ev8KevFjXq149jliv0pQUV txAKfJnNYQUqXvpIs0r7RQRdT3cDtfyYVvYVw7n0dCeMY8he6Ge9jiju8OgN8m0axNOS+alHIZ5 cBb4ysLipKnO7Nr8OhJWQOXfb62MubVHjwvWVHmRhIyYTdeXvs3ebpKkjHIt9ojpyD24oCA8fd X-Received: by 2002:a05:600c:c0c3:20b0:492:2fc8:21f2 with SMTP id 5b1f17b1804b1-493bc22e7b1mr91988825e9.8.1782967414454; Wed, 01 Jul 2026 21:43:34 -0700 (PDT) Received: from bdtpg (ec2-15-237-197-144.eu-west-3.compute.amazonaws.com. [15.237.197.144]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-477db8a4a15sm5541412f8f.14.2026.07.01.21.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jul 2026 21:43:34 -0700 (PDT) Date: Thu, 2 Jul 2026 04:43:32 +0000 From: Bertrand Drouvot To: Michael Paquier Cc: Ewan Young , pgsql-hackers@lists.postgresql.org Subject: Re: Prevent crash when calling pgstat functions with unregistered stats kind Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="i51FQWZELabmlh2Y" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --i51FQWZELabmlh2Y Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Jul 02, 2026 at 04:23:16AM +0000, Bertrand Drouvot wrote: > Hi, > > On Thu, Jul 02, 2026 at 01:10:18PM +0900, Michael Paquier wrote: > > On Thu, Jul 02, 2026 at 04:06:01AM +0000, Bertrand Drouvot wrote: > > > I agree that the responsibility should primarily be in the extension. However, > > > the issue is that the NULL dereference happens inside core code (pgstat_prep_pending_entry, > > > etc.), and the resulting segfault(s) cause the postmaster to terminate all > > > backends (not just the offending session). > > > > > > Given that one misconfigured extension can crash all connections on the server, > > > a defensive check in core seems reasonable (kind of similar to 341e9a05e7b). > > > > Nope, this was a different thing, doable in a couple of steps: > > - Load the library. > > - Write custom stats. > > - Stop the server, flush the stats. > > - Edit the configuration, not loading the library. > > - Restart the server, loading failed. > > > > The problem of this thread ought to be blocked at its source, in the > > extension itself: let's not give free hands to an extension to do what > > it should not be allowed to do. There is a similar defense in > > test_custom_rmgrs, as one example. We should just map to that. > > Ok but what about extensions that don't call pgstat_register_kind() at all? Your > point is that they would see the issue during the development of the extension? (If > so, I think I could agree). Something like in the attached? Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com --i51FQWZELabmlh2Y Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-Fix-test_custom_stats-modules-to-error-out-when-n.patch" From 7468051aa68c0571e2502bcfee3e2a319e96069e Mon Sep 17 00:00:00 2001 From: Bertrand Drouvot Date: Thu, 2 Jul 2026 04:34:49 +0000 Subject: [PATCH v3] Fix test_custom_stats modules to error out when not preloaded Previously, test_custom_var_stats and test_custom_fixed_stats silently skipped pgstat_register_kind() when not loaded via shared_preload_libraries. This left the SQL functions callable without the kind registered, leading to NULL dereferences in core pgstat functions. Remove the early return and let pgstat_register_kind() error out naturally, matching the pattern used by test_custom_rmgrs with RegisterCustomRmgr(). Now CREATE EXTENSION fails with a clear error if the module is not preloaded. Author: Bertrand Drouvot Reviewed-by: Ewan Young Discussion: https://postgr.es/m/akS/ldidWeqG1FWk%40bdtpg --- .../modules/test_custom_stats/test_custom_fixed_stats.c | 9 ++++----- .../modules/test_custom_stats/test_custom_var_stats.c | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) 100.0% src/test/modules/test_custom_stats/ diff --git a/src/test/modules/test_custom_stats/test_custom_fixed_stats.c b/src/test/modules/test_custom_stats/test_custom_fixed_stats.c index a066ce117a6..8b29ca3e27a 100644 --- a/src/test/modules/test_custom_stats/test_custom_fixed_stats.c +++ b/src/test/modules/test_custom_stats/test_custom_fixed_stats.c @@ -72,11 +72,10 @@ static const PgStat_KindInfo custom_stats = { void _PG_init(void) { - /* Must be loaded via shared_preload_libraries */ - if (!process_shared_preload_libraries_in_progress) - return; - - /* Register custom statistics kind */ + /* + * In order to register our custom statistics kind, we have to be loaded + * via shared_preload_libraries. Otherwise, registration will fail. + */ pgstat_register_kind(PGSTAT_KIND_TEST_CUSTOM_FIXED_STATS, &custom_stats); } diff --git a/src/test/modules/test_custom_stats/test_custom_var_stats.c b/src/test/modules/test_custom_stats/test_custom_var_stats.c index 863d6a52492..fa415c81301 100644 --- a/src/test/modules/test_custom_stats/test_custom_var_stats.c +++ b/src/test/modules/test_custom_stats/test_custom_var_stats.c @@ -129,11 +129,10 @@ static const PgStat_KindInfo custom_stats = { void _PG_init(void) { - /* Must be loaded via shared_preload_libraries */ - if (!process_shared_preload_libraries_in_progress) - return; - - /* Register custom statistics kind */ + /* + * In order to register our custom statistics kind, we have to be loaded + * via shared_preload_libraries. Otherwise, registration will fail. + */ pgstat_register_kind(PGSTAT_KIND_TEST_CUSTOM_VAR_STATS, &custom_stats); } -- 2.34.1 --i51FQWZELabmlh2Y--