From 74db4c3270928389b58e6d960958d2f594a0e29d Mon Sep 17 00:00:00 2001
From: Jakub Wartak <jakub.wartak@enterprisedb.com>
Date: Tue, 8 Sep 2026 12:44:55 +0200
Subject: [PATCH videa1] partially working USE_ASSERT_CHECKING for pthreads
 mutexes

---
 src/include/port/pg_threads/map_pthread_ext.h | 5 +++++
 src/port/pg_threads.c                         | 7 +++++++
 2 files changed, 12 insertions(+)

diff --git a/src/include/port/pg_threads/map_pthread_ext.h b/src/include/port/pg_threads/map_pthread_ext.h
index 3c591f15b55..38b74832211 100644
--- a/src/include/port/pg_threads/map_pthread_ext.h
+++ b/src/include/port/pg_threads/map_pthread_ext.h
@@ -45,8 +45,13 @@ enum
 
 #ifndef PG_THREADS_USE_THREADS_H
 #define PG_CND_INIT_IMPL			PTHREAD_COND_INITIALIZER
+#if defined(USE_ASSERT_CHECKING) && defined (PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP)
+/* XXX: this seems to be glibc only */
+#define PG_MTX_PLAIN_INIT_IMPL		PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
+#else
 #define PG_MTX_PLAIN_INIT_IMPL		PTHREAD_MUTEX_INITIALIZER
 #endif
+#endif
 #define PG_RWLOCK_INIT_IMPL			PTHREAD_RWLOCK_INITIALIZER
 
 /* pg_rwlock_t */
diff --git a/src/port/pg_threads.c b/src/port/pg_threads.c
index 07dc7fe0dd2..14ab6399d9d 100644
--- a/src/port/pg_threads.c
+++ b/src/port/pg_threads.c
@@ -797,6 +797,13 @@ pg_mtx_init_impl(pg_mtx_impl *mtx, int type)
 	pthread_mutexattr_init(&attr);
 	if (type & pg_mtx_recursive_impl)
 		pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+#ifdef USE_ASSERT_CHECKING
+	else
+	{
+		/* Requires mutex initialized with PTHREAD_ERRORCHECK_MUTEX_INITIALIZER */
+		pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+	}
+#endif
 	result = pg_thrd_map(pthread_mutex_init(mtx, &attr));
 	pthread_mutexattr_destroy(&attr);
 
-- 
2.43.0

