Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add plan text to query one #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions pg_stat_sql_plans.c
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,7 @@ pgssp_store(const char *query, uint64 queryId,
}
else
{

/* this part comes from auto_explain
* ExplainPrintPlan has been replaced by
* ExplainOnePlan to be able to be calculated
Expand Down Expand Up @@ -1508,14 +1509,36 @@ pgssp_store(const char *query, uint64 queryId,
{
Size query_offset;
int gc_count;
bool stored;
bool stored = false;
bool do_gc;

if (planId != UINT64CONST(0) && planId != UINT64CONST(1) && planId != UINT64CONST(-1) && pgssp_explain)
{
/* add plan to query text */
char *query_with_plan;
query_with_plan = malloc(query_len + es->str->len + 1 + 1);
strcpy(query_with_plan, query);
strcat(query_with_plan, "\n");
strcat(query_with_plan, es->str->data);
query=query_with_plan;
query_len=strlen(query_with_plan);
// free(query_with_plan);
}

/* Append new query text to file with only shared lock held */
stored = qtext_store( query, query_len,
&query_offset, &gc_count);

//PLY this generates many duplicated query texts in external file (on MSYS2)
// when tested with pgbench -c50 -t5 (a kind of query storm ...)
// In an ideal world:
// - there should be no action between first hash_search and exclusive lock promotion,
// - qtext_store should be executed AFTER hashtable entry creation,
// outside exclusive lock
// query length and offset updated at the same time than counters
// In real life, keep it as is or remove it there (to run it under exclusive lock)
// that may fix initial issue, but reduce performances in the case of many distinct queries.

/*
* Determine whether we need to garbage collect external query texts
* while the shared lock is still held. This micro-optimization
Expand Down Expand Up @@ -1548,17 +1571,7 @@ pgssp_store(const char *query, uint64 queryId,
/* If needed, perform garbage collection while exclusive lock held */
if (do_gc)
gc_qtexts();

if (planId != UINT64CONST(0) && planId != UINT64CONST(1) && planId != UINT64CONST(-1) && pgssp_explain)
{
/*
* Plan is only logged one time for each queryid / planId
*/
ereport(LOG,
(errmsg("qpid: %lld queryid: %lld planid: %lld plan:\n%s",
(long long)qpId, (long long)queryId, (long long)planId, es->str->data),
errhidecontext(true), errhidestmt(true)));
}

}


Expand Down