mirror of
https://github.com/postgres/postgres.git
synced 2025-06-11 20:28:21 +03:00
pg_stat_statements: Track number of times pgss entries were deallocated.
If more distinct statements than pg_stat_statements.max are observed, pg_stat_statements entries about the least-executed statements are deallocated. This commit enables us to track the total number of times those entries were deallocated. That number can be viewed in the pg_stat_statements_info view that this commit adds. It's useful when tuning pg_stat_statements.max parameter. If it's high, i.e., the entries are deallocated very frequently, which might cause the performance regression and we can increase pg_stat_statements.max to avoid those frequent deallocations. The pg_stat_statements_info view is intended to display the statistics of pg_stat_statements module itself. Currently it has only one column "dealloc" indicating the number of times entries were deallocated. But an upcoming patch will add other columns (for example, the time at which pg_stat_statements statistics were last reset) into the view. Author: Katsuragi Yuta, Yuki Seino Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/0d9f1107772cf5c3f954e985464c7298@oss.nttdata.com
This commit is contained in:
@ -98,7 +98,7 @@ PG_MODULE_MAGIC;
|
||||
#define PGSS_TEXT_FILE PG_STAT_TMP_DIR "/pgss_query_texts.stat"
|
||||
|
||||
/* Magic number identifying the stats file format */
|
||||
static const uint32 PGSS_FILE_HEADER = 0x20171004;
|
||||
static const uint32 PGSS_FILE_HEADER = 0x20201126;
|
||||
|
||||
/* PostgreSQL major version number, changes in which invalidate all entries */
|
||||
static const uint32 PGSS_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;
|
||||
@ -193,6 +193,14 @@ typedef struct Counters
|
||||
uint64 wal_bytes; /* total amount of WAL bytes generated */
|
||||
} Counters;
|
||||
|
||||
/*
|
||||
* Global statistics for pg_stat_statements
|
||||
*/
|
||||
typedef struct pgssGlobalStats
|
||||
{
|
||||
int64 dealloc; /* # of times entries were deallocated */
|
||||
} pgssGlobalStats;
|
||||
|
||||
/*
|
||||
* Statistics per statement
|
||||
*
|
||||
@ -222,6 +230,7 @@ typedef struct pgssSharedState
|
||||
Size extent; /* current extent of query file */
|
||||
int n_writers; /* number of active writers to query file */
|
||||
int gc_count; /* query file garbage collection cycle count */
|
||||
pgssGlobalStats stats; /* global statistics for pgss */
|
||||
} pgssSharedState;
|
||||
|
||||
/*
|
||||
@ -327,6 +336,7 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_2);
|
||||
PG_FUNCTION_INFO_V1(pg_stat_statements_1_3);
|
||||
PG_FUNCTION_INFO_V1(pg_stat_statements_1_8);
|
||||
PG_FUNCTION_INFO_V1(pg_stat_statements);
|
||||
PG_FUNCTION_INFO_V1(pg_stat_statements_info);
|
||||
|
||||
static void pgss_shmem_startup(void);
|
||||
static void pgss_shmem_shutdown(int code, Datum arg);
|
||||
@ -554,6 +564,7 @@ pgss_shmem_startup(void)
|
||||
pgss->extent = 0;
|
||||
pgss->n_writers = 0;
|
||||
pgss->gc_count = 0;
|
||||
pgss->stats.dealloc = 0;
|
||||
}
|
||||
|
||||
memset(&info, 0, sizeof(info));
|
||||
@ -673,6 +684,10 @@ pgss_shmem_startup(void)
|
||||
entry->counters = temp.counters;
|
||||
}
|
||||
|
||||
/* Read global statistics for pg_stat_statements */
|
||||
if (fread(&pgss->stats, sizeof(pgssGlobalStats), 1, file) != 1)
|
||||
goto read_error;
|
||||
|
||||
pfree(buffer);
|
||||
FreeFile(file);
|
||||
FreeFile(qfile);
|
||||
@ -794,6 +809,10 @@ pgss_shmem_shutdown(int code, Datum arg)
|
||||
}
|
||||
}
|
||||
|
||||
/* Dump global statistics for pg_stat_statements */
|
||||
if (fwrite(&pgss->stats, sizeof(pgssGlobalStats), 1, file) != 1)
|
||||
goto error;
|
||||
|
||||
free(qbuffer);
|
||||
qbuffer = NULL;
|
||||
|
||||
@ -1863,6 +1882,26 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
|
||||
tuplestore_donestoring(tupstore);
|
||||
}
|
||||
|
||||
/*
|
||||
* Return statistics of pg_stat_statements.
|
||||
*/
|
||||
Datum
|
||||
pg_stat_statements_info(PG_FUNCTION_ARGS)
|
||||
{
|
||||
pgssGlobalStats stats;
|
||||
|
||||
/* Read global statistics for pg_stat_statements */
|
||||
{
|
||||
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
|
||||
|
||||
SpinLockAcquire(&s->mutex);
|
||||
stats = s->stats;
|
||||
SpinLockRelease(&s->mutex);
|
||||
}
|
||||
|
||||
PG_RETURN_INT64(stats.dealloc);
|
||||
}
|
||||
|
||||
/*
|
||||
* Estimate shared memory space needed.
|
||||
*/
|
||||
@ -2018,6 +2057,15 @@ entry_dealloc(void)
|
||||
}
|
||||
|
||||
pfree(entries);
|
||||
|
||||
/* Increment the number of times entries are deallocated */
|
||||
{
|
||||
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
|
||||
|
||||
SpinLockAcquire(&s->mutex);
|
||||
s->stats.dealloc += 1;
|
||||
SpinLockRelease(&s->mutex);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -2504,6 +2552,15 @@ entry_reset(Oid userid, Oid dbid, uint64 queryid)
|
||||
hash_search(pgss_hash, &entry->key, HASH_REMOVE, NULL);
|
||||
num_remove++;
|
||||
}
|
||||
|
||||
/* Reset global statistics for pg_stat_statements */
|
||||
{
|
||||
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
|
||||
|
||||
SpinLockAcquire(&s->mutex);
|
||||
s->stats.dealloc = 0;
|
||||
SpinLockRelease(&s->mutex);
|
||||
}
|
||||
}
|
||||
|
||||
/* All entries are removed? */
|
||||
|
Reference in New Issue
Block a user