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 1uybcw-00AFbh-RE for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Sep 2025 19:46:10 +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 1uybcu-002HEO-BF for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Sep 2025 19:46:09 +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.94.2) (envelope-from ) id 1uybcu-002HEG-0I for pgsql-hackers@lists.postgresql.org; Tue, 16 Sep 2025 19:46:08 +0000 Received: from mail-il1-x132.google.com ([2607:f8b0:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uybcq-001BQt-1D for pgsql-hackers@postgresql.org; Tue, 16 Sep 2025 19:46:07 +0000 Received: by mail-il1-x132.google.com with SMTP id e9e14a558f8ab-4240784853fso27069875ab.3 for ; Tue, 16 Sep 2025 12:46:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758051964; x=1758656764; darn=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=COO2pPZU1gi9nQ/u8xGIoiXm7D8JPvOZvhd0HLVkxKA=; b=WhZvDJRDL1cnmTVqmNXGQuiD/lEnYH6WlyeRGHsUwxCtGLegg6QkicFOPwH+o83WxV sCnRd3IjI2e/B7ChoMWhEM+FMGiimg0l8n2+Drr9cm2DZq59xEoU/w5SY5GUnOuHDaec m+/gSybz/qZZux1pKJw8CHkwX97wV5CXqPhFp2AL7+Gc0Ajqed30H/cX6TBOVjdTUGNO fLUrhRrzAe+e/XsH4wfg9AzK/5sFhFkzY5umr8nlm/ZfpFmX2qnmQ/5cSiygp9nGoG/a QWgKjmVyJhGqs8upM3yiMSwvExne9MV0zhoi6/fR46jX1sbL0Ct4LCa7rZjgQ9zE5JRg bftw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758051964; x=1758656764; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=COO2pPZU1gi9nQ/u8xGIoiXm7D8JPvOZvhd0HLVkxKA=; b=fueloR4GfXtBYP2hts4eLR/nWLKcoUdP0ZMUSlIOxBTBxwhUpd22GmZqyKf8GUIfDY el2O4pIfpo7cjEGWEQek2cmlkkJF69nKtnMMoNUUTfjSPPHK8td432e+kUdOcnecb6jk fLYh6xccnlQX47ueEEuqs0zyBUrSRVjGa1gStxvckcguWdlCCyJc2j3Pe6JqQ3UFucUs Eez1PZ0Rl+DkEJUZqnJatl0YfrKVNJ3mUH4mjpXUm7mhCwDu2LwoXP17qiIMpXMyafVJ 3/MGdFcXKXEQW97Tr6AwPn22MKJtL4LSEtMAiapcvSdwDOEeZOtgdfTow36VA6EsRFb9 KUag== X-Gm-Message-State: AOJu0YyKZLNlQ7OcN7Qdhiu/d6PSK/JxidfzrqiufEAEKcyetANO6L16 t/Wd1sZ9Q097K1hgjfpEXg7j4nvh+vaKhDDecU8tPD2pDKaTWbk+zQoHlmLx2w== X-Gm-Gg: ASbGncvS33iuq6Mf0Iu80AEgKTG7k3t8qSn1/fyExUJb74L5EKgsaIQNggJvP2UoJ1N ld1zYU0bP9Xlf0m4FvZrKGcRmbTrMVHxsxFlmGBdYjaQzsMug6eqNKhdKYbqdbIipuUZ91ILjlC JxyGPECzyg5HDXOv8wvG0ibgh/Bs0yLESF8r6VfcTCimuUWJx+ibCjL27TVgtgE62aDokJcI3c7 3okgyt3c8TJEVAP8ik/Xi8NcB+uQWxgoWUeTXAceQiiy2RS9xW7tP+/YecqoKwYgwWTYtD3AghC hg3bUKQD9v8uvVRiA34hOMRoXvN4WALkkcU1zPhwpE7SizvYGyFAjaVEgnxcPvx2mrUxD7HV/nR /lyeYeIRwkq/eWpoREjTuKY14CugP6WqPxr6sV30ubshQ9KQ64fa3WGVOkgv5ZKCOg445+Aj0l9 1TESyj//ZlMtaPUecCvrA4RaE= X-Google-Smtp-Source: AGHT+IF0wsTDBvpREprvDrtDZx7Zqg1eWK0oapUtY/1oWqTkPNLA4+5i0il1fpUoN/zeNweJuwjinw== X-Received: by 2002:a05:6e02:18cd:b0:424:71:32e5 with SMTP id e9e14a558f8ab-42400713e12mr88720865ab.31.1758051963766; Tue, 16 Sep 2025 12:46:03 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-515fe2a2f21sm2807416173.64.2025.09.16.12.46.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Sep 2025 12:46:03 -0700 (PDT) Date: Tue, 16 Sep 2025 14:46:01 -0500 From: Nathan Bossart To: Sami Imseih Cc: pgsql-hackers Subject: Re: GetNamedLWLockTranche crashes on Windows in normal backend Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="6bRGgFxlVbDeESTo" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --6bRGgFxlVbDeESTo Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Sep 16, 2025 at 02:35:55PM -0500, Nathan Bossart wrote: > On Thu, Sep 11, 2025 at 04:18:00PM -0500, Nathan Bossart wrote: >> I've committed 0001, and I plan to look closer at 0002 soon. > > I ended up rewriting 0002 as an ordinary regression test, which resulted in > a much smaller patch with comparable coverage. (Yes, it needs some > comments.) Thoughts? Sorry for the noise. I was able to simplify the test code further. -- nathan --6bRGgFxlVbDeESTo Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v6-0001-test_lwlock_tranches.patch From 92d7a5569d94f1b5c429d9b52868e5d3f6fe2b2c Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 16 Sep 2025 10:15:11 -0500 Subject: [PATCH v6 1/1] test_lwlock_tranches --- src/test/modules/Makefile | 1 + src/test/modules/meson.build | 1 + .../modules/test_lwlock_tranches/.gitignore | 4 + .../modules/test_lwlock_tranches/Makefile | 25 +++++ .../expected/test_lwlock_tranches.out | 29 ++++++ .../modules/test_lwlock_tranches/meson.build | 35 +++++++ .../sql/test_lwlock_tranches.sql | 13 +++ .../test_lwlock_tranches--1.0.sql | 13 +++ .../test_lwlock_tranches.c | 98 +++++++++++++++++++ .../test_lwlock_tranches.conf | 1 + .../test_lwlock_tranches.control | 4 + 11 files changed, 224 insertions(+) create mode 100644 src/test/modules/test_lwlock_tranches/.gitignore create mode 100644 src/test/modules/test_lwlock_tranches/Makefile create mode 100644 src/test/modules/test_lwlock_tranches/expected/test_lwlock_tranches.out create mode 100644 src/test/modules/test_lwlock_tranches/meson.build create mode 100644 src/test/modules/test_lwlock_tranches/sql/test_lwlock_tranches.sql create mode 100644 src/test/modules/test_lwlock_tranches/test_lwlock_tranches--1.0.sql create mode 100644 src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c create mode 100644 src/test/modules/test_lwlock_tranches/test_lwlock_tranches.conf create mode 100644 src/test/modules/test_lwlock_tranches/test_lwlock_tranches.control diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile index 903a8ac151a..8a3cd2afab7 100644 --- a/src/test/modules/Makefile +++ b/src/test/modules/Makefile @@ -29,6 +29,7 @@ SUBDIRS = \ test_integerset \ test_json_parser \ test_lfind \ + test_lwlock_tranches \ test_misc \ test_oat_hooks \ test_parser \ diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build index 93be0f57289..717e85066ba 100644 --- a/src/test/modules/meson.build +++ b/src/test/modules/meson.build @@ -28,6 +28,7 @@ subdir('test_int128') subdir('test_integerset') subdir('test_json_parser') subdir('test_lfind') +subdir('test_lwlock_tranches') subdir('test_misc') subdir('test_oat_hooks') subdir('test_parser') diff --git a/src/test/modules/test_lwlock_tranches/.gitignore b/src/test/modules/test_lwlock_tranches/.gitignore new file mode 100644 index 00000000000..5dcb3ff9723 --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/.gitignore @@ -0,0 +1,4 @@ +# Generated subdirectories +/log/ +/results/ +/tmp_check/ diff --git a/src/test/modules/test_lwlock_tranches/Makefile b/src/test/modules/test_lwlock_tranches/Makefile new file mode 100644 index 00000000000..e357b7d6d66 --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/Makefile @@ -0,0 +1,25 @@ +# src/test/modules/test_lwlock_tranches/Makefile + +MODULE_big = test_lwlock_tranches +OBJS = \ + $(WIN32RES) \ + test_lwlock_tranches.o +PGFILEDESC = "test_lwlock_tranches - test code for LWLock tranches allocated by extensions" + +EXTENSION = test_lwlock_tranches +DATA = test_lwlock_tranches--1.0.sql + +REGRESS_OPTS = --temp-config $(top_srcdir)/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.conf +REGRESS = test_lwlock_tranches +NO_INSTALLCHECK = 1 + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = src/test/modules/test_lwlock_tranches +top_builddir = ../../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/src/test/modules/test_lwlock_tranches/expected/test_lwlock_tranches.out b/src/test/modules/test_lwlock_tranches/expected/test_lwlock_tranches.out new file mode 100644 index 00000000000..b8e2d7feaca --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/expected/test_lwlock_tranches.out @@ -0,0 +1,29 @@ +CREATE EXTENSION test_lwlock_tranches; +\c +SELECT test_lwlock_tranches(); + test_lwlock_tranches +---------------------- + +(1 row) + +\c +SELECT test_lwlock_tranche_creation(NULL); +ERROR: tranche name cannot be NULL +\c +SELECT test_lwlock_tranche_creation(repeat('a', 64)); +ERROR: tranche name too long +DETAIL: LWLock tranche names must be no longer than 63 bytes. +\c +SELECT test_lwlock_tranche_creation('test'); +ERROR: maximum number of tranches already registered +DETAIL: No more than 256 tranches may be registered. +\c +SELECT test_lwlock_tranche_lookup('test_lwlock_tranches_startup'); + test_lwlock_tranche_lookup +---------------------------- + +(1 row) + +\c +SELECT test_lwlock_tranche_lookup('bogus'); +ERROR: requested tranche is not registered diff --git a/src/test/modules/test_lwlock_tranches/meson.build b/src/test/modules/test_lwlock_tranches/meson.build new file mode 100644 index 00000000000..755c2b72267 --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/meson.build @@ -0,0 +1,35 @@ +# Copyright (c) 2025, PostgreSQL Global Development Group + +test_lwlock_tranches_sources = files( + 'test_lwlock_tranches.c', +) + +if host_system == 'windows' + test_lwlock_tranches_sources += rc_lib_gen.process(win32ver_rc, extra_args: [ + '--NAME', 'test_lwlock_tranches', + '--FILEDESC', 'test_lwlock_tranches - test code for LWLock tranches allocated by extensions',]) +endif + +test_lwlock_tranches = shared_module('test_lwlock_tranches', + test_lwlock_tranches_sources, + kwargs: pg_test_mod_args, +) +test_install_libs += test_lwlock_tranches + +test_install_data += files( + 'test_lwlock_tranches.control', + 'test_lwlock_tranches--1.0.sql', +) + +tests += { + 'name': 'test_lwlock_tranches', + 'sd': meson.current_source_dir(), + 'bd': meson.current_build_dir(), + 'regress': { + 'sql': [ + 'test_lwlock_tranches', + ], + 'regress_args': ['--temp-config', files('test_lwlock_tranches.conf')], + 'runningcheck': false, + }, +} diff --git a/src/test/modules/test_lwlock_tranches/sql/test_lwlock_tranches.sql b/src/test/modules/test_lwlock_tranches/sql/test_lwlock_tranches.sql new file mode 100644 index 00000000000..f4de50e38d9 --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/sql/test_lwlock_tranches.sql @@ -0,0 +1,13 @@ +CREATE EXTENSION test_lwlock_tranches; +\c +SELECT test_lwlock_tranches(); +\c +SELECT test_lwlock_tranche_creation(NULL); +\c +SELECT test_lwlock_tranche_creation(repeat('a', 64)); +\c +SELECT test_lwlock_tranche_creation('test'); +\c +SELECT test_lwlock_tranche_lookup('test_lwlock_tranches_startup'); +\c +SELECT test_lwlock_tranche_lookup('bogus'); diff --git a/src/test/modules/test_lwlock_tranches/test_lwlock_tranches--1.0.sql b/src/test/modules/test_lwlock_tranches/test_lwlock_tranches--1.0.sql new file mode 100644 index 00000000000..b7d9a400f3b --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/test_lwlock_tranches--1.0.sql @@ -0,0 +1,13 @@ +/* src/test/modules/test_lwlock_tranches/test_lwlock_tranches--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION test_lwlock_tranches" to load this file. \quit + +CREATE FUNCTION test_lwlock_tranches() RETURNS VOID + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION test_lwlock_tranche_creation(tranche_name TEXT) RETURNS VOID + AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE FUNCTION test_lwlock_tranche_lookup(tranche_name TEXT) RETURNS VOID + AS 'MODULE_PATHNAME' LANGUAGE C; diff --git a/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c b/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c new file mode 100644 index 00000000000..7a6556961b1 --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c @@ -0,0 +1,98 @@ +/*-------------------------------------------------------------------------- + * + * test_lwlock_tranches.c + * Test code for LWLock tranches allocated by extensions. + * + * Copyright (c) 2025, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c + * + * ------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include "fmgr.h" +#include "miscadmin.h" +#include "storage/lwlock.h" +#include "utils/builtins.h" +#include "utils/wait_classes.h" + +PG_MODULE_MAGIC; + +#define STARTUP_TRANCHE_NAME "test_lwlock_tranches_startup" +#define DYNAMIC_TRANCHE_NAME "test_lwlock_tranches_dynamic" + +#define NUM_STARTUP_TRANCHES (256 - 2) +#define NUM_DYNAMIC_TRANCHES (256 - NUM_STARTUP_TRANCHES) + +#define GET_TRANCHE_NAME(a) GetLWLockIdentifier(PG_WAIT_LWLOCK, (a)) + +static shmem_request_hook_type prev_shmem_request_hook; +static void test_lwlock_tranches_shmem_request(void); + +void +_PG_init(void) +{ + prev_shmem_request_hook = shmem_request_hook; + shmem_request_hook = test_lwlock_tranches_shmem_request; +} + +static void +test_lwlock_tranches_shmem_request(void) +{ + if (prev_shmem_request_hook) + prev_shmem_request_hook(); + + for (int i = 0; i < NUM_STARTUP_TRANCHES; i++) + RequestNamedLWLockTranche(STARTUP_TRANCHE_NAME, 1); +} + +PG_FUNCTION_INFO_V1(test_lwlock_tranches); +Datum +test_lwlock_tranches(PG_FUNCTION_ARGS) +{ + int dynamic_tranches[NUM_DYNAMIC_TRANCHES]; + + for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++) + dynamic_tranches[i] = LWLockNewTrancheId(DYNAMIC_TRANCHE_NAME); + + for (int i = 0; i < NUM_STARTUP_TRANCHES; i++) + { + if (strcmp(GET_TRANCHE_NAME(LWTRANCHE_FIRST_USER_DEFINED + i), + STARTUP_TRANCHE_NAME) != 0) + elog(ERROR, "incorrect startup lock tranche name"); + } + + for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++) + { + if (strcmp(GET_TRANCHE_NAME(dynamic_tranches[i]), + DYNAMIC_TRANCHE_NAME) != 0) + elog(ERROR, "incorrect dynamic lock tranche name"); + } + + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(test_lwlock_tranche_creation); +Datum +test_lwlock_tranche_creation(PG_FUNCTION_ARGS) +{ + char *tranche_name = PG_ARGISNULL(0) ? NULL : TextDatumGetCString(PG_GETARG_DATUM(0)); + + (void) LWLockNewTrancheId(tranche_name); + + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(test_lwlock_tranche_lookup); +Datum +test_lwlock_tranche_lookup(PG_FUNCTION_ARGS) +{ + char *tranche_name = TextDatumGetCString(PG_GETARG_DATUM(0)); + + (void) GetNamedLWLockTranche(tranche_name); + + PG_RETURN_VOID(); +} diff --git a/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.conf b/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.conf new file mode 100644 index 00000000000..acbe5bf51c3 --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.conf @@ -0,0 +1 @@ +shared_preload_libraries = 'test_lwlock_tranches' diff --git a/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.control b/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.control new file mode 100644 index 00000000000..1cde04bdc0b --- /dev/null +++ b/src/test/modules/test_lwlock_tranches/test_lwlock_tranches.control @@ -0,0 +1,4 @@ +comment = 'Test code for LWLock tranches allocated by extensions' +default_version = '1.0' +module_pathname = '$libdir/test_lwlock_tranches' +relocatable = true -- 2.39.5 (Apple Git-154) --6bRGgFxlVbDeESTo--