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

Commit 2e0fedf

Browse files
committed
pg_stat_statements: Track time at which all statistics were last reset.
This commit adds "stats_reset" column into the pg_stat_statements_info view. This column indicates the time at which all statistics in the pg_stat_statements view were last reset. Per discussion, this commit also changes pg_stat_statements_info code so that "dealloc" column is reset at the same time as "stats_reset" is reset, i.e., whenever all pg_stat_statements entries are removed, for the sake of consistency. Previously "dealloc" was reset only when pg_stat_statements_reset(0, 0, 0) is called and was not reset when pg_stat_statements_reset() with non-zero value argument discards all entries. This was confusing. Author: Naoki Nakamichi, Yuki Seino Reviewed-by: Yuki Seino, Kyotaro Horiguchi, Li Japin, Fujii Masao Discussion: https://postgr.es/m/c102cf3180d0ee73c1c5a0f7f8558322@oss.nttdata.com
1 parent 00f690a commit 2e0fedf

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

contrib/pg_stat_statements/pg_stat_statements--1.8--1.9.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
--- Define pg_stat_statements_info
77
CREATE FUNCTION pg_stat_statements_info(
8-
OUT dealloc bigint
8+
OUT dealloc bigint,
9+
OUT stats_reset timestamp with time zone
910
)
10-
RETURNS bigint
11+
RETURNS record
1112
AS 'MODULE_PATHNAME'
1213
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
1314

contrib/pg_stat_statements/pg_stat_statements.c

+35-11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include "utils/acl.h"
8282
#include "utils/builtins.h"
8383
#include "utils/memutils.h"
84+
#include "utils/timestamp.h"
8485

8586
PG_MODULE_MAGIC;
8687

@@ -98,7 +99,7 @@ PG_MODULE_MAGIC;
9899
#define PGSS_TEXT_FILE PG_STAT_TMP_DIR "/pgss_query_texts.stat"
99100

100101
/* Magic number identifying the stats file format */
101-
static const uint32 PGSS_FILE_HEADER = 0x20201126;
102+
static const uint32 PGSS_FILE_HEADER = 0x20201218;
102103

103104
/* PostgreSQL major version number, changes in which invalidate all entries */
104105
static const uint32 PGSS_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;
@@ -199,6 +200,7 @@ typedef struct Counters
199200
typedef struct pgssGlobalStats
200201
{
201202
int64 dealloc; /* # of times entries were deallocated */
203+
TimestampTz stats_reset; /* timestamp with all stats reset */
202204
} pgssGlobalStats;
203205

204206
/*
@@ -565,6 +567,7 @@ pgss_shmem_startup(void)
565567
pgss->n_writers = 0;
566568
pgss->gc_count = 0;
567569
pgss->stats.dealloc = 0;
570+
pgss->stats.stats_reset = GetCurrentTimestamp();
568571
}
569572

570573
info.keysize = sizeof(pgssHashKey);
@@ -1881,13 +1884,26 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
18811884
tuplestore_donestoring(tupstore);
18821885
}
18831886

1887+
/* Number of output arguments (columns) for pg_stat_statements_info */
1888+
#define PG_STAT_STATEMENTS_INFO_COLS 2
1889+
18841890
/*
18851891
* Return statistics of pg_stat_statements.
18861892
*/
18871893
Datum
18881894
pg_stat_statements_info(PG_FUNCTION_ARGS)
18891895
{
18901896
pgssGlobalStats stats;
1897+
TupleDesc tupdesc;
1898+
Datum values[PG_STAT_STATEMENTS_INFO_COLS];
1899+
bool nulls[PG_STAT_STATEMENTS_INFO_COLS];
1900+
1901+
/* Build a tuple descriptor for our result type */
1902+
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1903+
elog(ERROR, "return type must be a row type");
1904+
1905+
MemSet(values, 0, sizeof(values));
1906+
MemSet(nulls, 0, sizeof(nulls));
18911907

18921908
/* Read global statistics for pg_stat_statements */
18931909
{
@@ -1898,7 +1914,10 @@ pg_stat_statements_info(PG_FUNCTION_ARGS)
18981914
SpinLockRelease(&s->mutex);
18991915
}
19001916

1901-
PG_RETURN_INT64(stats.dealloc);
1917+
values[0] = Int64GetDatum(stats.dealloc);
1918+
values[1] = TimestampTzGetDatum(stats.stats_reset);
1919+
1920+
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
19021921
}
19031922

19041923
/*
@@ -2551,21 +2570,26 @@ entry_reset(Oid userid, Oid dbid, uint64 queryid)
25512570
hash_search(pgss_hash, &entry->key, HASH_REMOVE, NULL);
25522571
num_remove++;
25532572
}
2554-
2555-
/* Reset global statistics for pg_stat_statements */
2556-
{
2557-
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
2558-
2559-
SpinLockAcquire(&s->mutex);
2560-
s->stats.dealloc = 0;
2561-
SpinLockRelease(&s->mutex);
2562-
}
25632573
}
25642574

25652575
/* All entries are removed? */
25662576
if (num_entries != num_remove)
25672577
goto release_lock;
25682578

2579+
/*
2580+
* Reset global statistics for pg_stat_statements since all entries are
2581+
* removed.
2582+
*/
2583+
{
2584+
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
2585+
TimestampTz stats_reset = GetCurrentTimestamp();
2586+
2587+
SpinLockAcquire(&s->mutex);
2588+
s->stats.dealloc = 0;
2589+
s->stats.stats_reset = stats_reset;
2590+
SpinLockRelease(&s->mutex);
2591+
}
2592+
25692593
/*
25702594
* Write new empty query file, perhaps even creating a new one to recover
25712595
* if the file was missing.

doc/src/sgml/pgstatstatements.sgml

+15-3
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,16 @@
523523
<varname>pg_stat_statements.max</varname> were observed
524524
</para></entry>
525525
</row>
526+
<row>
527+
<entry role="catalog_table_entry"><para role="column_definition">
528+
<structfield>stats_reset</structfield> <type>timestamp with time zone</type>
529+
</para>
530+
<para>
531+
Time at which all statistics in the
532+
<structname>pg_stat_statements</structname> view were last reset.
533+
</para></entry>
534+
</row>
535+
526536
</tbody>
527537
</tgroup>
528538
</table>
@@ -549,9 +559,11 @@
549559
specified, the default value <literal>0</literal>(invalid) is used for
550560
each of them and the statistics that match with other parameters will be
551561
reset. If no parameter is specified or all the specified parameters are
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.
562+
<literal>0</literal>(invalid), it will discard all statistics.
563+
If all statistics in the <filename>pg_stat_statements</filename>
564+
view are discarded, it will also reset the statistics in the
565+
<structname>pg_stat_statements_info</structname> view.
566+
By default, this function can only be executed by superusers.
555567
Access may be granted to others using <command>GRANT</command>.
556568
</para>
557569
</listitem>

0 commit comments

Comments
 (0)