@@ -95,6 +95,35 @@ static void pgws_ProcessUtility(PlannedStmt *pstmt,
95
95
#endif
96
96
);
97
97
98
+ /*---- GUC variables ----*/
99
+
100
+ typedef enum
101
+ {
102
+ PGWS_PROFILE_QUERIES_NONE , /* profile no statements */
103
+ PGWS_PROFILE_QUERIES_TOP , /* only top level statements */
104
+ PGWS_PROFILE_QUERIES_ALL /* all statements, including nested ones */
105
+ } PGWSTrackLevel ;
106
+
107
+ static const struct config_enum_entry pgws_profile_queries_options [] =
108
+ {
109
+ {"none" , PGWS_PROFILE_QUERIES_NONE , false},
110
+ {"off" , PGWS_PROFILE_QUERIES_NONE , false},
111
+ {"no" , PGWS_PROFILE_QUERIES_NONE , false},
112
+ {"false" , PGWS_PROFILE_QUERIES_NONE , false},
113
+ {"0" , PGWS_PROFILE_QUERIES_NONE , false},
114
+ {"top" , PGWS_PROFILE_QUERIES_TOP , false},
115
+ {"on" , PGWS_PROFILE_QUERIES_TOP , false},
116
+ {"yes" , PGWS_PROFILE_QUERIES_TOP , false},
117
+ {"true" , PGWS_PROFILE_QUERIES_TOP , false},
118
+ {"1" , PGWS_PROFILE_QUERIES_TOP , false},
119
+ {"all" , PGWS_PROFILE_QUERIES_ALL , false},
120
+ {NULL , 0 , false}
121
+ };
122
+
123
+ #define pgws_enabled (level ) \
124
+ ((pgws_collector_hdr->profileQueries == PGWS_PROFILE_QUERIES_ALL) || \
125
+ (pgws_collector_hdr->profileQueries == PGWS_PROFILE_QUERIES_TOP && (level) == 0))
126
+
98
127
/*
99
128
* Calculate max processes count.
100
129
*
@@ -185,6 +214,14 @@ shmem_int_guc_check_hook(int *newval, void **extra, GucSource source)
185
214
return true;
186
215
}
187
216
217
+ static bool
218
+ shmem_enum_guc_check_hook (int * newval , void * * extra , GucSource source )
219
+ {
220
+ if (UsedShmemSegAddr == NULL )
221
+ return false;
222
+ return true;
223
+ }
224
+
188
225
static bool
189
226
shmem_bool_guc_check_hook (bool * newval , void * * extra , GucSource source )
190
227
{
@@ -260,8 +297,8 @@ setup_gucs()
260
297
else if (!strcmp (name , "pg_wait_sampling.profile_queries" ))
261
298
{
262
299
profile_queries_found = true;
263
- var -> _bool .variable = & pgws_collector_hdr -> profileQueries ;
264
- pgws_collector_hdr -> profileQueries = true ;
300
+ var -> _enum .variable = & pgws_collector_hdr -> profileQueries ;
301
+ pgws_collector_hdr -> profileQueries = PGWS_PROFILE_QUERIES_TOP ;
265
302
}
266
303
else if (!strcmp (name , "pg_wait_sampling.sample_cpu" ))
267
304
{
@@ -296,10 +333,10 @@ setup_gucs()
296
333
PGC_SUSET , 0 , shmem_bool_guc_check_hook , NULL , NULL );
297
334
298
335
if (!profile_queries_found )
299
- DefineCustomBoolVariable ("pg_wait_sampling.profile_queries" ,
336
+ DefineCustomEnumVariable ("pg_wait_sampling.profile_queries" ,
300
337
"Sets whether profile should be collected per query." , NULL ,
301
- & pgws_collector_hdr -> profileQueries , true ,
302
- PGC_SUSET , 0 , shmem_bool_guc_check_hook , NULL , NULL );
338
+ & pgws_collector_hdr -> profileQueries , PGWS_PROFILE_QUERIES_TOP , pgws_profile_queries_options ,
339
+ PGC_SUSET , 0 , shmem_enum_guc_check_hook , NULL , NULL );
303
340
304
341
if (!sample_cpu_found )
305
342
DefineCustomBoolVariable ("pg_wait_sampling.sample_cpu" ,
@@ -354,6 +391,8 @@ pgws_shmem_startup(void)
354
391
355
392
pgws_collector_hdr = shm_toc_allocate (toc , sizeof (CollectorShmqHeader ));
356
393
shm_toc_insert (toc , 0 , pgws_collector_hdr );
394
+ /* needed to please check_GUC_init */
395
+ pgws_collector_hdr -> profileQueries = PGWS_PROFILE_QUERIES_TOP ;
357
396
pgws_collector_mq = shm_toc_allocate (toc , COLLECTOR_QUEUE_SIZE );
358
397
shm_toc_insert (toc , 1 , pgws_collector_mq );
359
398
pgws_proc_queryids = shm_toc_allocate (toc ,
@@ -933,10 +972,15 @@ pgws_planner_hook(Query *parse,
933
972
int cursorOptions ,
934
973
ParamListInfo boundParams )
935
974
{
936
- PlannedStmt * result ;
937
- int i = MyProc - ProcGlobal -> allProcs ;
938
- if (nesting_level == 0 )
975
+ PlannedStmt * result ;
976
+ int i = MyProc - ProcGlobal -> allProcs ;
977
+ uint64 save_queryId = 0 ;
978
+
979
+ if (pgws_enabled (nesting_level ))
980
+ {
981
+ save_queryId = pgws_proc_queryids [i ];
939
982
pgws_proc_queryids [i ] = parse -> queryId ;
983
+ }
940
984
941
985
nesting_level ++ ;
942
986
PG_TRY ();
@@ -957,12 +1001,16 @@ pgws_planner_hook(Query *parse,
957
1001
nesting_level -- ;
958
1002
if (nesting_level == 0 )
959
1003
pgws_proc_queryids [i ] = UINT64CONST (0 );
1004
+ else if (pgws_enabled (nesting_level ))
1005
+ pgws_proc_queryids [i ] = save_queryId ;
960
1006
}
961
1007
PG_CATCH ();
962
1008
{
963
1009
nesting_level -- ;
964
1010
if (nesting_level == 0 )
965
1011
pgws_proc_queryids [i ] = UINT64CONST (0 );
1012
+ else if (pgws_enabled (nesting_level ))
1013
+ pgws_proc_queryids [i ] = save_queryId ;
966
1014
PG_RE_THROW ();
967
1015
}
968
1016
PG_END_TRY ();
@@ -977,9 +1025,8 @@ static void
977
1025
pgws_ExecutorStart (QueryDesc * queryDesc , int eflags )
978
1026
{
979
1027
int i = MyProc - ProcGlobal -> allProcs ;
980
- if (nesting_level == 0 )
1028
+ if (pgws_enabled ( nesting_level ) )
981
1029
pgws_proc_queryids [i ] = queryDesc -> plannedstmt -> queryId ;
982
-
983
1030
if (prev_ExecutorStart )
984
1031
prev_ExecutorStart (queryDesc , eflags );
985
1032
else
@@ -991,6 +1038,9 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
991
1038
ScanDirection direction ,
992
1039
uint64 count , bool execute_once )
993
1040
{
1041
+ int i = MyProc - ProcGlobal -> allProcs ;
1042
+ uint64 save_queryId = pgws_proc_queryids [i ];
1043
+
994
1044
nesting_level ++ ;
995
1045
PG_TRY ();
996
1046
{
@@ -999,10 +1049,18 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
999
1049
else
1000
1050
standard_ExecutorRun (queryDesc , direction , count , execute_once );
1001
1051
nesting_level -- ;
1052
+ if (nesting_level == 0 )
1053
+ pgws_proc_queryids [i ] = UINT64CONST (0 );
1054
+ else
1055
+ pgws_proc_queryids [i ] = save_queryId ;
1002
1056
}
1003
1057
PG_CATCH ();
1004
1058
{
1005
1059
nesting_level -- ;
1060
+ if (nesting_level == 0 )
1061
+ pgws_proc_queryids [i ] = UINT64CONST (0 );
1062
+ else
1063
+ pgws_proc_queryids [i ] = save_queryId ;
1006
1064
PG_RE_THROW ();
1007
1065
}
1008
1066
PG_END_TRY ();
@@ -1011,6 +1069,9 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
1011
1069
static void
1012
1070
pgws_ExecutorFinish (QueryDesc * queryDesc )
1013
1071
{
1072
+ int i = MyProc - ProcGlobal -> allProcs ;
1073
+ uint64 save_queryId = pgws_proc_queryids [i ];
1074
+
1014
1075
nesting_level ++ ;
1015
1076
PG_TRY ();
1016
1077
{
@@ -1019,10 +1080,15 @@ pgws_ExecutorFinish(QueryDesc *queryDesc)
1019
1080
else
1020
1081
standard_ExecutorFinish (queryDesc );
1021
1082
nesting_level -- ;
1083
+ if (nesting_level == 0 )
1084
+ pgws_proc_queryids [i ] = UINT64CONST (0 );
1085
+ else
1086
+ pgws_proc_queryids [i ] = save_queryId ;
1022
1087
}
1023
1088
PG_CATCH ();
1024
1089
{
1025
1090
nesting_level -- ;
1091
+ pgws_proc_queryids [i ] = save_queryId ;
1026
1092
PG_RE_THROW ();
1027
1093
}
1028
1094
PG_END_TRY ();
@@ -1061,10 +1127,14 @@ pgws_ProcessUtility(PlannedStmt *pstmt,
1061
1127
#endif
1062
1128
)
1063
1129
{
1064
- int i = MyProc - ProcGlobal -> allProcs ;
1130
+ int i = MyProc - ProcGlobal -> allProcs ;
1131
+ uint64 save_queryId = 0 ;
1065
1132
1066
- if (nesting_level == 0 )
1133
+ if (pgws_enabled (nesting_level ))
1134
+ {
1135
+ save_queryId = pgws_proc_queryids [i ];
1067
1136
pgws_proc_queryids [i ] = pstmt -> queryId ;
1137
+ }
1068
1138
1069
1139
nesting_level ++ ;
1070
1140
PG_TRY ();
@@ -1098,12 +1168,16 @@ pgws_ProcessUtility(PlannedStmt *pstmt,
1098
1168
nesting_level -- ;
1099
1169
if (nesting_level == 0 )
1100
1170
pgws_proc_queryids [i ] = UINT64CONST (0 );
1171
+ else if (pgws_enabled (nesting_level ))
1172
+ pgws_proc_queryids [i ] = save_queryId ;
1101
1173
}
1102
1174
PG_CATCH ();
1103
1175
{
1104
1176
nesting_level -- ;
1105
1177
if (nesting_level == 0 )
1106
1178
pgws_proc_queryids [i ] = UINT64CONST (0 );
1179
+ else if (pgws_enabled (nesting_level ))
1180
+ pgws_proc_queryids [i ] = save_queryId ;
1107
1181
PG_RE_THROW ();
1108
1182
}
1109
1183
PG_END_TRY ();
0 commit comments