Summary:
Today, we set and reset query id inside the executor hooks.
Most of the RPCs are sent when the control is inside the
executor hooks, but there are some RPCs which can be sent
outside of executor hooks in some cases, and as a result
all the wait events related to that RPC will have the
default query id of 5 which is incorrect.
We maintain a stack of query ids because there can be multiple
query ids for nested queries, triggers etc.
This diff fixes the problem by modifying the way query ids
are pushed and popped from the stack --
Pop: If this is the last query id, don't pop and mark it
to be popped the next time we push into the stack.
Push: If a query id was marked to be popped, we first pop
from the stack and then push the new query id.
If no query id is pushed after marking a query id to be popped,
then we pop it when the query finishes, i.e. when resetting
root request id.
This way we make sure to always have a valid query id from the
time it is first set, until the query finishes.
We also need to call EnableQueryId() in YbAshInit, because in
initdb mode, EnableQueryId() is not called from
pg_stat_statements. So query ids are not calculated as no one
requested it, and the newly added assert in
YbAshNestedQueryIdStackPop fails because the query id
in the stack is 0. Requesting the query id to be calculated
using EnableQueryId() fixes this. EnableQueryId() is also
idempotent, so it can be called multiple times.
Jira: DB-13768
Test Plan:
./yb_build.sh --java-test TestYbAsh#testQueryId5Samples
./yb_build.sh --cxx-test pg_ash-test --gtest_filter PgAshSingleNode.TestFinishTransactionRPCs
Verified the tests fail without the changes
Reviewers: hbhanawat, ishan.chhangani
Reviewed By: ishan.chhangani
Subscribers: yql
Differential Revision: https://phorge.dev.yugabyte.com/D44715