Skip to content

Commit 5deb563

Browse files
committed
Expand assertion check for query ID reporting in executor
As formulated, the assertion added in the executor by 24f5205 to check that a query ID is set had two problems: - track_activities may be disabled while compute_query_id is enabled, causing the query ID to not be reported to pg_stat_activity. - debug_query_string may not be set in some context. The only path where this would matter is visibly autovacuum, should parallel workers be enabled there at some point. This is not the case currently. There was no test showing the interactions between the query ID and track_activities, so let's add one based on a scan of pg_stat_activity. This assertion is still an experimentation at this stage, but let's see if this shows more paths where query IDs are not properly set while they should. Discussion: https://postgr.es/m/Zvn5616oYXmpXyHI@paquier.xyz
1 parent 102de3b commit 5deb563

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/backend/executor/execMain.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
7171
/* Hook for plugin to get control in ExecCheckPermissions() */
7272
ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook = NULL;
7373

74+
/*
75+
* Check that the query ID is set, which is something that happens only
76+
* if compute_query_id is enabled (or a module forced it), if track_activities
77+
* is enabled, and if a client provided a query string to map with the query
78+
* ID computed from it.
79+
*/
80+
#define EXEC_CHECK_QUERY_ID \
81+
do { \
82+
Assert(!IsQueryIdEnabled() || !pgstat_track_activities || \
83+
!debug_query_string || pgstat_get_my_query_id() != 0); \
84+
} while(0)
85+
7486
/* decls for local routines only used within this module */
7587
static void InitPlan(QueryDesc *queryDesc, int eflags);
7688
static void CheckValidRowMarkRel(Relation rel, RowMarkType markType);
@@ -297,7 +309,7 @@ ExecutorRun(QueryDesc *queryDesc,
297309
bool execute_once)
298310
{
299311
/* If enabled, the query ID should be set. */
300-
Assert(!IsQueryIdEnabled() || pgstat_get_my_query_id() != 0);
312+
EXEC_CHECK_QUERY_ID;
301313

302314
if (ExecutorRun_hook)
303315
(*ExecutorRun_hook) (queryDesc, direction, count, execute_once);
@@ -408,7 +420,7 @@ void
408420
ExecutorFinish(QueryDesc *queryDesc)
409421
{
410422
/* If enabled, the query ID should be set. */
411-
Assert(!IsQueryIdEnabled() || pgstat_get_my_query_id() != 0);
423+
EXEC_CHECK_QUERY_ID;
412424

413425
if (ExecutorFinish_hook)
414426
(*ExecutorFinish_hook) (queryDesc);
@@ -471,7 +483,7 @@ void
471483
ExecutorEnd(QueryDesc *queryDesc)
472484
{
473485
/* If enabled, the query ID should be set. */
474-
Assert(!IsQueryIdEnabled() || pgstat_get_my_query_id() != 0);
486+
EXEC_CHECK_QUERY_ID;
475487

476488
if (ExecutorEnd_hook)
477489
(*ExecutorEnd_hook) (queryDesc);

src/test/regress/expected/guc.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,27 @@ set default_with_oids to f;
824824
-- Should not allow to set it to true.
825825
set default_with_oids to t;
826826
ERROR: tables declared WITH OIDS are not supported
827+
-- Test that disabling track_activities disables query ID reporting in
828+
-- pg_stat_activity.
829+
SET compute_query_id = on;
830+
SET track_activities = on;
831+
SELECT query_id IS NOT NULL AS qid_set FROM pg_stat_activity
832+
WHERE pid = pg_backend_pid();
833+
qid_set
834+
---------
835+
t
836+
(1 row)
837+
838+
SET track_activities = off;
839+
SELECT query_id IS NOT NULL AS qid_set FROM pg_stat_activity
840+
WHERE pid = pg_backend_pid();
841+
qid_set
842+
---------
843+
f
844+
(1 row)
845+
846+
RESET track_activities;
847+
RESET compute_query_id;
827848
-- Test GUC categories and flag patterns
828849
SELECT pg_settings_get_flags(NULL);
829850
pg_settings_get_flags

src/test/regress/sql/guc.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,18 @@ set default_with_oids to f;
319319
-- Should not allow to set it to true.
320320
set default_with_oids to t;
321321

322+
-- Test that disabling track_activities disables query ID reporting in
323+
-- pg_stat_activity.
324+
SET compute_query_id = on;
325+
SET track_activities = on;
326+
SELECT query_id IS NOT NULL AS qid_set FROM pg_stat_activity
327+
WHERE pid = pg_backend_pid();
328+
SET track_activities = off;
329+
SELECT query_id IS NOT NULL AS qid_set FROM pg_stat_activity
330+
WHERE pid = pg_backend_pid();
331+
RESET track_activities;
332+
RESET compute_query_id;
333+
322334
-- Test GUC categories and flag patterns
323335
SELECT pg_settings_get_flags(NULL);
324336
SELECT pg_settings_get_flags('does_not_exist');

0 commit comments

Comments
 (0)