From dd524f2dde44b3907aa457777192c19d3dba9097 Mon Sep 17 00:00:00 2001
From: Georgy Shelkovy <rekgrpth@gmail.com>
Date: Fri, 21 Feb 2025 09:08:18 +0500
Subject: [PATCH 1/7] Fix compatibility with pg18

Upstream commit postgres/postgres@525392d changed return type of ExecutorStart_hook API from void to bool.
---
 pg_wait_sampling.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c
index 153d875..0a12003 100644
--- a/pg_wait_sampling.c
+++ b/pg_wait_sampling.c
@@ -73,7 +73,13 @@ static PlannedStmt *pgws_planner_hook(Query *parse,
 		const char *query_string,
 #endif
 		int cursorOptions, ParamListInfo boundParams);
-static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags);
+static
+#if PG_VERSION_NUM >= 180000
+bool
+#else
+void
+#endif
+pgws_ExecutorStart(QueryDesc *queryDesc, int eflags);
 static void pgws_ExecutorRun(QueryDesc *queryDesc,
 							 ScanDirection direction,
 							 uint64 count
@@ -965,16 +971,21 @@ pgws_planner_hook(Query *parse,
 /*
  * ExecutorStart hook: save queryId for collector
  */
-static void
+static
+#if PG_VERSION_NUM >= 180000
+bool
+#else
+void
+#endif
 pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
 {
 	int i = MyProc - ProcGlobal->allProcs;
 	if (pgws_enabled(nesting_level))
 		pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId;
 	if (prev_ExecutorStart)
-		prev_ExecutorStart(queryDesc, eflags);
+		return prev_ExecutorStart(queryDesc, eflags);
 	else
-		standard_ExecutorStart(queryDesc, eflags);
+		return standard_ExecutorStart(queryDesc, eflags);
 }
 
 static void

From 3c1046c83c0a45e9fd43d7e805e21a916c25ee30 Mon Sep 17 00:00:00 2001
From: Oleg Tselebrovskiy <o.tselebrovskiy@postgrespro.ru>
Date: Mon, 24 Feb 2025 14:38:08 +0700
Subject: [PATCH 2/7] Remove units from profile_period and history_period

---
 pg_wait_sampling.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c
index 29f487e..a35fb94 100644
--- a/pg_wait_sampling.c
+++ b/pg_wait_sampling.c
@@ -375,7 +375,7 @@ _PG_init(void)
 							1,
 							INT_MAX,
 							PGC_SIGHUP,
-							GUC_UNIT_MS,
+							0,
 							NULL,
 							NULL,
 							NULL);
@@ -388,7 +388,7 @@ _PG_init(void)
 							1,
 							INT_MAX,
 							PGC_SIGHUP,
-							GUC_UNIT_MS,
+							0,
 							NULL,
 							NULL,
 							NULL);

From 24b2d144cff961c514579b998de59112099fe3c0 Mon Sep 17 00:00:00 2001
From: Oleg Tselebrovskiy <o.tselebrovskiy@postgrespro.ru>
Date: Thu, 10 Apr 2025 16:55:29 +0700
Subject: [PATCH 3/7] Fix return of pgws_ExecutorStart since some systems gave
 warnings when returning from void functions

---
 pg_wait_sampling.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c
index a35fb94..81c37ea 100644
--- a/pg_wait_sampling.c
+++ b/pg_wait_sampling.c
@@ -995,9 +995,17 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
 	if (pgws_enabled(nesting_level))
 		pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId;
 	if (prev_ExecutorStart)
+#if PG_VERSION_NUM >= 180000
 		return prev_ExecutorStart(queryDesc, eflags);
+#else
+		prev_ExecutorStart(queryDesc, eflags);
+#endif
 	else
+#if PG_VERSION_NUM >= 180000
 		return standard_ExecutorStart(queryDesc, eflags);
+#else
+		standard_ExecutorStart(queryDesc, eflags);
+#endif
 }
 
 static void

From 9d713ce514e5b96b13fd6aa1980b6db11ec9ed45 Mon Sep 17 00:00:00 2001
From: Oleg Tselebrovskiy <mr.medvecrab@gmail.com>
Date: Wed, 28 May 2025 12:35:29 +0700
Subject: [PATCH 4/7] Revert "Fix return of pgws_ExecutorStart since some
 systems gave warnings when returning from void functions"

This reverts commit 24b2d144cff961c514579b998de59112099fe3c0.
---
 pg_wait_sampling.c | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c
index 81c37ea..a35fb94 100644
--- a/pg_wait_sampling.c
+++ b/pg_wait_sampling.c
@@ -995,17 +995,9 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
 	if (pgws_enabled(nesting_level))
 		pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId;
 	if (prev_ExecutorStart)
-#if PG_VERSION_NUM >= 180000
 		return prev_ExecutorStart(queryDesc, eflags);
-#else
-		prev_ExecutorStart(queryDesc, eflags);
-#endif
 	else
-#if PG_VERSION_NUM >= 180000
 		return standard_ExecutorStart(queryDesc, eflags);
-#else
-		standard_ExecutorStart(queryDesc, eflags);
-#endif
 }
 
 static void

From fafeda026efb260cdcd4e31e21e6166a41521fb9 Mon Sep 17 00:00:00 2001
From: Oleg Tselebrovskiy <mr.medvecrab@gmail.com>
Date: Wed, 28 May 2025 12:38:45 +0700
Subject: [PATCH 5/7] Revert "Fix compatibility with pg18"

This reverts commit dd524f2dde44b3907aa457777192c19d3dba9097.
---
 pg_wait_sampling.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)

diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c
index a35fb94..f5bd6e0 100644
--- a/pg_wait_sampling.c
+++ b/pg_wait_sampling.c
@@ -76,13 +76,7 @@ static PlannedStmt *pgws_planner_hook(Query *parse,
 									  const char *query_string,
 #endif
 									  int cursorOptions, ParamListInfo boundParams);
-static
-#if PG_VERSION_NUM >= 180000
-bool
-#else
-void
-#endif
-pgws_ExecutorStart(QueryDesc *queryDesc, int eflags);
+static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags);
 static void pgws_ExecutorRun(QueryDesc *queryDesc,
 							 ScanDirection direction,
 							 uint64 count
@@ -982,12 +976,7 @@ pgws_planner_hook(Query *parse,
 /*
  * ExecutorStart hook: save queryId for collector
  */
-static
-#if PG_VERSION_NUM >= 180000
-bool
-#else
-void
-#endif
+static void
 pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
 {
 	int			i = MyProc - ProcGlobal->allProcs;
@@ -995,9 +984,9 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
 	if (pgws_enabled(nesting_level))
 		pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId;
 	if (prev_ExecutorStart)
-		return prev_ExecutorStart(queryDesc, eflags);
+		prev_ExecutorStart(queryDesc, eflags);
 	else
-		return standard_ExecutorStart(queryDesc, eflags);
+		standard_ExecutorStart(queryDesc, eflags);
 }
 
 static void

From 930a67a235f5c2e5e4e9868c22f9bb8289cba45e Mon Sep 17 00:00:00 2001
From: Valeriy Zainullin <v.zaynullin@postgrespro.ru>
Date: Thu, 19 Jun 2025 18:16:01 +0300
Subject: [PATCH 6/7] Check collector has started in
 pg_wait_sampling_reset_profile.

The worker might have not started yet or it may never start,
because its registration was cancelled due to worker limit.

This commit adds a check for NULL value of pgws_collector_hdr->latch.
The previous usage in pg_wait_sampling.c has such a check, we
should do the same here.
---
 pg_wait_sampling.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/pg_wait_sampling.c b/pg_wait_sampling.c
index f5bd6e0..e165a6a 100644
--- a/pg_wait_sampling.c
+++ b/pg_wait_sampling.c
@@ -649,6 +649,10 @@ receive_array(SHMRequest request, Size item_size, Size *count)
 	pgws_collector_hdr->request = request;
 	LockRelease(&collectorTag, ExclusiveLock, false);
 
+        /*
+         * Check that the collector was started to avoid NULL
+         * pointer dereference.
+         */
 	if (!pgws_collector_hdr->latch)
 		ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
 						errmsg("pg_wait_sampling collector wasn't started")));
@@ -819,6 +823,14 @@ pg_wait_sampling_reset_profile(PG_FUNCTION_ARGS)
 	pgws_collector_hdr->request = PROFILE_RESET;
 	LockRelease(&collectorTag, ExclusiveLock, false);
 
+        /*
+         * Check that the collector was started to avoid NULL
+         * pointer dereference.
+         */
+	if (!pgws_collector_hdr->latch)
+		ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
+						errmsg("pg_wait_sampling collector wasn't started")));
+
 	SetLatch(pgws_collector_hdr->latch);
 
 	LockRelease(&queueTag, ExclusiveLock, false);

From f96fb9eaf0b02709af5f9998d6982061bcc7eb70 Mon Sep 17 00:00:00 2001
From: Sergey Shinderuk <s.shinderuk@postgrespro.ru>
Date: Mon, 4 Aug 2025 18:27:26 +0300
Subject: [PATCH 7/7] Fix Travis CI build

---
 .travis.yml  | 2 +-
 run-tests.sh | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml
index 2c63eff..98d4318 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,7 +1,7 @@
 dist: jammy
 language: c
 env:
-- PG_MAJOR=18 SNAPSHOT=1
+- PG_MAJOR=18 BETA=1
 - PG_MAJOR=17
 - PG_MAJOR=16
 - PG_MAJOR=15
diff --git a/run-tests.sh b/run-tests.sh
index f3f1bba..f42e999 100755
--- a/run-tests.sh
+++ b/run-tests.sh
@@ -6,6 +6,7 @@ export PGDATA=/var/lib/postgresql/$PG_MAJOR/test
 export COPT=-Werror
 export USE_PGXS=1
 
+sudo mkdir -p /var/lib/postgresql/$PG_MAJOR
 sudo chmod 1777 /var/lib/postgresql/$PG_MAJOR
 sudo chmod 1777 /var/run/postgresql
 
