#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
+#include "utils/timestamp.h"
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 = 0x20201126;
+static const uint32 PGSS_FILE_HEADER = 0x20201218;
/* PostgreSQL major version number, changes in which invalidate all entries */
static const uint32 PGSS_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;
typedef struct pgssGlobalStats
{
int64 dealloc; /* # of times entries were deallocated */
+ TimestampTz stats_reset; /* timestamp with all stats reset */
} pgssGlobalStats;
/*
pgss->n_writers = 0;
pgss->gc_count = 0;
pgss->stats.dealloc = 0;
+ pgss->stats.stats_reset = GetCurrentTimestamp();
}
info.keysize = sizeof(pgssHashKey);
tuplestore_donestoring(tupstore);
}
+/* Number of output arguments (columns) for pg_stat_statements_info */
+#define PG_STAT_STATEMENTS_INFO_COLS 2
+
/*
* Return statistics of pg_stat_statements.
*/
pg_stat_statements_info(PG_FUNCTION_ARGS)
{
pgssGlobalStats stats;
+ TupleDesc tupdesc;
+ Datum values[PG_STAT_STATEMENTS_INFO_COLS];
+ bool nulls[PG_STAT_STATEMENTS_INFO_COLS];
+
+ /* Build a tuple descriptor for our result type */
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+
+ MemSet(values, 0, sizeof(values));
+ MemSet(nulls, 0, sizeof(nulls));
/* Read global statistics for pg_stat_statements */
{
SpinLockRelease(&s->mutex);
}
- PG_RETURN_INT64(stats.dealloc);
+ values[0] = Int64GetDatum(stats.dealloc);
+ values[1] = TimestampTzGetDatum(stats.stats_reset);
+
+ PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
}
/*
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? */
if (num_entries != num_remove)
goto release_lock;
+ /*
+ * Reset global statistics for pg_stat_statements since all entries are
+ * removed.
+ */
+ {
+ volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;
+ TimestampTz stats_reset = GetCurrentTimestamp();
+
+ SpinLockAcquire(&s->mutex);
+ s->stats.dealloc = 0;
+ s->stats.stats_reset = stats_reset;
+ SpinLockRelease(&s->mutex);
+ }
+
/*
* Write new empty query file, perhaps even creating a new one to recover
* if the file was missing.
<varname>pg_stat_statements.max</varname> were observed
</para></entry>
</row>
+ <row>
+ <entry role="catalog_table_entry"><para role="column_definition">
+ <structfield>stats_reset</structfield> <type>timestamp with time zone</type>
+ </para>
+ <para>
+ Time at which all statistics in the
+ <structname>pg_stat_statements</structname> view were last reset.
+ </para></entry>
+ </row>
+
</tbody>
</tgroup>
</table>
specified, the default value <literal>0</literal>(invalid) is used for
each of them and the statistics that match with other parameters will be
reset. If no parameter is specified or all the specified parameters are
- <literal>0</literal>(invalid), it will discard all statistics including
- the statistics that <structname>pg_stat_statements_info</structname>
- displays. By default, this function can only be executed by superusers.
+ <literal>0</literal>(invalid), it will discard all statistics.
+ If all statistics in the <filename>pg_stat_statements</filename>
+ view are discarded, it will also reset the statistics in the
+ <structname>pg_stat_statements_info</structname> view.
+ By default, this function can only be executed by superusers.
Access may be granted to others using <command>GRANT</command>.
</para>
</listitem>