From b7f0dd8b23ae6a42ce94ee4c3856f9ff22cc8eb2 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Fri, 13 Sep 2024 14:56:18 +0900
Subject: [PATCH v6 2/2] Add sanity checks in executor for query ID

This causes the test added by the previous commit to blow up on sight,
as would a removal of the query ID reporting in ExecuteStart() blow up
when running the tests of pg_stat_statements.

No backpatck done here.
---
 src/backend/executor/execMain.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index 7042ca6c60..77b8d4762d 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -50,6 +50,7 @@
 #include "foreign/fdwapi.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "nodes/queryjumble.h"
 #include "parser/parse_relation.h"
 #include "rewrite/rewriteHandler.h"
 #include "tcop/utility.h"
@@ -295,6 +296,9 @@ ExecutorRun(QueryDesc *queryDesc,
 			ScanDirection direction, uint64 count,
 			bool execute_once)
 {
+	/* If enabled, the query ID should be set. */
+	Assert(!IsQueryIdEnabled() || pgstat_get_my_query_id() != 0);
+
 	if (ExecutorRun_hook)
 		(*ExecutorRun_hook) (queryDesc, direction, count, execute_once);
 	else
@@ -403,6 +407,9 @@ standard_ExecutorRun(QueryDesc *queryDesc,
 void
 ExecutorFinish(QueryDesc *queryDesc)
 {
+	/* If enabled, the query ID should be set. */
+	Assert(!IsQueryIdEnabled() || pgstat_get_my_query_id() != 0);
+
 	if (ExecutorFinish_hook)
 		(*ExecutorFinish_hook) (queryDesc);
 	else
@@ -418,6 +425,9 @@ standard_ExecutorFinish(QueryDesc *queryDesc)
 	/* sanity checks */
 	Assert(queryDesc != NULL);
 
+	/* If enabled, the query ID should be set. */
+	Assert(!IsQueryIdEnabled() || pgstat_get_my_query_id() != 0);
+
 	estate = queryDesc->estate;
 
 	Assert(estate != NULL);
-- 
2.45.2

