@@ -95,6 +95,35 @@ static void pgws_ProcessUtility(PlannedStmt *pstmt,
9595#endif
9696);
9797
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+
98127/*
99128 * Calculate max processes count.
100129 *
@@ -185,6 +214,14 @@ shmem_int_guc_check_hook(int *newval, void **extra, GucSource source)
185214return true;
186215}
187216
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+
188225static bool
189226shmem_bool_guc_check_hook (bool * newval ,void * * extra ,GucSource source )
190227{
@@ -260,8 +297,8 @@ setup_gucs()
260297else if (!strcmp (name ,"pg_wait_sampling.profile_queries" ))
261298{
262299profile_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 ;
265302}
266303else if (!strcmp (name ,"pg_wait_sampling.sample_cpu" ))
267304{
@@ -296,10 +333,10 @@ setup_gucs()
296333PGC_SUSET ,0 ,shmem_bool_guc_check_hook ,NULL ,NULL );
297334
298335if (!profile_queries_found )
299- DefineCustomBoolVariable ("pg_wait_sampling.profile_queries" ,
336+ DefineCustomEnumVariable ("pg_wait_sampling.profile_queries" ,
300337"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 );
303340
304341if (!sample_cpu_found )
305342DefineCustomBoolVariable ("pg_wait_sampling.sample_cpu" ,
@@ -354,6 +391,8 @@ pgws_shmem_startup(void)
354391
355392pgws_collector_hdr = shm_toc_allocate (toc ,sizeof (CollectorShmqHeader ));
356393shm_toc_insert (toc ,0 ,pgws_collector_hdr );
394+ /* needed to please check_GUC_init */
395+ pgws_collector_hdr -> profileQueries = PGWS_PROFILE_QUERIES_TOP ;
357396pgws_collector_mq = shm_toc_allocate (toc ,COLLECTOR_QUEUE_SIZE );
358397shm_toc_insert (toc ,1 ,pgws_collector_mq );
359398pgws_proc_queryids = shm_toc_allocate (toc ,
@@ -933,10 +972,15 @@ pgws_planner_hook(Query *parse,
933972int cursorOptions ,
934973ParamListInfo boundParams )
935974{
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 ];
939982pgws_proc_queryids [i ]= parse -> queryId ;
983+ }
940984
941985nesting_level ++ ;
942986PG_TRY ();
@@ -957,12 +1001,16 @@ pgws_planner_hook(Query *parse,
9571001nesting_level -- ;
9581002if (nesting_level == 0 )
9591003pgws_proc_queryids [i ]= UINT64CONST (0 );
1004+ else if (pgws_enabled (nesting_level ))
1005+ pgws_proc_queryids [i ]= save_queryId ;
9601006}
9611007PG_CATCH ();
9621008{
9631009nesting_level -- ;
9641010if (nesting_level == 0 )
9651011pgws_proc_queryids [i ]= UINT64CONST (0 );
1012+ else if (pgws_enabled (nesting_level ))
1013+ pgws_proc_queryids [i ]= save_queryId ;
9661014PG_RE_THROW ();
9671015}
9681016PG_END_TRY ();
@@ -977,9 +1025,8 @@ static void
9771025pgws_ExecutorStart (QueryDesc * queryDesc ,int eflags )
9781026{
9791027int i = MyProc - ProcGlobal -> allProcs ;
980- if (nesting_level == 0 )
1028+ if (pgws_enabled ( nesting_level ) )
9811029pgws_proc_queryids [i ]= queryDesc -> plannedstmt -> queryId ;
982-
9831030if (prev_ExecutorStart )
9841031prev_ExecutorStart (queryDesc ,eflags );
9851032else
@@ -991,6 +1038,9 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
9911038ScanDirection direction ,
9921039uint64 count ,bool execute_once )
9931040{
1041+ int i = MyProc - ProcGlobal -> allProcs ;
1042+ uint64 save_queryId = pgws_proc_queryids [i ];
1043+
9941044nesting_level ++ ;
9951045PG_TRY ();
9961046{
@@ -999,10 +1049,18 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
9991049else
10001050standard_ExecutorRun (queryDesc ,direction ,count ,execute_once );
10011051nesting_level -- ;
1052+ if (nesting_level == 0 )
1053+ pgws_proc_queryids [i ]= UINT64CONST (0 );
1054+ else
1055+ pgws_proc_queryids [i ]= save_queryId ;
10021056}
10031057PG_CATCH ();
10041058{
10051059nesting_level -- ;
1060+ if (nesting_level == 0 )
1061+ pgws_proc_queryids [i ]= UINT64CONST (0 );
1062+ else
1063+ pgws_proc_queryids [i ]= save_queryId ;
10061064PG_RE_THROW ();
10071065}
10081066PG_END_TRY ();
@@ -1011,6 +1069,9 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
10111069static void
10121070pgws_ExecutorFinish (QueryDesc * queryDesc )
10131071{
1072+ int i = MyProc - ProcGlobal -> allProcs ;
1073+ uint64 save_queryId = pgws_proc_queryids [i ];
1074+
10141075nesting_level ++ ;
10151076PG_TRY ();
10161077{
@@ -1019,10 +1080,15 @@ pgws_ExecutorFinish(QueryDesc *queryDesc)
10191080else
10201081standard_ExecutorFinish (queryDesc );
10211082nesting_level -- ;
1083+ if (nesting_level == 0 )
1084+ pgws_proc_queryids [i ]= UINT64CONST (0 );
1085+ else
1086+ pgws_proc_queryids [i ]= save_queryId ;
10221087}
10231088PG_CATCH ();
10241089{
10251090nesting_level -- ;
1091+ pgws_proc_queryids [i ]= save_queryId ;
10261092PG_RE_THROW ();
10271093}
10281094PG_END_TRY ();
@@ -1061,10 +1127,14 @@ pgws_ProcessUtility(PlannedStmt *pstmt,
10611127#endif
10621128)
10631129{
1064- int i = MyProc - ProcGlobal -> allProcs ;
1130+ int i = MyProc - ProcGlobal -> allProcs ;
1131+ uint64 save_queryId = 0 ;
10651132
1066- if (nesting_level == 0 )
1133+ if (pgws_enabled (nesting_level ))
1134+ {
1135+ save_queryId = pgws_proc_queryids [i ];
10671136pgws_proc_queryids [i ]= pstmt -> queryId ;
1137+ }
10681138
10691139nesting_level ++ ;
10701140PG_TRY ();
@@ -1098,12 +1168,16 @@ pgws_ProcessUtility(PlannedStmt *pstmt,
10981168nesting_level -- ;
10991169if (nesting_level == 0 )
11001170pgws_proc_queryids [i ]= UINT64CONST (0 );
1171+ else if (pgws_enabled (nesting_level ))
1172+ pgws_proc_queryids [i ]= save_queryId ;
11011173}
11021174PG_CATCH ();
11031175{
11041176nesting_level -- ;
11051177if (nesting_level == 0 )
11061178pgws_proc_queryids [i ]= UINT64CONST (0 );
1179+ else if (pgws_enabled (nesting_level ))
1180+ pgws_proc_queryids [i ]= save_queryId ;
11071181PG_RE_THROW ();
11081182}
11091183PG_END_TRY ();