Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3fdca70

Browse files
authored
Merge pull request#78 from postgrespro/track_top_level_queryid
Make it so subqueries do not rewrite top-level queryId
2 parents7c7c716 +0b5c61b commit3fdca70

File tree

1 file changed

+91
-26
lines changed

1 file changed

+91
-26
lines changed

‎pg_wait_sampling.c‎

Lines changed: 91 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ static bool shmem_initialized = false;
4343

4444
/* Hooks */
4545
staticExecutorStart_hook_typeprev_ExecutorStart=NULL;
46+
staticExecutorRun_hook_typeprev_ExecutorRun=NULL;
47+
staticExecutorFinish_hook_typeprev_ExecutorFinish=NULL;
4648
staticExecutorEnd_hook_typeprev_ExecutorEnd=NULL;
4749
staticplanner_hook_typeplanner_hook_next=NULL;
4850

51+
/* Current nesting depth of planner/Executor calls */
52+
staticintnesting_level=0;
53+
4954
/* Pointers to shared memory objects */
5055
shm_mq*pgws_collector_mq=NULL;
5156
uint64*pgws_proc_queryids=NULL;
@@ -67,6 +72,10 @@ static PlannedStmt *pgws_planner_hook(Query *parse,
6772
#endif
6873
intcursorOptions,ParamListInfoboundParams);
6974
staticvoidpgws_ExecutorStart(QueryDesc*queryDesc,inteflags);
75+
staticvoidpgws_ExecutorRun(QueryDesc*queryDesc,
76+
ScanDirectiondirection,
77+
uint64count,boolexecute_once);
78+
staticvoidpgws_ExecutorFinish(QueryDesc*queryDesc);
7079
staticvoidpgws_ExecutorEnd(QueryDesc*queryDesc);
7180

7281
/*
@@ -395,6 +404,10 @@ _PG_init(void)
395404
planner_hook=pgws_planner_hook;
396405
prev_ExecutorStart=ExecutorStart_hook;
397406
ExecutorStart_hook=pgws_ExecutorStart;
407+
prev_ExecutorRun=ExecutorRun_hook;
408+
ExecutorRun_hook=pgws_ExecutorRun;
409+
prev_ExecutorFinish=ExecutorFinish_hook;
410+
ExecutorFinish_hook=pgws_ExecutorFinish;
398411
prev_ExecutorEnd=ExecutorEnd_hook;
399412
ExecutorEnd_hook=pgws_ExecutorEnd;
400413
}
@@ -865,27 +878,41 @@ pgws_planner_hook(Query *parse,
865878
intcursorOptions,
866879
ParamListInfoboundParams)
867880
{
868-
if (MyProc)
869-
{
870-
inti=MyProc-ProcGlobal->allProcs;
871-
if (!pgws_proc_queryids[i])
872-
pgws_proc_queryids[i]=parse->queryId;
881+
PlannedStmt*result;
882+
inti=MyProc-ProcGlobal->allProcs;
883+
if (nesting_level==0)
884+
pgws_proc_queryids[i]=parse->queryId;
873885

874-
}
875-
876-
/* Invoke original hook if needed */
877-
if (planner_hook_next)
878-
returnplanner_hook_next(parse,
886+
nesting_level++;
887+
PG_TRY();
888+
{
889+
/* Invoke original hook if needed */
890+
if (planner_hook_next)
891+
result=planner_hook_next(parse,
879892
#ifPG_VERSION_NUM >=130000
880-
query_string,
893+
query_string,
881894
#endif
882-
cursorOptions,boundParams);
883-
884-
returnstandard_planner(parse,
895+
cursorOptions,boundParams);
896+
else
897+
result=standard_planner(parse,
885898
#ifPG_VERSION_NUM >=130000
886-
query_string,
899+
query_string,
887900
#endif
888-
cursorOptions,boundParams);
901+
cursorOptions,boundParams);
902+
nesting_level--;
903+
if (nesting_level==0)
904+
pgws_proc_queryids[i]=UINT64CONST(0);
905+
}
906+
PG_CATCH();
907+
{
908+
nesting_level--;
909+
if (nesting_level==0)
910+
pgws_proc_queryids[i]=UINT64CONST(0);
911+
PG_RE_THROW();
912+
}
913+
PG_END_TRY();
914+
915+
returnresult;
889916
}
890917

891918
/*
@@ -894,29 +921,67 @@ pgws_planner_hook(Query *parse,
894921
staticvoid
895922
pgws_ExecutorStart(QueryDesc*queryDesc,inteflags)
896923
{
897-
inti;
898-
899-
if (MyProc)
900-
{
901-
i=MyProc-ProcGlobal->allProcs;
902-
if (!pgws_proc_queryids[i])
903-
pgws_proc_queryids[i]=queryDesc->plannedstmt->queryId;
904-
}
924+
inti=MyProc-ProcGlobal->allProcs;
925+
if (nesting_level==0)
926+
pgws_proc_queryids[i]=queryDesc->plannedstmt->queryId;
905927

906928
if (prev_ExecutorStart)
907929
prev_ExecutorStart(queryDesc,eflags);
908930
else
909931
standard_ExecutorStart(queryDesc,eflags);
910932
}
911933

934+
staticvoid
935+
pgws_ExecutorRun(QueryDesc*queryDesc,
936+
ScanDirectiondirection,
937+
uint64count,boolexecute_once)
938+
{
939+
nesting_level++;
940+
PG_TRY();
941+
{
942+
if (prev_ExecutorRun)
943+
prev_ExecutorRun(queryDesc,direction,count,execute_once);
944+
else
945+
standard_ExecutorRun(queryDesc,direction,count,execute_once);
946+
nesting_level--;
947+
}
948+
PG_CATCH();
949+
{
950+
nesting_level--;
951+
PG_RE_THROW();
952+
}
953+
PG_END_TRY();
954+
}
955+
956+
staticvoid
957+
pgws_ExecutorFinish(QueryDesc*queryDesc)
958+
{
959+
nesting_level++;
960+
PG_TRY();
961+
{
962+
if (prev_ExecutorFinish)
963+
prev_ExecutorFinish(queryDesc);
964+
else
965+
standard_ExecutorFinish(queryDesc);
966+
nesting_level--;
967+
}
968+
PG_CATCH();
969+
{
970+
nesting_level--;
971+
PG_RE_THROW();
972+
}
973+
PG_END_TRY();
974+
}
975+
912976
/*
913977
* ExecutorEnd hook: clear queryId
914978
*/
915979
staticvoid
916980
pgws_ExecutorEnd(QueryDesc*queryDesc)
917981
{
918-
if (MyProc)
919-
pgws_proc_queryids[MyProc-ProcGlobal->allProcs]=UINT64CONST(0);
982+
inti=MyProc-ProcGlobal->allProcs;
983+
if (nesting_level==0)
984+
pgws_proc_queryids[i]=UINT64CONST(0);
920985

921986
if (prev_ExecutorEnd)
922987
prev_ExecutorEnd(queryDesc);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp