mirror of
https://github.com/percona/pg_stat_monitor.git
synced 2026-02-04 22:16:20 +00:00
PG-159: Bucket start time (bucket_start_time) should be aligned with bucket_time.
This commit is contained in:
@@ -28,7 +28,7 @@ CREATE FUNCTION pg_stat_monitor(IN showtext boolean,
|
|||||||
OUT elevel int,
|
OUT elevel int,
|
||||||
OUT sqlcode int,
|
OUT sqlcode int,
|
||||||
OUT message text,
|
OUT message text,
|
||||||
OUT bucket_start_time timestamptz,
|
OUT bucket_start_time text,
|
||||||
|
|
||||||
OUT plans int8,
|
OUT plans int8,
|
||||||
OUT plan_total_time float8,
|
OUT plan_total_time float8,
|
||||||
@@ -92,7 +92,7 @@ FROM pg_stat_monitor_settings();
|
|||||||
-- Register a view on the function for ease of use.
|
-- Register a view on the function for ease of use.
|
||||||
CREATE VIEW pg_stat_monitor AS SELECT
|
CREATE VIEW pg_stat_monitor AS SELECT
|
||||||
bucket,
|
bucket,
|
||||||
bucket_start_time,
|
bucket_start_time AS bucket_start_time,
|
||||||
userid,
|
userid,
|
||||||
dbid,
|
dbid,
|
||||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||||
|
|||||||
@@ -1189,9 +1189,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
|
|||||||
nulls[i++] = true;
|
nulls[i++] = true;
|
||||||
else
|
else
|
||||||
values[i++] = CStringGetTextDatum(tmp.error.message);
|
values[i++] = CStringGetTextDatum(tmp.error.message);
|
||||||
|
values[i++] = CStringGetTextDatum(pgss->bucket_start_time[entry->key.bucket_id]);
|
||||||
values[i++] = TimestampGetDatum(pgss->bucket_start_time[entry->key.bucket_id]);
|
|
||||||
|
|
||||||
for (kind = 0; kind < PGSS_NUMKIND; kind++)
|
for (kind = 0; kind < PGSS_NUMKIND; kind++)
|
||||||
{
|
{
|
||||||
values[i++] = Int64GetDatumFast(tmp.calls[kind].calls);
|
values[i++] = Int64GetDatumFast(tmp.calls[kind].calls);
|
||||||
@@ -1238,17 +1236,19 @@ get_next_wbucket(pgssSharedState *pgss)
|
|||||||
uint64 current_usec;
|
uint64 current_usec;
|
||||||
uint64 bucket_id;
|
uint64 bucket_id;
|
||||||
char file_name[1024];
|
char file_name[1024];
|
||||||
|
struct tm *lt;
|
||||||
|
int sec = 0;
|
||||||
|
|
||||||
gettimeofday(&tv,NULL);
|
gettimeofday(&tv,NULL);
|
||||||
current_usec = tv.tv_sec;
|
current_usec = tv.tv_usec;
|
||||||
|
|
||||||
if ((current_usec - pgss->prev_bucket_usec) > PGSM_BUCKET_TIME)
|
current_usec = (TimestampTz) tv.tv_sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
|
||||||
|
current_usec = (current_usec * USECS_PER_SEC) + tv.tv_usec;
|
||||||
|
|
||||||
|
if ((current_usec - pgss->prev_bucket_usec) > (PGSM_BUCKET_TIME * 1000 * 1000))
|
||||||
{
|
{
|
||||||
unsigned char *buf;
|
unsigned char *buf;
|
||||||
bucket_id = pgss->current_wbucket + 1;
|
bucket_id = (tv.tv_sec / PGSM_BUCKET_TIME) % PGSM_MAX_BUCKETS;
|
||||||
if (bucket_id == PGSM_MAX_BUCKETS)
|
|
||||||
bucket_id = 0;
|
|
||||||
|
|
||||||
LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
|
LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
|
||||||
buf = pgss_qbuf[bucket_id];
|
buf = pgss_qbuf[bucket_id];
|
||||||
hash_entry_dealloc(bucket_id);
|
hash_entry_dealloc(bucket_id);
|
||||||
@@ -1260,7 +1260,11 @@ get_next_wbucket(pgssSharedState *pgss)
|
|||||||
memset(buf, 0, sizeof (uint64));
|
memset(buf, 0, sizeof (uint64));
|
||||||
LWLockRelease(pgss->lock);
|
LWLockRelease(pgss->lock);
|
||||||
pgss->prev_bucket_usec = current_usec;
|
pgss->prev_bucket_usec = current_usec;
|
||||||
pgss->bucket_start_time[bucket_id] = GetCurrentTimestamp();
|
lt = localtime(&tv.tv_sec);
|
||||||
|
sec = lt->tm_sec - (lt->tm_sec % PGSM_BUCKET_TIME);
|
||||||
|
if (sec < 0)
|
||||||
|
sec = 0;
|
||||||
|
snprintf(pgss->bucket_start_time[bucket_id], sizeof(pgss->bucket_start_time[bucket_id]), "%02d-%02d-%04d %02d:%02d:%02d", lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900, lt->tm_hour, lt->tm_min, sec);
|
||||||
return bucket_id;
|
return bucket_id;
|
||||||
}
|
}
|
||||||
return pgss->current_wbucket;
|
return pgss->current_wbucket;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
|
|
||||||
@@ -242,7 +243,7 @@ typedef struct pgssSharedState
|
|||||||
int64 query_buf_size_bucket;
|
int64 query_buf_size_bucket;
|
||||||
int32 relations[REL_LST];
|
int32 relations[REL_LST];
|
||||||
char cmdTag[CMD_LST][CMD_LEN];
|
char cmdTag[CMD_LST][CMD_LEN];
|
||||||
Timestamp bucket_start_time[MAX_BUCKETS]; /* start time of the bucket */
|
char bucket_start_time[MAX_BUCKETS][60]; /* start time of the bucket */
|
||||||
} pgssSharedState;
|
} pgssSharedState;
|
||||||
|
|
||||||
#define ResetSharedState(x) \
|
#define ResetSharedState(x) \
|
||||||
|
|||||||
Reference in New Issue
Block a user