Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2e0fedf

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_infoview. This column indicates the time at which all statistics in thepg_stat_statements view were last reset.Per discussion, this commit also changes pg_stat_statements_info codeso 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 sakeof consistency. Previously "dealloc" was reset only whenpg_stat_statements_reset(0, 0, 0) is called and was not reset whenpg_stat_statements_reset() with non-zero value argument discards allentries. This was confusing.Author: Naoki Nakamichi, Yuki SeinoReviewed-by: Yuki Seino, Kyotaro Horiguchi, Li Japin, Fujii MasaoDiscussion:https://postgr.es/m/c102cf3180d0ee73c1c5a0f7f8558322@oss.nttdata.com
1 parent00f690a commit2e0fedf

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
--- Define pg_stat_statements_info
77
CREATEFUNCTIONpg_stat_statements_info(
8-
OUT deallocbigint
8+
OUT deallocbigint,
9+
OUT stats_resettimestamp with time zone
910
)
10-
RETURNSbigint
11+
RETURNSrecord
1112
AS'MODULE_PATHNAME'
1213
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
1314

‎contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 35 additions & 11 deletions
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
#definePGSS_TEXT_FILEPG_STAT_TMP_DIR "/pgss_query_texts.stat"
99100

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

103104
/* PostgreSQL major version number, changes in which invalidate all entries */
104105
staticconstuint32PGSS_PG_MAJOR_VERSION=PG_VERSION_NUM /100;
@@ -199,6 +200,7 @@ typedef struct Counters
199200
typedefstructpgssGlobalStats
200201
{
201202
int64dealloc;/* # of times entries were deallocated */
203+
TimestampTzstats_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+
#definePG_STAT_STATEMENTS_INFO_COLS2
1889+
18841890
/*
18851891
* Return statistics of pg_stat_statements.
18861892
*/
18871893
Datum
18881894
pg_stat_statements_info(PG_FUNCTION_ARGS)
18891895
{
18901896
pgssGlobalStatsstats;
1897+
TupleDesctupdesc;
1898+
Datumvalues[PG_STAT_STATEMENTS_INFO_COLS];
1899+
boolnulls[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-
volatilepgssSharedState*s= (volatilepgssSharedState*)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
gotorelease_lock;
25682578

2579+
/*
2580+
* Reset global statistics for pg_stat_statements since all entries are
2581+
* removed.
2582+
*/
2583+
{
2584+
volatilepgssSharedState*s= (volatilepgssSharedState*)pgss;
2585+
TimestampTzstats_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

Lines changed: 15 additions & 3 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp