From e24c3dd20f56656d5d3602492096a30939e7c649 Mon Sep 17 00:00:00 2001 From: kayform Date: Fri, 26 Apr 2024 10:52:10 +0900 Subject: [PATCH 1/4] Prevent abnormal termination due to plan text read failure when `pg_store_plans.plan_storage=file` and `pgsp_plan_texts.stat` is deleted. --- pg_store_plans.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pg_store_plans.c b/pg_store_plans.c index a8f01ca..7233090 100644 --- a/pg_store_plans.c +++ b/pg_store_plans.c @@ -1579,6 +1579,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: From e92e7c950ccf555c371f5da64d34a7cf0cee2f80 Mon Sep 17 00:00:00 2001 From: experdb Date: Wed, 15 Jan 2025 13:06:12 +0900 Subject: [PATCH 2/4] modified default values log_timing from on to off, max: 1000 to 5000, min_duration 0 to 10 --- pg_store_plans.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pg_store_plans.c b/pg_store_plans.c index 7233090..fdf6e8c 100644 --- a/pg_store_plans.c +++ b/pg_store_plans.c @@ -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, @@ -471,7 +471,7 @@ _PG_init(void) "Minimum duration to record plan in milliseconds.", NULL, &min_duration, - 0, + 10, 0, INT_MAX, PGC_SUSET, @@ -517,7 +517,7 @@ _PG_init(void) "Log timings.", NULL, &log_timing, - true, + false, PGC_SUSET, 0, NULL, From fb41a5848a8f2b0ab3ccf4abd561a3b68f88d03f Mon Sep 17 00:00:00 2001 From: "kayform@" Date: Fri, 14 Feb 2025 14:50:27 +0900 Subject: [PATCH 3/4] Fix suspected memory leak using by Coverity --- pg_store_plans.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pg_store_plans.c b/pg_store_plans.c index fdf6e8c..3861592 100644 --- a/pg_store_plans.c +++ b/pg_store_plans.c @@ -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; @@ -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: @@ -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; + } } @@ -1685,6 +1702,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 */ From 1adb4a318cabf10f1aebaf1382f4d761156bc9d6 Mon Sep 17 00:00:00 2001 From: kayform Date: Fri, 14 Feb 2025 15:41:01 +0900 Subject: [PATCH 4/4] competable with v17 --- pg_store_plans.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pg_store_plans.c b/pg_store_plans.c index 3861592..c5fda3f 100644 --- a/pg_store_plans.c +++ b/pg_store_plans.c @@ -1373,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);