diff options
author | Fujii Masao <fujii@postgresql.org> | 2020-11-26 21:18:05 +0900 |
---|---|---|
committer | Fujii Masao <fujii@postgresql.org> | 2020-11-26 21:18:05 +0900 |
commit | 9fbc3f318d039c3e1e8614c38e40843cf8fcffde (patch) | |
tree | d23518e6b4de9c0ddf86cfc40541ecc26d0e82e8 /contrib/pg_stat_statements/pg_stat_statements.c | |
parent | 4a36eab79a193700b7b65baf6c09c795c90c02c6 (diff) | |
download | postgresql-9fbc3f318d039c3e1e8614c38e40843cf8fcffde.tar.gz postgresql-9fbc3f318d039c3e1e8614c38e40843cf8fcffde.zip |
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
Diffstat (limited to 'contrib/pg_stat_statements/pg_stat_statements.c')
-rw-r--r-- | contrib/pg_stat_statements/pg_stat_statements.c | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index dd963c4644a..70cfdb2c9d1 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -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; @@ -194,6 +194,14 @@ typedef struct Counters } Counters; /* + * Global statistics for pg_stat_statements + */ +typedef struct pgssGlobalStats +{ + int64 dealloc; /* # of times entries were deallocated */ +} pgssGlobalStats; + +/* * Statistics per statement * * Note: in event of a failure in garbage collection of the query text file, @@ -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; @@ -1864,6 +1883,26 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo, } /* + * 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. */ static Size @@ -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? */ |