Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9fbc3f3

Browse files
committed
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
1 parent 4a36eab commit 9fbc3f3

File tree

8 files changed

+155
-8
lines changed

8 files changed

+155
-8
lines changed

contrib/pg_stat_statements/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ OBJS = \
66
pg_stat_statements.o
77

88
EXTENSION = pg_stat_statements
9-
DATA = pg_stat_statements--1.4.sql \
9+
DATA = pg_stat_statements--1.4.sql pg_stat_statements--1.8--1.9.sql \
1010
pg_stat_statements--1.7--1.8.sql pg_stat_statements--1.6--1.7.sql \
1111
pg_stat_statements--1.5--1.6.sql pg_stat_statements--1.4--1.5.sql \
1212
pg_stat_statements--1.3--1.4.sql pg_stat_statements--1.2--1.3.sql \

contrib/pg_stat_statements/expected/pg_stat_statements.out

+15
Original file line numberDiff line numberDiff line change
@@ -861,4 +861,19 @@ SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE
861861
SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 1 | 0 | 0
862862
(6 rows)
863863

864+
--
865+
-- access to pg_stat_statements_info view
866+
--
867+
SELECT pg_stat_statements_reset();
868+
pg_stat_statements_reset
869+
--------------------------
870+
871+
(1 row)
872+
873+
SELECT dealloc FROM pg_stat_statements_info;
874+
dealloc
875+
---------
876+
0
877+
(1 row)
878+
864879
DROP EXTENSION pg_stat_statements;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* contrib/pg_stat_statements/pg_stat_statements--1.8--1.9.sql */
2+
3+
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
4+
\echo Use "ALTER EXTENSION pg_stat_statements UPDATE TO '1.9'" to load this file. \quit
5+
6+
--- Define pg_stat_statements_info
7+
CREATE FUNCTION pg_stat_statements_info(
8+
OUT dealloc bigint
9+
)
10+
RETURNS bigint
11+
AS 'MODULE_PATHNAME'
12+
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
13+
14+
CREATE VIEW pg_stat_statements_info AS
15+
SELECT * FROM pg_stat_statements_info();
16+
17+
GRANT SELECT ON pg_stat_statements_info TO PUBLIC;

contrib/pg_stat_statements/pg_stat_statements.c

+58-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ PG_MODULE_MAGIC;
9898
#define PGSS_TEXT_FILE PG_STAT_TMP_DIR "/pgss_query_texts.stat"
9999

100100
/* Magic number identifying the stats file format */
101-
static const uint32 PGSS_FILE_HEADER = 0x20171004;
101+
static const uint32 PGSS_FILE_HEADER = 0x20201126;
102102

103103
/* PostgreSQL major version number, changes in which invalidate all entries */
104104
static const uint32 PGSS_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;
@@ -193,6 +193,14 @@ typedef struct Counters
193193
uint64 wal_bytes; /* total amount of WAL bytes generated */
194194
} Counters;
195195

196+
/*
197+
* Global statistics for pg_stat_statements
198+
*/
199+
typedef struct pgssGlobalStats
200+
{
201+
int64 dealloc; /* # of times entries were deallocated */
202+
} pgssGlobalStats;
203+
196204
/*
197205
* Statistics per statement
198206
*
@@ -222,6 +230,7 @@ typedef struct pgssSharedState
222230
Size extent; /* current extent of query file */
223231
int n_writers; /* number of active writers to query file */
224232
int gc_count; /* query file garbage collection cycle count */
233+
pgssGlobalStats stats; /* global statistics for pgss */
225234
} pgssSharedState;
226235

227236
/*
@@ -327,6 +336,7 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_2);
327336
PG_FUNCTION_INFO_V1(pg_stat_statements_1_3);
328337
PG_FUNCTION_INFO_V1(pg_stat_statements_1_8);
329338
PG_FUNCTION_INFO_V1(pg_stat_statements);
339+
PG_FUNCTION_INFO_V1(pg_stat_statements_info);
330340

331341
static void pgss_shmem_startup(void);
332342
static void pgss_shmem_shutdown(int code, Datum arg);
@@ -554,6 +564,7 @@ pgss_shmem_startup(void)
554564
pgss->extent = 0;
555565
pgss->n_writers = 0;
556566
pgss->gc_count = 0;
567+
pgss->stats.dealloc = 0;
557568
}
558569

559570
memset(&info, 0, sizeof(info));
@@ -673,6 +684,10 @@ pgss_shmem_startup(void)
673684
entry->counters = temp.counters;
674685
}
675686

687+
/* Read global statistics for pg_stat_statements */
688+
if (fread(&pgss->stats, sizeof(pgssGlobalStats), 1, file) != 1)
689+
goto read_error;
690+
676691
pfree(buffer);
677692
FreeFile(file);
678693
FreeFile(qfile);
@@ -794,6 +809,10 @@ pgss_shmem_shutdown(int code, Datum arg)
794809
}
795810
}
796811

812+
/* Dump global statistics for pg_stat_statements */
813+
if (fwrite(&pgss->stats, sizeof(pgssGlobalStats), 1, file) != 1)
814+
goto error;
815+
797816
free(qbuffer);
798817
qbuffer = NULL;
799818

@@ -1863,6 +1882,26 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
18631882
tuplestore_donestoring(tupstore);
18641883
}
18651884

1885+
/*
1886+
* Return statistics of pg_stat_statements.
1887+
*/
1888+
Datum
1889+
pg_stat_statements_info(PG_FUNCTION_ARGS)
1890+
{
1891+
pgssGlobalStats stats;
1892+
1893+
/* Read global statistics for pg_stat_statements */
1894+
{
1895+
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
1896+
1897+
SpinLockAcquire(&s->mutex);
1898+
stats = s->stats;
1899+
SpinLockRelease(&s->mutex);
1900+
}
1901+
1902+
PG_RETURN_INT64(stats.dealloc);
1903+
}
1904+
18661905
/*
18671906
* Estimate shared memory space needed.
18681907
*/
@@ -2018,6 +2057,15 @@ entry_dealloc(void)
20182057
}
20192058

20202059
pfree(entries);
2060+
2061+
/* Increment the number of times entries are deallocated */
2062+
{
2063+
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
2064+
2065+
SpinLockAcquire(&s->mutex);
2066+
s->stats.dealloc += 1;
2067+
SpinLockRelease(&s->mutex);
2068+
}
20212069
}
20222070

20232071
/*
@@ -2504,6 +2552,15 @@ entry_reset(Oid userid, Oid dbid, uint64 queryid)
25042552
hash_search(pgss_hash, &entry->key, HASH_REMOVE, NULL);
25052553
num_remove++;
25062554
}
2555+
2556+
/* Reset global statistics for pg_stat_statements */
2557+
{
2558+
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
2559+
2560+
SpinLockAcquire(&s->mutex);
2561+
s->stats.dealloc = 0;
2562+
SpinLockRelease(&s->mutex);
2563+
}
25072564
}
25082565

25092566
/* All entries are removed? */
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pg_stat_statements extension
22
comment = 'track planning and execution statistics of all SQL statements executed'
3-
default_version = '1.8'
3+
default_version = '1.9'
44
module_pathname = '$libdir/pg_stat_statements'
55
relocatable = true

contrib/pg_stat_statements/sql/pg_stat_statements.sql

+6
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,10 @@ SELECT 42;
358358
SELECT 42;
359359
SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
360360

361+
--
362+
-- access to pg_stat_statements_info view
363+
--
364+
SELECT pg_stat_statements_reset();
365+
SELECT dealloc FROM pg_stat_statements_info;
366+
361367
DROP EXTENSION pg_stat_statements;

doc/src/sgml/pgstatstatements.sgml

+56-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
<para>
2424
When <filename>pg_stat_statements</filename> is loaded, it tracks
2525
statistics across all databases of the server. To access and manipulate
26-
these statistics, the module provides a view, <structname>pg_stat_statements</structname>,
26+
these statistics, the module provides views
27+
<structname>pg_stat_statements</structname> and
28+
<structname>pg_stat_statements_info</structname>,
2729
and the utility functions <function>pg_stat_statements_reset</function> and
2830
<function>pg_stat_statements</function>. These are not available globally but
2931
can be enabled for a specific database with
@@ -480,6 +482,52 @@
480482
</para>
481483
</sect2>
482484

485+
<sect2>
486+
<title>The <structname>pg_stat_statements_info</structname> View</title>
487+
488+
<indexterm>
489+
<primary>pg_stat_statements_info</primary>
490+
</indexterm>
491+
492+
<para>
493+
The statistics of the <filename>pg_stat_statements</filename> module
494+
itself are tracked and made available via a view named
495+
<structname>pg_stat_statements_info</structname>. This view contains
496+
only a single row. The columns of the view are shown in
497+
<xref linkend="pgstatstatementsinfo-columns"/>.
498+
</para>
499+
500+
<table id="pgstatstatementsinfo-columns">
501+
<title><structname>pg_stat_statements_info</structname> Columns</title>
502+
<tgroup cols="1">
503+
<thead>
504+
<row>
505+
<entry role="catalog_table_entry"><para role="column_definition">
506+
Column Type
507+
</para>
508+
<para>
509+
Description
510+
</para></entry>
511+
</row>
512+
</thead>
513+
514+
<tbody>
515+
<row>
516+
<entry role="catalog_table_entry"><para role="column_definition">
517+
<structfield>dealloc</structfield> <type>bigint</type>
518+
</para>
519+
<para>
520+
Total number of times <structname>pg_stat_statements</structname>
521+
entries about the least-executed statements were deallocated
522+
because more distinct statements than
523+
<varname>pg_stat_statements.max</varname> were observed
524+
</para></entry>
525+
</row>
526+
</tbody>
527+
</tgroup>
528+
</table>
529+
</sect2>
530+
483531
<sect2>
484532
<title>Functions</title>
485533

@@ -501,9 +549,10 @@
501549
specified, the default value <literal>0</literal>(invalid) is used for
502550
each of them and the statistics that match with other parameters will be
503551
reset. If no parameter is specified or all the specified parameters are
504-
<literal>0</literal>(invalid), it will discard all statistics. By
505-
default, this function can only be executed by superusers. Access may be
506-
granted to others using <command>GRANT</command>.
552+
<literal>0</literal>(invalid), it will discard all statistics including
553+
the statistics that <structname>pg_stat_statements_info</structname>
554+
displays. By default, this function can only be executed by superusers.
555+
Access may be granted to others using <command>GRANT</command>.
507556
</para>
508557
</listitem>
509558
</varlistentry>
@@ -555,7 +604,9 @@
555604
statements tracked by the module (i.e., the maximum number of rows
556605
in the <structname>pg_stat_statements</structname> view). If more distinct
557606
statements than that are observed, information about the least-executed
558-
statements is discarded.
607+
statements is discarded. The number of times such information was
608+
discarded can be seen in the
609+
<structname>pg_stat_statements_info</structname> view.
559610
The default value is 5000.
560611
This parameter can only be set at server start.
561612
</para>

src/tools/pgindent/typedefs.list

+1
Original file line numberDiff line numberDiff line change
@@ -3215,6 +3215,7 @@ pgpid_t
32153215
pgsocket
32163216
pgsql_thing_t
32173217
pgssEntry
3218+
pgssGlobalStats
32183219
pgssHashKey
32193220
pgssJumbleState
32203221
pgssLocationLen

0 commit comments

Comments
 (0)