@@ -43,9 +43,14 @@ static bool shmem_initialized = false;
43
43
44
44
/* Hooks */
45
45
static ExecutorStart_hook_type prev_ExecutorStart = NULL ;
46
+ static ExecutorRun_hook_type prev_ExecutorRun = NULL ;
47
+ static ExecutorFinish_hook_type prev_ExecutorFinish = NULL ;
46
48
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL ;
47
49
static planner_hook_type planner_hook_next = NULL ;
48
50
51
+ /* Current nesting depth of planner/Executor calls */
52
+ static int nesting_level = 0 ;
53
+
49
54
/* Pointers to shared memory objects */
50
55
shm_mq * pgws_collector_mq = NULL ;
51
56
uint64 * pgws_proc_queryids = NULL ;
@@ -67,6 +72,10 @@ static PlannedStmt *pgws_planner_hook(Query *parse,
67
72
#endif
68
73
int cursorOptions , ParamListInfo boundParams );
69
74
static void pgws_ExecutorStart (QueryDesc * queryDesc , int eflags );
75
+ static void pgws_ExecutorRun (QueryDesc * queryDesc ,
76
+ ScanDirection direction ,
77
+ uint64 count , bool execute_once );
78
+ static void pgws_ExecutorFinish (QueryDesc * queryDesc );
70
79
static void pgws_ExecutorEnd (QueryDesc * queryDesc );
71
80
72
81
/*
@@ -395,6 +404,10 @@ _PG_init(void)
395
404
planner_hook = pgws_planner_hook ;
396
405
prev_ExecutorStart = ExecutorStart_hook ;
397
406
ExecutorStart_hook = pgws_ExecutorStart ;
407
+ prev_ExecutorRun = ExecutorRun_hook ;
408
+ ExecutorRun_hook = pgws_ExecutorRun ;
409
+ prev_ExecutorFinish = ExecutorFinish_hook ;
410
+ ExecutorFinish_hook = pgws_ExecutorFinish ;
398
411
prev_ExecutorEnd = ExecutorEnd_hook ;
399
412
ExecutorEnd_hook = pgws_ExecutorEnd ;
400
413
}
@@ -865,23 +878,38 @@ pgws_planner_hook(Query *parse,
865
878
int cursorOptions ,
866
879
ParamListInfo boundParams )
867
880
{
881
+ PlannedStmt * result ;
868
882
int i = MyProc - ProcGlobal -> allProcs ;
869
- if (! pgws_proc_queryids [ i ] )
883
+ if (nesting_level == 0 )
870
884
pgws_proc_queryids [i ] = parse -> queryId ;
871
885
872
886
/* Invoke original hook if needed */
873
- if (planner_hook_next )
874
- return planner_hook_next (parse ,
887
+ nesting_level ++ ;
888
+ PG_TRY ();
889
+ {
890
+ if (planner_hook_next )
891
+ result = planner_hook_next (parse ,
875
892
#if PG_VERSION_NUM >= 130000
876
- query_string ,
893
+ query_string ,
877
894
#endif
878
- cursorOptions , boundParams );
879
-
880
- return standard_planner (parse ,
895
+ cursorOptions , boundParams );
896
+ else
897
+ result = standard_planner (parse ,
881
898
#if PG_VERSION_NUM >= 130000
882
- query_string ,
899
+ query_string ,
883
900
#endif
884
- cursorOptions , boundParams );
10000
901
+ cursorOptions , boundParams );
902
+ }
903
+ PG_CATCH ();
904
+ {
905
+ nesting_level -- ;
906
+ if (nesting_level == 0 )
907
+ pgws_proc_queryids [i ] = UINT64CONST (0 );
908
+ PG_RE_THROW ();
909
+ }
910
+ PG_END_TRY ();
911
+
912
+ return result ;
885
913
}
886
914
887
915
/*
@@ -891,7 +919,7 @@ static void
891
919
pgws_ExecutorStart (QueryDesc * queryDesc , int eflags )
892
920
{
893
921
int i = MyProc - ProcGlobal -> allProcs ;
894
- if (! pgws_proc_queryids [ i ] )
922
+ if (nesting_level == 0 )
895
923
pgws_proc_queryids [i ] = queryDesc -> plannedstmt -> queryId ;
896
924
897
925
if (prev_ExecutorStart )
@@ -900,13 +928,55 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
900
928
standard_ExecutorStart (queryDesc , eflags );
901
929
}
902
930
931
+ static void
932
+ pgws_ExecutorRun (QueryDesc * queryDesc ,
933
+ ScanDirection direction ,
934
+ uint64 count , bool execute_once )
935
+ {
936
+ nesting_level ++ ;
937
+ PG_TRY ();
938
+ {
939
+ if (prev_ExecutorRun )
940
+ prev_ExecutorRun (queryDesc , direction , count , execute_once );
941
+ else
942
+ standard_ExecutorRun (queryDesc , direction , count , execute_once );
943
+ }
944
+ PG_CATCH ();
945
+ {
946
+ nesting_level -- ;
947
+ PG_RE_THROW ();
948
+ }
949
+ PG_END_TRY ();
950
+ }
951
+
952
+ static void
953
+ pgws_ExecutorFinish (QueryDesc * queryDesc )
954
+ {
955
+ nesting_level ++ ;
956
+ PG_TRY ();
957
+ {
958
+ if (prev_ExecutorFinish )
959
+ prev_ExecutorFinish (queryDesc );
960
+ else
961
+ standard_ExecutorFinish (queryDesc );
962
+ }
963
+ PG_CATCH ();
964
+ {
965
+ nesting_level -- ;
966
+ PG_RE_THROW ();
967
+ }
968
+ PG_END_TRY ();
969
+ }
970
+
903
971
/*
904
972
* ExecutorEnd hook: clear queryId
905
973
*/
906
974
static void
907
975
pgws_ExecutorEnd (QueryDesc * queryDesc )
908
976
{
909
- pgws_proc_queryids [MyProc - ProcGlobal -> allProcs ] = UINT64CONST (0 );
977
+ int i = MyProc - ProcGlobal -> allProcs ;
978
+ if (nesting_level == 0 )
979
+ pgws_proc_queryids [i ] = UINT64CONST (0 );
910
980
911
981
if (prev_ExecutorEnd )
912
982
prev_ExecutorEnd (queryDesc );
0 commit comments