Skip to content

Commit

Permalink
Rename I/O timing statistics columns to shared_blk_{read|write}_time
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed May 22, 2024
1 parent 3c68a60 commit 9d8b072
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MODULE_big = pg_stat_monitor
OBJS = hash_query.o guc.o pg_stat_monitor.o $(WIN32RES)

EXTENSION = pg_stat_monitor
DATA = pg_stat_monitor--2.0.sql pg_stat_monitor--1.0--2.0.sql
DATA = pg_stat_monitor--2.0.sql pg_stat_monitor--1.0--2.0.sql pg_stat_monitor--2.0--2.1.sql

PGFILEDESC = "pg_stat_monitor - execution statistics of SQL statements"

Expand Down
212 changes: 212 additions & 0 deletions pg_stat_monitor--2.0--2.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/* contrib/pg_stat_monitor/pg_stat_monitor--1.0--2.0.sql */

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "ALTER EXTENSION pg_stat_monitor" to load this file. \quit

DROP FUNCTION pg_stat_monitor_internal CASCADE;
DROP FUNCTION pgsm_create_view CASCADE;

CREATE FUNCTION pg_stat_monitor_internal(
IN showtext boolean,
OUT bucket int8, -- 0
OUT userid oid,
OUT username text,
OUT dbid oid,
OUT datname text,
OUT client_ip int8,

OUT queryid int8, -- 4
OUT planid int8,
OUT query text,
OUT query_plan text,
OUT pgsm_query_id int8,
OUT top_queryid int8,
OUT top_query text,
OUT application_name text,

OUT relations text, -- 11
OUT cmd_type int,
OUT elevel int,
OUT sqlcode TEXT,
OUT message text,
OUT bucket_start_time timestamptz,

OUT calls int8, -- 16

OUT total_exec_time float8,
OUT min_exec_time float8,
OUT max_exec_time float8,
OUT mean_exec_time float8,
OUT stddev_exec_time float8,

OUT rows int8,

OUT plans int8, -- 23

OUT total_plan_time float8,
OUT min_plan_time float8,
OUT max_plan_time float8,
OUT mean_plan_time float8,
OUT stddev_plan_time float8,

OUT shared_blks_hit int8, -- 29
OUT shared_blks_read int8,
OUT shared_blks_dirtied int8,
OUT shared_blks_written int8,
OUT local_blks_hit int8,
OUT local_blks_read int8,
OUT local_blks_dirtied int8,
OUT local_blks_written int8,
OUT temp_blks_read int8,
OUT temp_blks_written int8,
OUT shared_blk_read_time float8,
OUT shared_blk_write_time float8,

OUT temp_blk_read_time float8,
OUT temp_blk_write_time float8,

OUT resp_calls text, -- 41
OUT cpu_user_time float8,
OUT cpu_sys_time float8,
OUT wal_records int8,
OUT wal_fpi int8,
OUT wal_bytes numeric,
OUT comments TEXT,

OUT jit_functions int8,
OUT jit_generation_time float8,
OUT jit_inlining_count int8,
OUT jit_inlining_time float8,
OUT jit_optimization_count int8,
OUT jit_optimization_time float8,
OUT jit_emission_count int8,
OUT jit_emission_time float8,

OUT toplevel BOOLEAN,
OUT bucket_done BOOLEAN
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'pg_stat_monitor_2_1'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;

CREATE FUNCTION pgsm_create_17_view() RETURNS INT AS
$$
BEGIN
CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time AS bucket_start_time,
userid,
username,
dbid,
datname,
'0.0.0.0'::inet + client_ip AS client_ip,
pgsm_query_id,
queryid,
toplevel,
top_queryid,
query,
comments,
planid,
query_plan,
top_query,
application_name,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
elevel,
sqlcode,
message,
calls,
total_exec_time,
min_exec_time,
max_exec_time,
mean_exec_time,
stddev_exec_time,
rows,
shared_blks_hit,
shared_blks_read,
shared_blks_dirtied,
shared_blks_written,
local_blks_hit,
local_blks_read,
local_blks_dirtied,
local_blks_written,
temp_blks_read,
temp_blks_written,
shared_blk_read_time,
shared_blk_write_time,
temp_blk_read_time,
temp_blk_write_time,

(string_to_array(resp_calls, ',')) resp_calls,
cpu_user_time,
cpu_sys_time,
wal_records,
wal_fpi,
wal_bytes,
bucket_done,

plans,
total_plan_time,
min_plan_time,
max_plan_time,
mean_plan_time,
stddev_plan_time,

jit_functions,
jit_generation_time,
jit_inlining_count,
jit_inlining_time,
jit_optimization_count,
jit_optimization_time,
jit_emission_count,
jit_emission_time

FROM pg_stat_monitor_internal(TRUE)
ORDER BY bucket_start_time;
RETURN 0;
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION pgsm_create_view() RETURNS INT AS
$$
DECLARE ver integer;
BEGIN
SELECT current_setting('server_version_num') INTO ver;
IF (ver >= 170000) THEN
return pgsm_create_17_view();
END IF;
IF (ver >= 150000) THEN
return pgsm_create_15_view();
END IF;
IF (ver >= 140000) THEN
return pgsm_create_14_view();
END IF;
IF (ver >= 130000) THEN
return pgsm_create_13_view();
END IF;
IF (ver >= 110000) THEN
return pgsm_create_11_view();
END IF;
RETURN 0;
END;
$$ LANGUAGE plpgsql;

SELECT pgsm_create_view();
REVOKE ALL ON FUNCTION pgsm_create_view FROM PUBLIC;
REVOKE ALL ON FUNCTION pgsm_create_11_view FROM PUBLIC;
REVOKE ALL ON FUNCTION pgsm_create_13_view FROM PUBLIC;
REVOKE ALL ON FUNCTION pgsm_create_14_view FROM PUBLIC;
REVOKE ALL ON FUNCTION pgsm_create_15_view FROM PUBLIC;
REVOKE ALL ON FUNCTION pgsm_create_17_view FROM PUBLIC;

GRANT EXECUTE ON FUNCTION range TO PUBLIC;
GRANT EXECUTE ON FUNCTION decode_error_level TO PUBLIC;
GRANT EXECUTE ON FUNCTION get_histogram_timings TO PUBLIC;
GRANT EXECUTE ON FUNCTION get_cmd_type TO PUBLIC;
GRANT EXECUTE ON FUNCTION pg_stat_monitor_internal TO PUBLIC;

GRANT SELECT ON pg_stat_monitor TO PUBLIC;

-- Reset is only available to super user
REVOKE ALL ON FUNCTION pg_stat_monitor_reset FROM PUBLIC;
59 changes: 42 additions & 17 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
typedef enum pgsmVersion
{
PGSM_V1_0 = 0,
PGSM_V2_0
PGSM_V2_0,
PGSM_V2_1
} pgsmVersion;

PG_MODULE_MAGIC;

#define BUILD_VERSION "2.0.4"
#define BUILD_VERSION "2.1.0"

/* Number of output arguments (columns) for various API versions */
#define PG_STAT_MONITOR_COLS_V1_0 52
#define PG_STAT_MONITOR_COLS_V2_0 64
#define PG_STAT_MONITOR_COLS_V2_1 64 //TODO !!!!!!!
#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"
Expand Down Expand Up @@ -145,6 +147,7 @@ PG_FUNCTION_INFO_V1(pg_stat_monitor_version);
PG_FUNCTION_INFO_V1(pg_stat_monitor_reset);
PG_FUNCTION_INFO_V1(pg_stat_monitor_1_0);
PG_FUNCTION_INFO_V1(pg_stat_monitor_2_0);
PG_FUNCTION_INFO_V1(pg_stat_monitor_2_1);
PG_FUNCTION_INFO_V1(pg_stat_monitor);
PG_FUNCTION_INFO_V1(get_histogram_timings);
PG_FUNCTION_INFO_V1(pg_stat_monitor_hook_stats);
Expand Down Expand Up @@ -1236,10 +1239,10 @@ BufferUsageAccumDiff(BufferUsage *bufusage, BufferUsage *pgBufferUsage, BufferUs
bufusage->local_blks_written = pgBufferUsage->local_blks_written - bufusage_start->local_blks_written;
bufusage->temp_blks_read = pgBufferUsage->temp_blks_read - bufusage_start->temp_blks_read;
bufusage->temp_blks_written = pgBufferUsage->temp_blks_written - bufusage_start->temp_blks_written;
bufusage->blk_read_time = pgBufferUsage->blk_read_time;
INSTR_TIME_SUBTRACT(bufusage->blk_read_time, bufusage_start->blk_read_time);
bufusage->blk_write_time = pgBufferUsage->blk_write_time;
INSTR_TIME_SUBTRACT(bufusage->blk_write_time, bufusage_start->blk_write_time);
bufusage->shared_blk_read_time = pgBufferUsage->shared_blk_read_time;
INSTR_TIME_SUBTRACT(bufusage->shared_blk_read_time, bufusage_start->shared_blk_read_time);
bufusage->shared_blk_write_time = pgBufferUsage->shared_blk_write_time;
INSTR_TIME_SUBTRACT(bufusage->shared_blk_write_time, bufusage_start->shared_blk_write_time);
}
#endif

Expand Down Expand Up @@ -1520,11 +1523,11 @@ pgsm_update_entry(pgsmEntry * entry,
e->counters.blocks.temp_blks_written += bufusage->temp_blks_written;

#if PG_VERSION_NUM < 170000
e->counters.blocks.blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->blk_read_time);
e->counters.blocks.blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->blk_write_time);
e->counters.blocks.shared_blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->shared_blk_read_time);
e->counters.blocks.shared_blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->shared_blk_write_time);
#else
e->counters.blocks.blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->shared_blk_read_time);
e->counters.blocks.blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->shared_blk_write_time);
e->counters.blocks.shared_blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->shared_blk_read_time);
e->counters.blocks.shared_blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->shared_blk_write_time);
#endif

#if PG_VERSION_NUM >= 150000
Expand All @@ -1533,8 +1536,8 @@ pgsm_update_entry(pgsmEntry * entry,
#endif

#if PG_VERSION_NUM < 170000
memcpy((void *) &e->counters.blocks.instr_blk_read_time, &bufusage->blk_read_time, sizeof(instr_time));
memcpy((void *) &e->counters.blocks.instr_blk_write_time, &bufusage->blk_write_time, sizeof(instr_time));
memcpy((void *) &e->counters.blocks.instr_blk_read_time, &bufusage->shared_blk_read_time, sizeof(instr_time));
memcpy((void *) &e->counters.blocks.instr_blk_write_time, &bufusage->shared_blk_write_time, sizeof(instr_time));
#else
memcpy((void *) &e->counters.blocks.instr_blk_read_time, &bufusage->shared_blk_read_time, sizeof(instr_time));
memcpy((void *) &e->counters.blocks.instr_blk_write_time, &bufusage->shared_blk_write_time, sizeof(instr_time));
Expand Down Expand Up @@ -1816,8 +1819,8 @@ pgsm_store(pgsmEntry * entry)
bufusage.temp_blks_written = entry->counters.blocks.temp_blks_written;

#if PG_VERSION_NUM < 170000
memcpy(&bufusage.blk_read_time, &entry->counters.blocks.instr_blk_read_time, sizeof(instr_time));
memcpy(&bufusage.blk_write_time, &entry->counters.blocks.instr_blk_write_time, sizeof(instr_time));
memcpy(&bufusage.shared_blk_read_time, &entry->counters.blocks.instr_blk_read_time, sizeof(instr_time));
memcpy(&bufusage.shared_blk_write_time, &entry->counters.blocks.instr_blk_write_time, sizeof(instr_time));
#else
memcpy(&bufusage.shared_blk_read_time, &entry->counters.blocks.instr_blk_read_time, sizeof(instr_time));
memcpy(&bufusage.shared_blk_write_time, &entry->counters.blocks.instr_blk_write_time, sizeof(instr_time));
Expand Down Expand Up @@ -1997,6 +2000,13 @@ pg_stat_monitor_2_0(PG_FUNCTION_ARGS)
return (Datum) 0;
}

Datum
pg_stat_monitor_2_1(PG_FUNCTION_ARGS)
{
pg_stat_monitor_internal(fcinfo, PGSM_V2_1, true);
return (Datum) 0;
}

/*
* Legacy entry point for pg_stat_monitor() API versions 1.0
*/
Expand Down Expand Up @@ -2036,7 +2046,22 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
PGSM_HASH_SEQ_STATUS hstat;
pgsmEntry *entry;
pgsmSharedState *pgsm;
int expected_columns = (api_version >= PGSM_V2_0) ? PG_STAT_MONITOR_COLS_V2_0 : PG_STAT_MONITOR_COLS_V1_0;

int expected_columns;
switch (api_version) {
case PGSM_V1_0:
expected_columns = PG_STAT_MONITOR_COLS_V1_0;
break;
case PGSM_V2_0:
expected_columns = PG_STAT_MONITOR_COLS_V2_0;
break;
case PGSM_V2_1:
expected_columns = PG_STAT_MONITOR_COLS_V2_1;
break;
default:
// TODO error?
break;
}

/* Disallow old api usage */
if (api_version < PGSM_V2_0)
Expand Down Expand Up @@ -2376,8 +2401,8 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
values[i++] = Int64GetDatumFast(tmp.blocks.local_blks_written);
values[i++] = Int64GetDatumFast(tmp.blocks.temp_blks_read);
values[i++] = Int64GetDatumFast(tmp.blocks.temp_blks_written);
values[i++] = Float8GetDatumFast(tmp.blocks.blk_read_time);
values[i++] = Float8GetDatumFast(tmp.blocks.blk_write_time);
values[i++] = Float8GetDatumFast(tmp.blocks.shared_blk_read_time);
values[i++] = Float8GetDatumFast(tmp.blocks.shared_blk_write_time);
values[i++] = Float8GetDatumFast(tmp.blocks.temp_blk_read_time);
values[i++] = Float8GetDatumFast(tmp.blocks.temp_blk_write_time);

Expand Down
2 changes: 1 addition & 1 deletion pg_stat_monitor.control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pg_stat_monitor extension
comment = 'The pg_stat_monitor is a PostgreSQL Query Performance Monitoring tool, based on PostgreSQL contrib module pg_stat_statements. pg_stat_monitor provides aggregated statistics, client information, plan details including plan, and histogram information.'
default_version = '2.0'
default_version = '2.1'
module_pathname = '$libdir/pg_stat_monitor'
relocatable = true
4 changes: 2 additions & 2 deletions pg_stat_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ typedef struct Blocks
int64 local_blks_written; /* # of local disk blocks written */
int64 temp_blks_read; /* # of temp blocks read */
int64 temp_blks_written; /* # of temp blocks written */
double blk_read_time; /* time spent reading, in msec */
double blk_write_time; /* time spent writing, in msec */
double shared_blk_read_time; /* time spent reading shared blocks, in msec */
double shared_blk_write_time; /* time spent writing shared blocks, in msec */

double temp_blk_read_time; /* time spent reading temp blocks, in msec */
double temp_blk_write_time; /* time spent writing temp blocks, in
Expand Down
3 changes: 2 additions & 1 deletion regression/expected/functions.out
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ SELECT routine_schema, routine_name, routine_type, data_type FROM information_sc
public | pgsm_create_13_view | FUNCTION | integer
public | pgsm_create_14_view | FUNCTION | integer
public | pgsm_create_15_view | FUNCTION | integer
public | pgsm_create_17_view | FUNCTION | integer
public | pgsm_create_view | FUNCTION | integer
public | range | FUNCTION | ARRAY
(13 rows)
(14 rows)

SET ROLE u1;
SELECT routine_schema, routine_name, routine_type, data_type FROM information_schema.routines WHERE routine_schema = 'public' ORDER BY routine_name COLLATE "C";
Expand Down
3 changes: 2 additions & 1 deletion regression/expected/functions_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ SELECT routine_schema, routine_name, routine_type, data_type FROM information_sc
public | pgsm_create_13_view | FUNCTION | integer
public | pgsm_create_14_view | FUNCTION | integer
public | pgsm_create_15_view | FUNCTION | integer
public | pgsm_create_17_view | FUNCTION | integer
public | pgsm_create_view | FUNCTION | integer
public | range | FUNCTION | ARRAY
(13 rows)
(14 rows)

SET ROLE u1;
SELECT routine_schema, routine_name, routine_type, data_type FROM information_schema.routines WHERE routine_schema = 'public' ORDER BY routine_name COLLATE "C";
Expand Down

0 comments on commit 9d8b072

Please sign in to comment.