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

Prevent abnormal termination due to plan text read failure when `pg_s… #33

Open
wants to merge 4 commits 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
36 changes: 33 additions & 3 deletions pg_store_plans.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ _PG_init(void)
"Sets the maximum number of plans tracked by pg_store_plans.",
NULL,
&store_size,
1000,
5000,
100,
INT_MAX,
PGC_POSTMASTER,
Expand Down Expand Up @@ -471,7 +471,7 @@ _PG_init(void)
"Minimum duration to record plan in milliseconds.",
NULL,
&min_duration,
0,
10,
0,
INT_MAX,
PGC_SUSET,
Expand Down Expand Up @@ -517,7 +517,7 @@ _PG_init(void)
"Log timings.",
NULL,
&log_timing,
true,
false,
PGC_SUSET,
0,
NULL,
Expand Down Expand Up @@ -738,6 +738,15 @@ pgsp_shmem_startup(void)
pgver != PGSP_PG_MAJOR_VERSION)
goto data_error;

/* check if num is out of range */
if (num < 0 || num > store_size)
{
ereport(LOG,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Invalid number of entries in file: %d", num)));
goto data_error;
}

for (i = 0; i < num; i++)
{
pgspEntry temp;
Expand Down Expand Up @@ -930,6 +939,10 @@ pgsp_shmem_shutdown(int code, Datum arg)
/* Unlink query-texts file; it's not needed while shutdown */
unlink(PGSP_TEXT_FILE);

if (pbuffer){
free(pbuffer); // or free(pbuffer)
pbuffer = NULL;
}
return;

error:
Expand All @@ -940,6 +953,10 @@ pgsp_shmem_shutdown(int code, Datum arg)
if (file)
FreeFile(file);
unlink(PGSP_DUMP_FILE ".tmp");
if (pbuffer){
free(pbuffer); // or free(pbuffer)
pbuffer = NULL;
}
}


Expand Down Expand Up @@ -1356,8 +1373,13 @@ pgsp_store(char *plan, queryid_t queryId,
e->counters.temp_blks_read += bufusage->temp_blks_read;
e->counters.temp_blks_written += bufusage->temp_blks_written;

#if PG_VERSION_NUM >= 170000 /* compatible with version 17 */
e->counters.blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->local_blk_read_time);
e->counters.blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->local_blk_write_time);
#else
e->counters.blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->blk_read_time);
e->counters.blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->blk_write_time);
#endif

#if PG_VERSION_NUM >= 150000
e->counters.temp_blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->temp_blk_read_time);
Expand Down Expand Up @@ -1579,6 +1601,9 @@ pg_store_plans_internal(FunctionCallInfo fcinfo,
else
pstr = SHMEM_PLAN_PTR(entry);

if (pstr == NULL)
continue; /* Ignore any entries with bogus texts */

switch (plan_format)
{
case PLAN_FORMAT_TEXT:
Expand Down Expand Up @@ -1682,6 +1707,11 @@ pg_store_plans_internal(FunctionCallInfo fcinfo,

/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);

if (pbuffer){
free(pbuffer); // or free(pbuffer)
pbuffer = NULL;
}
}

/* Number of output arguments (columns) for pg_stat_statements_info */
Expand Down