Add stats_since and minmax_stats_since fields
parent
fdbb072ed8
commit
c78d1af782
|
@ -303,6 +303,8 @@ hash_entry_alloc(pgsmSharedState * pgsm, pgsmHashKey * key, int encoding)
|
||||||
memset(&entry->counters, 0, sizeof(Counters));
|
memset(&entry->counters, 0, sizeof(Counters));
|
||||||
entry->query_text.query_pos = InvalidDsaPointer;
|
entry->query_text.query_pos = InvalidDsaPointer;
|
||||||
entry->counters.info.parent_query = InvalidDsaPointer;
|
entry->counters.info.parent_query = InvalidDsaPointer;
|
||||||
|
entry->stats_since = GetCurrentTimestamp();
|
||||||
|
entry->minmax_stats_since = entry->stats_since;
|
||||||
|
|
||||||
/* set the appropriate initial usage count */
|
/* set the appropriate initial usage count */
|
||||||
/* re-initialize the mutex each time ... we assume no one using it */
|
/* re-initialize the mutex each time ... we assume no one using it */
|
||||||
|
|
|
@ -89,7 +89,10 @@ CREATE FUNCTION pg_stat_monitor_internal(
|
||||||
OUT jit_deform_count int8,
|
OUT jit_deform_count int8,
|
||||||
OUT jit_deform_time float8,
|
OUT jit_deform_time float8,
|
||||||
|
|
||||||
OUT toplevel BOOLEAN, --66
|
OUT stats_since timestamp with time zone, -- 66
|
||||||
|
OUT minmax_stats_since timestamp with time zone,
|
||||||
|
|
||||||
|
OUT toplevel BOOLEAN, -- 68
|
||||||
OUT bucket_done BOOLEAN
|
OUT bucket_done BOOLEAN
|
||||||
)
|
)
|
||||||
RETURNS SETOF record
|
RETURNS SETOF record
|
||||||
|
@ -439,7 +442,10 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
jit_emission_count,
|
jit_emission_count,
|
||||||
jit_emission_time,
|
jit_emission_time,
|
||||||
jit_deform_count,
|
jit_deform_count,
|
||||||
jit_deform_time
|
jit_deform_time,
|
||||||
|
|
||||||
|
stats_since,
|
||||||
|
minmax_stats_since
|
||||||
|
|
||||||
FROM pg_stat_monitor_internal(TRUE)
|
FROM pg_stat_monitor_internal(TRUE)
|
||||||
ORDER BY bucket_start_time;
|
ORDER BY bucket_start_time;
|
||||||
|
|
|
@ -42,7 +42,7 @@ PG_MODULE_MAGIC;
|
||||||
/* Number of output arguments (columns) for various API versions */
|
/* Number of output arguments (columns) for various API versions */
|
||||||
#define PG_STAT_MONITOR_COLS_V1_0 52
|
#define PG_STAT_MONITOR_COLS_V1_0 52
|
||||||
#define PG_STAT_MONITOR_COLS_V2_0 64
|
#define PG_STAT_MONITOR_COLS_V2_0 64
|
||||||
#define PG_STAT_MONITOR_COLS_V2_1 68 //TODO !!!!!!!
|
#define PG_STAT_MONITOR_COLS_V2_1 70
|
||||||
#define PG_STAT_MONITOR_COLS PG_STAT_MONITOR_COLS_V2_0 /* maximum of above */
|
#define PG_STAT_MONITOR_COLS PG_STAT_MONITOR_COLS_V2_0 /* maximum of above */
|
||||||
|
|
||||||
#define PGSM_TEXT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat_monitor_query"
|
#define PGSM_TEXT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat_monitor_query"
|
||||||
|
@ -1361,9 +1361,12 @@ pgsm_update_entry(pgsmEntry * entry,
|
||||||
int sqlcode_len = error_info ? strlen(error_info->sqlcode) : 0;
|
int sqlcode_len = error_info ? strlen(error_info->sqlcode) : 0;
|
||||||
int plan_text_len = plan_info ? plan_info->plan_len : 0;
|
int plan_text_len = plan_info ? plan_info->plan_len : 0;
|
||||||
|
|
||||||
/* Start collecting data for next bucket and reset all counters */
|
/* Start collecting data for next bucket and reset all counters and timestamps */
|
||||||
if (reset)
|
if (reset) {
|
||||||
memset(&entry->counters, 0, sizeof(Counters));
|
memset(&entry->counters, 0, sizeof(Counters));
|
||||||
|
entry->stats_since = GetCurrentTimestamp();
|
||||||
|
entry->minmax_stats_since = entry->stats_since;
|
||||||
|
}
|
||||||
|
|
||||||
/* volatile block */
|
/* volatile block */
|
||||||
{
|
{
|
||||||
|
@ -2475,10 +2478,15 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
|
||||||
values[i++] = Int64GetDatumFast(tmp.jitinfo.jit_deform_count);
|
values[i++] = Int64GetDatumFast(tmp.jitinfo.jit_deform_count);
|
||||||
values[i++] = Float8GetDatumFast(tmp.jitinfo.jit_deform_time);
|
values[i++] = Float8GetDatumFast(tmp.jitinfo.jit_deform_time);
|
||||||
}
|
}
|
||||||
/* toplevel at column number 64 */
|
|
||||||
|
/* at column number 64 */
|
||||||
|
values[i++] = TimestampTzGetDatum(entry->stats_since);
|
||||||
|
values[i++] = TimestampTzGetDatum(entry->minmax_stats_since);
|
||||||
|
|
||||||
|
/* toplevel at column number 66 */
|
||||||
values[i++] = BoolGetDatum(toplevel);
|
values[i++] = BoolGetDatum(toplevel);
|
||||||
|
|
||||||
/* bucket_done at column number 65 */
|
/* bucket_done at column number 67 */
|
||||||
values[i++] = BoolGetDatum(pg_atomic_read_u64(&pgsm->current_wbucket) != bucketid);
|
values[i++] = BoolGetDatum(pg_atomic_read_u64(&pgsm->current_wbucket) != bucketid);
|
||||||
|
|
||||||
/* clean up and return the tuplestore */
|
/* clean up and return the tuplestore */
|
||||||
|
|
|
@ -370,13 +370,15 @@ typedef struct Counters
|
||||||
*/
|
*/
|
||||||
typedef struct pgsmEntry
|
typedef struct pgsmEntry
|
||||||
{
|
{
|
||||||
pgsmHashKey key; /* hash key of entry - MUST BE FIRST */
|
pgsmHashKey key; /* hash key of entry - MUST BE FIRST */
|
||||||
uint64 pgsm_query_id; /* pgsm generate normalized query hash */
|
uint64 pgsm_query_id; /* pgsm generate normalized query hash */
|
||||||
char datname[NAMEDATALEN]; /* database name */
|
char datname[NAMEDATALEN]; /* database name */
|
||||||
char username[NAMEDATALEN]; /* user name */
|
char username[NAMEDATALEN]; /* user name */
|
||||||
Counters counters; /* the statistics for this query */
|
Counters counters; /* the statistics for this query */
|
||||||
int encoding; /* query text encoding */
|
int encoding; /* query text encoding */
|
||||||
slock_t mutex; /* protects the counters only */
|
TimestampTz stats_since; /* timestamp of entry allocation */
|
||||||
|
TimestampTz minmax_stats_since; /* timestamp of last min/max values reset */
|
||||||
|
slock_t mutex; /* protects the counters only */
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
dsa_pointer query_pos; /* query location within query buffer */
|
dsa_pointer query_pos; /* query location within query buffer */
|
||||||
|
|
|
@ -30,11 +30,11 @@ my %pg_versions_pgsm_columns = ( 17 => "application_name,".
|
||||||
"jit_inlining_count,jit_inlining_time,jit_optimization_count,jit_optimization_time," .
|
"jit_inlining_count,jit_inlining_time,jit_optimization_count,jit_optimization_time," .
|
||||||
"local_blk_read_time,local_blk_write_time,local_blks_dirtied,local_blks_hit,".
|
"local_blk_read_time,local_blk_write_time,local_blks_dirtied,local_blks_hit,".
|
||||||
"local_blks_read,local_blks_written,max_exec_time,max_plan_time,mean_exec_time," .
|
"local_blks_read,local_blks_written,max_exec_time,max_plan_time,mean_exec_time," .
|
||||||
"mean_plan_time,message,min_exec_time,min_plan_time,pgsm_query_id,planid," .
|
"mean_plan_time,message,min_exec_time,min_plan_time,minmax_stats_since," .
|
||||||
"plans,query,query_plan,queryid,relations,resp_calls,rows," .
|
"pgsm_query_id,planid,plans,query,query_plan,queryid,relations,resp_calls,rows," .
|
||||||
"shared_blk_read_time,shared_blk_write_time,shared_blks_dirtied," .
|
"shared_blk_read_time,shared_blk_write_time,shared_blks_dirtied," .
|
||||||
"shared_blks_hit,shared_blks_read,shared_blks_written," .
|
"shared_blks_hit,shared_blks_read,shared_blks_written,sqlcode,stats_since," .
|
||||||
"sqlcode,stddev_exec_time,stddev_plan_time,temp_blk_read_time,temp_blk_write_time," .
|
"stddev_exec_time,stddev_plan_time,temp_blk_read_time,temp_blk_write_time," .
|
||||||
"temp_blks_read,temp_blks_written,top_query,top_queryid,toplevel," .
|
"temp_blks_read,temp_blks_written,top_query,top_queryid,toplevel," .
|
||||||
"total_exec_time,total_plan_time,userid,username,wal_bytes,wal_fpi,wal_records",
|
"total_exec_time,total_plan_time,userid,username,wal_bytes,wal_fpi,wal_records",
|
||||||
16 => "application_name,blk_read_time," .
|
16 => "application_name,blk_read_time," .
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
#!/usr/bin/perl
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use File::Basename;
|
||||||
|
use File::Compare;
|
||||||
|
use File::Copy;
|
||||||
|
use Text::Trim qw(trim);
|
||||||
|
use Test::More;
|
||||||
|
use lib 't';
|
||||||
|
use pgsm;
|
||||||
|
|
||||||
|
# Get filename and create out file name and dirs where requried
|
||||||
|
PGSM::setup_files_dir(basename($0));
|
||||||
|
|
||||||
|
if ($PGSM::PG_MAJOR_VERSION <= 16)
|
||||||
|
{
|
||||||
|
plan skip_all => "pg_stat_monitor test cases for versions 16 and below.";
|
||||||
|
}
|
||||||
|
|
||||||
|
# Create new PostgreSQL node and do initdb
|
||||||
|
my $node = PGSM->pgsm_init_pg();
|
||||||
|
my $pgdata = $node->data_dir;
|
||||||
|
|
||||||
|
# Update postgresql.conf to include/load pg_stat_monitor library
|
||||||
|
$node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_monitor'");
|
||||||
|
|
||||||
|
# Set change postgresql.conf for this test case.
|
||||||
|
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 1");
|
||||||
|
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 3");
|
||||||
|
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_normalized_query = yes");
|
||||||
|
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track = 'all'");
|
||||||
|
|
||||||
|
# Start server
|
||||||
|
my $rt_value = $node->start;
|
||||||
|
ok($rt_value == 1, "Start Server");
|
||||||
|
|
||||||
|
# Create EXTENSION and change out file permissions
|
||||||
|
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
|
||||||
|
ok($cmdret == 0, "Create PGSM EXTENSION");
|
||||||
|
PGSM::append_to_debug_file($stdout);
|
||||||
|
|
||||||
|
# Run 'SELECT pg_stat_monitor settings' and dump output to out file
|
||||||
|
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals, boot_val, reset_val, pending_restart FROM pg_settings WHERE name LIKE '%pg_stat_monitor%';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
|
||||||
|
ok($cmdret == 0, "Print PGSM EXTENSION Settings");
|
||||||
|
PGSM::append_to_debug_file($stdout);
|
||||||
|
|
||||||
|
# Run required commands/queries and dump output to out file.
|
||||||
|
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
|
||||||
|
ok($cmdret == 0, "Reset PGSM EXTENSION");
|
||||||
|
PGSM::append_to_debug_file($stdout);
|
||||||
|
|
||||||
|
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_sleep(1);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
|
||||||
|
ok($cmdret == 0, "1 - Run pg_sleep(1)");
|
||||||
|
PGSM::append_to_debug_file($stdout);
|
||||||
|
|
||||||
|
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_sleep(1);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
|
||||||
|
ok($cmdret == 0, "2 - Run pg_sleep(1)");
|
||||||
|
PGSM::append_to_debug_file($stdout);
|
||||||
|
|
||||||
|
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_sleep(1);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
|
||||||
|
ok($cmdret == 0, "3 - Run pg_sleep(1)");
|
||||||
|
PGSM::append_to_debug_file($stdout);
|
||||||
|
|
||||||
|
# Check that we have more than one bucket
|
||||||
|
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT COUNT(bucket) != 0 AS PGSM FROM pg_stat_monitor WHERE query LIKE '%sleep%';", extra_params => ['-t', '-Pformat=unaligned','-Ptuples_only=on']);
|
||||||
|
trim($stdout);
|
||||||
|
is($stdout,'t',"Check: we have more that one bucket");
|
||||||
|
|
||||||
|
# Check that stats timestamps are different for each query/bucket
|
||||||
|
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT COUNT(DISTINCT stats_since) = COUNT(stats_since) AS PGSM FROM pg_stat_monitor WHERE query LIKE '%sleep%';", extra_params => ['-t', '-Pformat=unaligned','-Ptuples_only=on']);
|
||||||
|
trim($stdout);
|
||||||
|
is($stdout,'t',"Check: for every bucket stats_since should be unique");
|
||||||
|
|
||||||
|
# Check that minmax_stats_since always match stats_since
|
||||||
|
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT COUNT(*) AS ST FROM pg_stat_monitor WHERE query LIKE '%sleep%' AND stats_since != minmax_stats_since;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
|
||||||
|
ok($stdout == 0, "Compare: Calls count is 1");
|
||||||
|
PGSM::append_to_debug_file($stdout);
|
||||||
|
|
||||||
|
# DROP EXTENSION
|
||||||
|
$stdout = $node->safe_psql('postgres', 'DROP EXTENSION pg_stat_monitor;', extra_params => ['-a']);
|
||||||
|
ok($cmdret == 0, "DROP PGSM EXTENSION");
|
||||||
|
PGSM::append_to_debug_file($stdout);
|
||||||
|
|
||||||
|
# Stop the server
|
||||||
|
$node->stop;
|
||||||
|
|
||||||
|
# Done testing for this testcase file.
|
||||||
|
done_testing();
|
Loading…
Reference in New Issue